从嵌套的JSON数组中获取值

时间:2017-03-21 18:07:29

标签: c# json .net-core

如何从此JSON获取单个值?例如SSN?此外,一些项目是可以容纳多个(地址,电话)的数组。解析这个的最佳方法是什么?我尝试过使用JArray.Parse(数据),但仍然无法“找到”各个项目。有人能指出我正确的方向吗?

[{
    "firstName": "test",
    "middleName": "test",
    "lastName": "test"
},
{
    "addresses":
    [{
        "street": "test",
        "city": "test",
        "state": "test",
        "zip": "test"
    }]
},
{
    "DOB": ""
},
{
    "SSN": "123123123"
},
{
    "occupation": "test",
    "typeOfOccupation": "test"
},
{
    "phones":
    [{
        "phone": "",
        "type": ""
    }]
},
{
    "Email": ""
},
{
    "typeOfId": "",
    "idNumber": "",
    "expirationDate": ""
}]

1 个答案:

答案 0 :(得分:0)

您说您使用了JArray.Parse,但是您是否将其用作dynamicCheck Query JSON with dynamicJSON.Net

var json = "[{ \"firstName\": \"test\", \"middleName\": \"test\", \"lastName\": \"test\" }, { \"addresses\": [{ \"street\": \"test\", \"city\": \"test\", \"state\": \"test\", \"zip\": \"test\" }] }, { \"DOB\": \"\" }, { \"SSN\": \"123123123\" }, { \"occupation\": \"test\", \"typeOfOccupation\": \"test\" }, { \"phones\": [{ \"phone\": \"\", \"type\": \"\" }] }, { \"Email\": \"\" }, { \"typeOfId\": \"\", \"idNumber\": \"\", \"expirationDate\": \"\" }]";
var data = JArray.Parse(json);
JToken ssn = data.SelectToken("$..SSN")
if(ssn!=null){
    Console.WriteLine(ssn.Value<string>());
}

工作fiddle here