简体   繁体   中英

Deserialize JSON object in c# using Newtonsoft json.net

I have JSON array. I want to map/deserialize this into following class.

public sealed class DataItem{
    public string UserId {get; set;}
    public string TestId {get; set;}
    public string UserName{get; set;}
    public string Data1 {get; set;}
    public string Data2 {get; set;}
    public string Data3 {get; set;}
    public string Data4 {get; set;}
    public string Data5 {get; set;}
    public string Data6 {get; set;}
    public string Data7 {get; set;}
    public string Data8 {get; set;}
    public string Data9 {get; set;}
    public string Data10 {get; set;}
    ...
    ...
}

JSON Array:

[{
    "UserId": "5656",
    "TestId": "562",
    "UserName": "testuser",
    "Data1": "dang",
    "Data2": "phy",
    "Data3": "right",
    "Data4": "left",
    "Data5": "top",
    "Data6": "abc",
    "Data7": "rat",
    "Data8": "test",
    "Data9": "91",
    "Data10": "9090",
     ...
     ...
}, {
    "UserId": "8989",
    "TestId": "12",
    "UserName": "abc",
    "Data1": "111",
    "Data2": "222",
    "Data3": "Pwww",
    "Data4": "aaa",
    "Data5": "bbbb",
    "Data6": "cc",
    "Data7": "ooo",
    "Data8": "hh",
    "Data9": "g",
    "Data10": "5656",
     ...
     ...

}]

In my program I did like this:

IList<DataItem> allItem = JsonConvert.DeserializeObject<IList<DataItem>>(myJsonArray);

This works fine.

But in my case there is about 1000 Datas inside json array like Data1 to Data1000 that comes from database. And I don't want to define 1000 Datas in my class ' DataItem ' like:

public sealed class DataItem{
    ...
    ...
    public string Data1 {get; set;}
    public string Data2 {get; set;}
    .
    .
    .
    public string Data1000 {get; set;}
}

Is there any way to solve this using some dynamic classes or objects to map those 1000 Datas (Data1 to Data1000).

I'm thinking to use a array of 1000 length and map them like this and I know this will not work.

 public sealed class DataItem{
   public string UserId {get; set;}
   public string TestId {get; set;}
   public string[] Data {get; set;}
}

public DataItem(){
    Data = new string[1000];
}

And in my program:

DataItem dataItem = new DataItem();
IList<DataItem> allItem = JsonConvert.DeserializeObject<IList<DataItem>>(myJsonArray);

PS I cannot change the structure of JSON Array.

You can try a bit different method:

JObject obj = JObject.Parse(jsonString); // gets processed json object
DataItem item = new DataItem(); 
item.UserId = obj.SelectToken("UserId").Value<string>();
//.. fill rest of the data
string format = "Data{0}";
for(int i = 0; i < item.Data.Length; i++) // iterate through all Data[x]
{
    item.Data[i] = obj.SelectToken(string.Format(format, i)).Value<string>();
}

And you can pack this into some fancy method like :

public DataItem DeserializeFromJson(string json)
{
    // put that code in here
}

Edit:

So the problem ( as you said previously ) is that you have an Json array. My method was only to show hot to read one object from the array.

Now to make things clear. If you want to deserialize an array I would recommend deserialize input into List<JObject> using JsonConvert.DeserializeObject<T>() method and then use DeserializeFromJson() method that I've suggested you to make from above snippet.

Combining these, you can achieve something like this:

public IEnumerable<DataItem> DeserializeListFromJson(string jsonArray)
{
    return JsonConverter.DeserializeObject<List<JObject>>(jsonArray).Select( obj => DeserializeFromJson(obj) );
}

// updated
public DataItem DeserializeFromJson(JObject obj)
{
    DataItem result = new DataItem();
    item.UserId = obj.SelectToken("UserId").Value<string>();
    //.. fill rest of the data
    string format = "Data{0}";
    for(int i = 0; i < item.Data.Length; i++) // iterate through all Data[x]
    {
        item.Data[i] = obj.SelectToken(string.Format(format, i)).Value<string>();
    }
}

You can use dictionary to store dynamic number of properties coming from JSON string.

Following is an example converting the JSON string, provided in the question, successfully to list of dictionary. Instead of list you can convert to array of dictionary.

var jsonData = "[{\"UserId\":\"5656\",\"TestId\":\"562\",\"UserName\":\"testuser\",\"Data1\":\"dang\",\"Data2\":\"phy\",\"Data3\":\"right\",\"Data4\":\"left\",\"Data5\":\"top\",\"Data6\":\"abc\",\"Data7\":\"rat\",\"Data8\":\"test\",\"Data9\":\"91\",\"Data10\":\"9090\"},{\"UserId\":\"8989\",\"TestId\":\"12\",\"UserName\":\"abc\",\"Data1\":\"111\",\"Data2\":\"222\",\"Data3\":\"Pwww\",\"Data4\":\"aaa\",\"Data5\":\"bbbb\",\"Data6\":\"cc\",\"Data7\":\"ooo\",\"Data8\":\"hh\",\"Data9\":\"g\",\"Data10\":\"5656\"}]";

            var data = JsonConvert.DeserializeObject<List<Dictionary<string, string>>>(jsonData);

            Console.WriteLine("Total : {0}", data.Count);
            Console.WriteLine("Item1 Total : {0}", data[0].Count);
            Console.WriteLine("Item2 Total : {0}", data[1].Count);

            foreach(var item in data)
            {
                Console.WriteLine("--- Propertie start-----");
                foreach(var kvPair in item)
                {
                    Console.WriteLine("property name : {0}", kvPair.Key);
                    Console.WriteLine("property value : {0}", kvPair.Value);
                }
                Console.WriteLine("--- Propertie end-----");
            }

            Console.ReadLine();

This would solve your issue.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM