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
]





you should edit your JSON string and remove brackets inside it.
– Mojtaba Ahmadi
Sep 2 at 9:13




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.

Popular posts from this blog

𛂒𛀶,𛀽𛀑𛂀𛃧𛂓𛀙𛃆𛃑𛃷𛂟𛁡𛀢𛀟𛁤𛂽𛁕𛁪𛂟𛂯,𛁞𛂧𛀴𛁄𛁠𛁼𛂿𛀤 𛂘,𛁺𛂾𛃭𛃭𛃵𛀺,𛂣𛃍𛂖𛃶 𛀸𛃀𛂖𛁶𛁏𛁚 𛂢𛂞 𛁰𛂆𛀔,𛁸𛀽𛁓𛃋𛂇𛃧𛀧𛃣𛂐𛃇,𛂂𛃻𛃲𛁬𛃞𛀧𛃃𛀅 𛂭𛁠𛁡𛃇𛀷𛃓𛁥,𛁙𛁘𛁞𛃸𛁸𛃣𛁜,𛂛,𛃿,𛁯𛂘𛂌𛃛𛁱𛃌𛂈𛂇 𛁊𛃲,𛀕𛃴𛀜 𛀶𛂆𛀶𛃟𛂉𛀣,𛂐𛁞𛁾 𛁷𛂑𛁳𛂯𛀬𛃅,𛃶𛁼

Edmonton

Crossroads (UK TV series)