I’m trying to parse some json using Json.NET. The json either returns an array or an error:
{
"error": "Invalid category"
}
Or:
[
{
"category_id": "23",
"product_name": "Movie1"
},
{
"category_id": "23",
"product_name": "Movie2"
}
]
How would I go about checking if an error exists and showing the message? I have tried using JObject.Parse but this throws an exception of “Current JsonReader item is not an object” which makes sense as it returns an array when there is no error.
In your case I would put the correct parsing in a try catch and the error parsing in the catch block like this:
So if it fails to parse the array list it will try to parse the string as an abject (don’t know how to call the JObject methods but it will probably something like
JObject.Parse<MyObject>("string")).