40

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?

2 Answers 2

86

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();
Sign up to request clarification or add additional context in comments.

3 Comments

what if my object was just a single string? JObject.parse fails for the same reason and unfortunately for me, there is no JString.parse...
just answered my own question. JRaw.Parse seems to work with a string
how can I get all indexes in my web service json response ? I mean I don't wanna get item that index 0.I wanna get whole json list response to my list how can do that ?My Code is here
-1

To overcome the error plz serialize the jsonstring in below format.this serialize string we are able parse as Jobject

Newtonsoft.Json.JsonConvert.SerializeObject(new {JsonString})

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.