1

I am using Newtonsoft's JSON Parser.

i obtain json in response. and each time it may be different. Possible variants:
1.

[
       {
        "type": "typing",
           "updates": [
               {
                   "__type": "qwerty"
                }
            ]
        }
] 


2.

[ 
    {
        "token": 1111,
        "type": "msg",
        "updates": [           
            {
                "__type": "asdfg",
                ....
            },
            {
                "__type": "asdfg",
                ....
            },
        ]
    },
    {
        "type": "typing",
        "updates": [
            {
                "__type": "qwerty"
            }
        ]
    }
]

The question is, what structure my object should have to parse any type of json?

var jToken = JToken.Parse(myResponse);
var obj = jToken.ToObject<MyObject>();

class MyObject
{
// what structure should i have here?
}
0

2 Answers 2

2

they are not different objects. Your service returns an object array where each object contains an "updates" array

var myobj =   JsonConvert.DeserializeObject<MyObject[]>(json);

public class MyObject
{
    public string token;
    public string type;
    public Update[] updates;
}
public class Update
{
    public string __type;
}
Sign up to request clarification or add additional context in comments.

Comments

0

I would suggest looking into one of the built in JSON serializors for .NET.

One (The better of the two) is found in System.Runtime.Serialization.Json.DataContractJsonSerializer

The other is in System.Web.Script.Serialization

Here is a detailed description of how to use DataContractJsonSerializer.

Comments

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.