0

I'm using Newtonsoft JSON.

Newtonsoft.Json.Linq.JArray userLists = Newtonsoft.Json.Linq.JArray.Parse(result)
MessageBox.Show((string)userLists[0]["name"]);

"result" contains:

{"response":"1","0":{"id":"1","username":"bla","name":"bla "},"1":{"id":"2","username":"blub","name":"blub"}}

What's wrong? The messagebox is empty and at index 1 it throws an error. Thanks in advance.

2
  • 2
    userLists is not an array, it's an object with property names "response", "0" and "1" - either treat it like an object (so userLists.0.name) or format the JSON with [ ] to indicate that it's an array. Commented Jul 17, 2016 at 7:19
  • @Keith If I do this MessageBox.Show((string)userLists.0.name); the interpreter shows me errors like "invalid expression", even if I change "JArray" to "JObject". Commented Jul 17, 2016 at 7:29

1 Answer 1

2

Your JSON does not contain an array but rather an object. You can access it via the string index:

var o = JObject.Parse("{'response':'1','0':{'id':'1','username':'bla','name':'bla '},'1':{'id':'2','username':'blub','name':'blub'}}");
MessageBox.Show((string)o["0"]["name"]);
Sign up to request clarification or add additional context in comments.

1 Comment

Oh, that's simple. ^^ Thank you.

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.