Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JSON.Net error reading

Tags:

json.net

I'm trying to parse some JSON data with Json.Net. Here is my data:

[     {         "UIDClan": "1",         "UIDKnjiga": "1",         "Naslov": "Title1",         "DatumZaKada": "2013-08-09 00:00:00",         "DatumIstekRez": null,         "Spremno": "0"     },     {         "UIDClan": "1",         "UIDKnjiga": "2",         "Naslov": "Title2",         "DatumZaKada": "2013-08-08 00:00:00",         "DatumIstekRez": null,         "Spremno": "0"     },     {         "UIDClan": "1",         "UIDKnjiga": "3",         "Naslov": "Title3",         "DatumZaKada": "2013-08-09 00:00:00",         "DatumIstekRez": "2013-10-09 00:00:00",         "Spremno": "1"     } ] 

With this piece of code i want to extract UIDClan data:

 JObject o = JObject.Parse(s);   Console.WriteLine(o["UIDClan"]); 

The error is

Error reading JObject from JsonReader. Current JsonReader item is not an object: StartArray. Path '', line 1, position 1.

I've checked with JSONLint and it's valid.

The examples that I found doesn't start with [.

Am I doing something wrong?

like image 273
Josef Avatar asked Sep 08 '13 20:09

Josef


1 Answers

You could try using a JArray. This JSON data is actually an array.

JArray v = JArray.Parse(s); 

To get the first item.

var firstItem = v[0]["UIDClan"].ToString(); 

You can even use linq

var items = v.Where(x =>  x["UIDClan"].ToString() == "1").ToList(); 
like image 137
scartag Avatar answered Oct 02 '22 10:10

scartag