Json parsing c #

I had 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 it

 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[""]; 
+4
source share
3 answers

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

 JArray sonuc = JArray.Parse(x); 
+7
source

You have many ways to parse your json, for example, you can use dynamic

 dynamic obj1 = JsonConvert.DeserializeObject(json); foreach (var node in obj1.nodes) { Console.WriteLine("{0} {1}", node.id, node.title); } 

or linq

 var obj2 = (JObject)JsonConvert.DeserializeObject(json); var nodes= obj2["nodes"].Children() .Select(node => new { Id= (string)node["id"], Title = (string)node["title"] }) .ToList(); 
+1
source
 JArray sonuc = JArray(x); 

Should do the job for you .. Since x is already in array form

-1
source

Source: https://habr.com/ru/post/1435210/


All Articles