如何从JSON响应中获取值

iyzzxitl  于 2023-01-22  发布在  其他
关注(0)|答案(1)|浏览(177)

我必须JSON响应来自数据库API。
我的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\"}"
            }
        ]
    }
}

我想在不同的变量存储此响应值请任何人帮助我这是我最后一年的项目。我正在使用windows应用程序c#

fkaflof6

fkaflof61#

您应该将这个JSON反序列化为类,然后对类对象执行任何您想要的操作。
您可以使用NewtonSoft.Json dll实现此目的。

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);

您可以获得如下属性:

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

    }

如果只想获取所有ConnectivityInfo值,也可以使用LINQ

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

如果你想进一步将connectivityInfo字符串反序列化为对象并获取它的属性,那么你必须再次将它反序列化为对象。您还必须修改JSON。
修改的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\"}"
        }]
    }
}

再上一堂课:

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

然后执行以下操作:

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

    }

相关问题