user41871
user41871

Reputation:

Deserializing JSON in C# : object vs array

I'm not a C# guy, but I'm writing a web service where I generate JSON data and I'm trying to help the guy writing the C# client parse the JSON.

Here's the situation: I return objects where some properties are objects and others are arrays. The client is generic and doesn't know in advance of parsing which properties and objects and which are arrays.

Is there a way to parse arbitrary JSON (whether array or object) without knowing in advance? For example these don't work

JArray.Parse(...)
JObject.Parse(...)

because they require advance knowledge of the type.

Ideally there's something like

Json.Parse(...)

that spits out an array or an object depending on the JSON.

Upvotes: 1

Views: 239

Answers (1)

L.B
L.B

Reputation: 116118

Using Json.Net:

 dynamic jObj = JsonConvert.DeserializeObject(json);

or

 var jObj = JsonConvert.DeserializeObject(json) as JToken;

or

var jObj = JToken.Parse(json);

Upvotes: 5

Related Questions