【问题标题】:C# Get Json sub valuesC# 获取 Json 子值
【发布时间】:2019-04-23 04:21:23
【问题描述】:

我有一个 API 响应,其结构如下所示。如何从这个 Json 字符串中获取值?

[
{
    "id": xxx,
    "profileId": xxx,
    "recipientId": xxx,
    "creationTime": "xxxx",
    "modificationTime": "xxxx",
    "active": true,
    "eligible": true,
    "balances": [
        {
            "balanceType": "AVAILABLE",
            "currency": "EUR",
            "amount": {
                "value": 55555,
                "currency": "EUR"
            },
            "reservedAmount": {
                "value": 0,
                "currency": "EUR"
            },
            "bankDetails": {
                "id": xxx,
                "currency": "EUR",
                "bankCode": "code",
                "accountNumber": "account number",
                "swift": "swift",
                "iban": "iban",
                "bankName": "bankName",
                "accountHolderName": "accountHolderName",
                "bankAddress": {
                    "addressFirstLine": "bankAddress",
                    "postCode": "xxxxx",
                    "city": "xxxxx",
                    "country": "xxxxx",
                    "stateCode": null
                }
            }
        }
    ]
}

]

我正在使用以下扩展方法来获取我已集成到我的系统中的其他一些 API 的数据。这适用于我集成的其他 API。

 public static string GetJsonField(RestSharp.IRestResponse value, string res)
    {
        Newtonsoft.Json.Linq.JObject json = Newtonsoft.Json.Linq.JObject.Parse(value.Content);
        res = json.GetValue(res).ToString();
        return res;
    }

提前致谢

【问题讨论】:

  • 我认为this 是您要找的。​​span>

标签: c# json string get


【解决方案1】:

在 json2csharp.com 中复制该 Json,它将为您提供在 c# 对象中转换 Json 所需的类。

然后只需使用 var myJson = JsonConverter.deserialize(Json) ;

您可以像访问任何其他类一样访问 Json 属性

【讨论】:

    猜你喜欢
    • 2022-01-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-10-31
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多