Deserialize Array of arrays in C#
Deserialize Array of arrays in C#
I have a following json: need to Deserialize in C#.
[
[
"ElementName": "H1 ",
"lat": 51.394319720562514,
"lng": -109.99996662139893
,
"ElementName": "H1 ",
"lat": 51.394319720562514,
"lng": -109.99995589256287
],
[
"ElementName": "H2 ",
"lat": 51.394319720562514,
"lng": -109.99996662139893
,
"ElementName": "H2 ",
"lat": 51.394319720562514,
"lng": -109.99995589256287
]
]
I have write the following code to desirialize it.
public class PointElement
public string ElementName get; set;
public double lat get; set;
public double lng get; set;
var testPointList = JsonConvert.DeserializeObject<List<PointElement>>(testNewJson);
but console shows the error
Controllers.PointElementRecord' because the type requires a JSON
object (e.g. "name":"value") to deserialize correctly. To fix this
error either change the JSON to a JSON object (e.g. "name":"value")
or change the deserialized type to an array or a type that implements
a collection interface (e.g. ICollection, IList) like List that can
be deserialized from a JSON array. JsonArrayAttribute can also be
added to the type to force it to deserialize from a JSON array. Path
'[0]', line 1, position 2.
need solution how to deserialize this kind array string as this code runs fine for below array
[
"ElementName": "H1 ",
"lat": 51.394319720562514,
"lng": -109.99996662139893
,
"ElementName": "H1 ",
"lat": 51.394319720562514,
"lng": -109.99995589256287
]
2 Answers
2
If you still want to Serialize the with your JSON you can use like below The below one works for your JSON
JsonConvert.DeserializeObject<List<PointElement>>(testJson);
If you want to create a list of list PointElement as your json descible, then the json converter should convert from List> instead of List:
var testPointList = JsonConvert.DeserializeObject<List<List<PointElement>>>(testNewJson)
Or incase you want to create a single list of PointElement then your json need to brackets which has nested array:
[
"ElementName": "H1 ",
"lat": 51.394319720562514,
"lng": -109.99996662139893
,
"ElementName": "H1 ",
"lat": 51.394319720562514,
"lng": -109.99995589256287
,
"ElementName": "H2 ",
"lat": 51.394319720562514,
"lng": -109.99996662139893
,
"ElementName": "H2 ",
"lat": 51.394319720562514,
"lng": -109.99995589256287
]
and then you can call
var testPointList = JsonConvert.DeserializeObject<List<PointElement>>(testNewJson);
Thanks for contributing an answer to Stack Overflow!
But avoid …
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
But avoid …
To learn more, see our tips on writing great answers.
Required, but never shown
Required, but never shown
By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.
you should edit your JSON string and remove brackets inside it.
– Mojtaba Ahmadi
Sep 2 at 9:13