Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Json parsing C#

I had a Json like this

{ "nodes" : [{"id" : "36018","title" : "Fotarı","date" : "20.09.2012 00:45", "short_description" : "Dünrina, rr!","bigimage_width" : "468","bigimage" : "https://qew","croppedimage" : "https://qwe.jpg"},{"id" : "36009","title" : "ey","date" : "20.09.2012 00:03", "short_description" : "İntız!","bigimage_width" : "220","bigimage" : "https://312.jpg","croppedimage" : "https://41172.jpg"},{"id" : "35915","title" : "ai!","date" : "20.09.2012 00:02", "short_description" : "Ssdi...","bigimage_width" : "220","bigimage" : "https://qwe.qwe" : "https://asd.asd"},...

so i did this

JObject j = JObject.Parse(x); // x is downloaded JSon code
JArray sonuc = (JArray)j["nodes"];

but now i have

[{"id":"1","name":"news"},{"id":"2","name":"hardware"},{"id":"3","name":"software"},{"id":"4","name":"\internet"},{"id":"6","name":"tv!"},{"id":"7","name":"texts"},{"id":"8","name":"update"},...

so what should i do with my code to make it work?

JObject j = JObject.Parse(x); // gives JsonReaderException exception here
JArray sonuc = (JArray)j[""];
like image 842
E.Mert Avatar asked Feb 19 '23 02:02

E.Mert


1 Answers

If you have an array in JSON (notice the opening [ and closing ] brackets) you can directly parse it with the JArray.Parse static method:

JArray sonuc = JArray.Parse(x);
like image 112
nemesv Avatar answered Feb 27 '23 11:02

nemesv