C#获取Json子值

时间:2019-04-22 20:12:12

标签: c# json string get

我有一个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;
    }

预先感谢

1 个答案:

答案 0 :(得分:-1)

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

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

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