简体   繁体   中英

Deserialize Json array of array in c#

I am new to c# don't know how to get a JSON array of array stored in a 2d array. I am having a JSON file with students marks like

[
    [10,5,4],
    [9,6,3]
]

and out of this I am using this code but getting error out at JArray

JArray a = JArray.Parse(json);

I have tried some other approaches as well but nothing helped basically what I want to do is want to create a boolean 2D array which will be populated on the basis of the above JSON record and for that purpose I want to populate the array with JSON content.

With the following valid JSON

{ "data" : [
    [10,5,4],
    [9,6,3]
]
}

The following class was used to hold the parsed data

public class RootObject {
    public IList<IList<int>> data { get; set; }
}

which can be parsed using Json.Net

var root = JsonConvert.DeserializeObject<RoootObject>(json);

and the content accessed

var x1 = root.data[0][1]; // 5
var x2 = root.data[1][1]; // 6

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