1

I've this json:

{
     "status": true,
     "Text": "Example"
}

But sometimes this could change, so I need to check if the index Text is available in the response passed, code:

var container = (JContainer)JsonConvert.DeserializeObject(response);
var message = container["Text"];

the problem is that I get this exception on message (if the json doesn't contain the key text):

{"Accessed JArray values with invalid key value: \"Text\". Int32 array index expected."}

How can I avoid this problem?

Sandokan
  • 1,075
  • 3
  • 15
  • 30
  • Duplicate question. See http://stackoverflow.com/questions/135448/how-do-i-check-if-an-object-has-a-property-in-javascript. – Christoph Feb 29 '16 at 15:37
  • To be precise, you need to check if the property exists, and only then access it. – Christoph Feb 29 '16 at 15:38
  • @Christoph yes I need to check if the property exists, you have linked a javascript question not c# – Sandokan Feb 29 '16 at 15:39
  • My bad. Then look at this posting. Cast to JObject and iterate over JObject.Children(). Does that work for you?http://stackoverflow.com/questions/13652983/dynamic-jcontainer-json-net-iterate-over-properties-at-runtime – Christoph Feb 29 '16 at 15:47
  • @Christoph I found the problem. The json returned (who cause the problem) contains multiple lines like this: `{"trace":{"details":{"[date]":"[29-02-2016 17:07:29.773750]","[level]":"[info]","[message]":"[System Done.]"},"context":[[{"ID":"John Dillinger"}]]}}{"trace":{"details":{"[date]":"[29-02-2016 17:07:29.773750]","[level]":"[info]","[message]":"[System Done.]"},"context":[[{"ID":"John Dillinger"}]]}}` infact I saw that if I place only one "trace" of the json the code work's fine, instead with the multiple line I got the error. – Sandokan Feb 29 '16 at 16:42
  • @Sandokan It appears the JSON you've posted is invalid. This could be the source of your problem. – Joseph Woodward Feb 29 '16 at 16:44
  • @JosephWoodward it's not possible, I can parse it correctly. It's a simple array of json – Sandokan Feb 29 '16 at 16:46
  • Not sure if you found the problem but I had similar error on Azure and for me fix was to use https in my post url. I am not sure if it is related as I cant see what you do exactly in your code. you can find my post here http://stackoverflow.com/questions/36664576/set-jarray-values-with-invalid-key-value-version-int32-array-index-expected – Emil Apr 17 '16 at 13:35

1 Answers1

1

What version of NewtonSoft are you using?

The following results in message being null and no exception is thrown.

var res = @"{""status"": true }";

var container = (JContainer)JsonConvert.DeserializeObject(res);
var message = container["Text"];

// message = null

Update:

Following your response, even this doesn't throw the exception you're seeing:

var res = @"{}";

var container = (JContainer)JsonConvert.DeserializeObject(res);
var message = container["Text"];

Having updated my code to reflect yours with the same version I'm still not getting the exception you're seeing. This is what I'm doing:

var res = @"{""trace"":{""details"":{""[date]"":""[29-02-2016 17:07:29.773750]"",""[level]"":""[info]"",""[message]"":""[System Done.]""},""context"":[[{""ID"":""John Dillinger""}]]}}";

var container = (JContainer)JsonConvert.DeserializeObject(res);
var message = container["Text"];

The message variable is still null.

In light of this perhaps try create a simple console application with the above code and see if you get the same exception?

Joseph Woodward
  • 9,191
  • 5
  • 44
  • 63
  • I know it, the code working. But as I said if the json returned is different against this `@"{""status"": true }";` I'll get the exception: `{"Accessed JArray values with invalid key value: \"Text\". Int32 array index expected."}` 'cause the key doesn't exists. It's clear now? – Sandokan Feb 29 '16 at 15:52
  • Can you update your question with code that causing the exception so I can try replicate it? – Joseph Woodward Feb 29 '16 at 15:54
  • Is the same code, just pass another json that doesn't contain the `Text` key – Sandokan Feb 29 '16 at 15:56
  • Oh I found the problem, In the try catch I manage only the `WebException`m but if I replace with `Exception` I can't use some property of web exception- – Sandokan Feb 29 '16 at 15:58
  • What I don't get is why the object all of a sudden is a JArray. I would expect that the Json would have to have [ ] around it to be an array. The Json in the first code block clearly looks like an object to me that should not be deserialized into an object. @Sandokan, are you absolutely sure that in the failure cases you are receiving a Json object and not an array? – Christoph Feb 29 '16 at 16:00
  • See my updated answer. I'm still not getting an exception. The `message` variable is simply set to null. – Joseph Woodward Feb 29 '16 at 16:00
  • @Christoph yes I'm sure now I get this exception: `'System.ArgumentException' in Newtonsoft.Json.dll` on this line: `var message = container["Text"];` and the code stop the execution – Sandokan Feb 29 '16 at 16:03
  • What version of Newtonsoft are you running? – Joseph Woodward Feb 29 '16 at 16:04
  • @JosephWoodward is the 8.0.0.0 – Sandokan Feb 29 '16 at 16:05
  • @Sandokan: Please capture an example of the Json PLAIN TEXT that is causing the exception. – Christoph Feb 29 '16 at 16:08
  • this is the json: `{"trace":{"details":{"[date]":"[29-02-2016 17:07:29.773750]","[level]":"[info]","[message]":"[System Done.]"},"context":[[{"ID":"John Dillinger"}]]}}` – Sandokan Feb 29 '16 at 16:09
  • Just tried the exact json and updated to version 8 and still no exception. See my updated answer. – Joseph Woodward Feb 29 '16 at 16:16
  • @JosephWoodward Please, check my last comment in the topic and also try multiple trace json line. I found the problem maybe. – Sandokan Feb 29 '16 at 16:43