简体   繁体   中英

Get the value from array in JSON

I'm receiving JSON that typically looks something like this:

[{
    "objectName": "UDO_Job",
    "primaryKey": "123456789",
    "UDO_JobPart": [{
        "length": "24.0",
        "width": "24.0",
        "qty": "12"
    }, {
        "length": "24.0",
        "width": "24.0",
        "qty": "1"
    }, {
        "length": "36.0",
        "width": "34.0",
        "qty": "3"
    }]
}]

I need to retrieve the primaryKey value, then the contents of each element of the UDO_JobPart array ( length , width , and qty ).

I am able to get the primaryKey value with this code:

var jArray = JArray.Parse(json);
int primaryKey = jArray[0]["primaryKey"].Value<int>();

But I'm hitting a wall with getting the contents of the array. I tried something like this:

double length = jArray[0]["UDO_JobPart"][0].["length"].Value<double>();

But I'm not getting anything back. Any advice would be greatly appreciated.

Remove . between [0] and [length] .

double length = jArray[0]["UDO_JobPart"][0]["length"].Value<double>();

Sample Program


Split from 1 line code above, the structure is as (for better understanding):

var jArray = JArray.Parse(json);
var jObj = jArray[0];
var jobPart = jObj["UDO_JobPart"][0];
double length = jobPart["length"].Value<double>();

Sample program

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM