I would like to parse the following json
{
"status": 1,
"data": {
"xyz": [
[
3.5927680391473826,
0.5641835941824296,
-0.88546609584117
],
[
3.5885032091080853,
0.7537851201209402,
-0.9748245317650808
]
],
"xy": [
[
479,
32
],
[
435,
85
]
]
}
}
I'm using the following code to do so,
resultantData = JObject.Parse(inputString)
List<float> xyz = resultantData["data"]["xyz"].Value<List<List<float>>>();
List<int> xy = resultantData["data"]["xy"].Value<List<int>>();
Unfortunately, the above code wasn't able to parse the JSON properly. What am I missing?
CodePudding user response:
I think you can do it using JsonConvert.DeserializeObject
var result = JsonConvert.DeserializeObject<Root>(inputString);
public partial class Root
{
[JsonProperty("status")]
public long Status { get; set; }
[JsonProperty("data")]
public Data Data { get; set; }
}
public partial class Data
{
[JsonProperty("xyz")]
public List<List<double>> Xyz { get; set; }
[JsonProperty("xy")]
public List<List<long>> Xy { get; set; }
}
CodePudding user response:
you have list of lists , not just list
List<List<float>> xyz = resultantData["data"]["xyz"].ToObject<List<List<float>>>();
List<List<int>> xy = resultantData["data"]["xy"].ToObject<List<List<int>>>();
you can use it like this
if (xyz != null && xyz.Count > 0)
{
List<float> xyz1 = xyz[0];
}
if you want to put everything in one pile, I don't think that it is a good idea , you have to try this
List<float> xyz = resultantData["data"]["xyz"].ToObject<List<List<float>>>()
.SelectMany(i=> i).ToList();
List<int> xy = resultantData["data"]["xy"].ToObject<List<List<int>>>()
.SelectMany(i=> i).ToList();