9

I have a JSON string in this form:

string jsonStr = "[\"A\", [\"Martini\", \"alovell\"],[\"Martin\", \"lovell\"]]"

I am trying to deserialize the JSON using the C# .NET deserializer DataContractJsonSerializer with the following code snippet

MemoryStream ms = new MemoryStream(Encoding.UTF8.GetBytes(jsonStr));
DataContractJsonSerializer serializer = new DataContractJsonSerializer(typeof<X>);
X data = (X)serializer.ReadObject(ms);

Now since the JSON array is an array of variable types I do not know what type of object X should be

If my String were

jsonStr = "[[\"Martini\", \"alovell\"],[\"Martin\", \"lovell\"]]"

I could use this:

X = List<List<String>> 

and that would work for me. I was wondering if there is any way to deserialize variable type JSON array?

1 Answer 1

8

You could use Json.NET to do this.

JArray a = JArray.Parse(jsonStr);

The JArray would contain either strings or nested JArray's depending on the JSON.

Sign up to request clarification or add additional context in comments.

1 Comment

I use this assembly, and it's fantastic. I highly recommend this.

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.