Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
88 views
in Technique[技术] by (71.8m points)

JSON.Net error reading

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?

question from:https://stackoverflow.com/questions/18688261/json-net-error-reading

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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();

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...