0

I have to JSON response comes from database API.

My JSON data:

{
    "AccessToken":"1929c63a-9c72-4c0d-aea8-345c0b5411e9",
    "LoggedInUserID":1,
    "LookupData":{
        "LiquiditySources":[
            {
                "ID":1,
                "ParentID":null,
                "Name":"testname1",
                "SequenceNo":null,
                "Caption":"test1",
                "ConnectivityInfo":
                    "{\"Port\":11111,\"Username\":testuser1,\"Password\":\"password1\"}"
            },
            {
                "ID":2,
                "ParentID":null,
                "Name":"testname2",
                "SequenceNo":null,
                "Caption":"test2",
                "ConnectivityInfo":
                    "{\"Port\":22222,\"Username\":testuser2,\"Password\":\"password2\"}"
            }
        ]
    }
}

I want to store this response values in different variables please anyone help me this is my final year project. I am using windows application c#

Vivek Nuna
  • 25,472
  • 25
  • 109
  • 197

1 Answers1

1

You should deserialize this JSON to class and then do whatever you want with the class object.

You can use NewtonSoft.Json dll for this purpose.

public class LiquiditySource
{
    public int ID { get; set; }
    public object ParentID { get; set; }
    public string Name { get; set; }
    public object SequenceNo { get; set; }
    public string Caption { get; set; }
    public string ConnectivityInfo { get; set; }
}

public class LookupData
{
    public List<LiquiditySource> LiquiditySources { get; set; }
}

public class SampleClass
{
    public string AccessToken { get; set; }
    public int LoggedInUserID { get; set; }
    public LookupData LookupData { get; set; }
}

string json = "your json string";
SampleClass obj = JsonConvert.DeserializeObject<SampleClass>(json);

You can get properties like this:

List<LiquiditySource> ls =  obj.LookupData.LiquiditySources;
    foreach(LiquiditySource liquiditySource in ls)
    {
        string connectivityInfo = liquiditySource.ConnectivityInfo;

    }

If you want only to get all ConnectivityInfo values, you can also use LINQ.

List<string> lsConnectivityInfo = obj.LookupData.LiquiditySources.Select(c => c.ConnectivityInfo).ToList();

If you want to furthur deserilize connectivityInfo string to object and get its property, then you have to again deserialize it to object. Note: You have to modify your JSON too.

Modified JSON:

{
    "AccessToken": "1929c63a-9c72-4c0d-aea8-345c0b5411e9",
    "LoggedInUserID": 1,
    "LookupData": {
        "LiquiditySources": [{
            "ID": 1,
            "ParentID": null,
            "Name": "testname1",
            "SequenceNo": null,
            "Caption": "test1",
            "ConnectivityInfo": "{\"Port\":11111,\"Username\":\"testuser1\",\"Password\":\"password1\"}"
        }, {
            "ID": 2,
            "ParentID": null,
            "Name": "testname2",
            "SequenceNo": null,
            "Caption": "test2",
            "ConnectivityInfo": "{\"Port\":22222,\"Username\":\"testuser2\",\"Password\":\"password2\"}"
        }]
    }
}

Make one more class:

public class ConnectivityInfo
{
    public int Port { get; set; }
    public string Username { get; set; }
    public string Password { get; set; }
}

Then do this:

foreach (LiquiditySource liquiditySource in ls)
    {
        string connectivityInfo = liquiditySource.ConnectivityInfo;
        ConnectivityInfo connectivityInfoObj = JsonConvert.DeserializeObject<ConnectivityInfo>(connectivityInfo);

    }
Vivek Nuna
  • 25,472
  • 25
  • 109
  • 197