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);
P.S. I cannot change the structure of JSON Array.
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.
This would solve your issue.
You can try a bit different method:
And you can pack this into some fancy method like :
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>
usingJsonConvert.DeserializeObject<T>()
method and then useDeserializeFromJson()
method that I've suggested you to make from above snippet.Combining these, you can achieve something like this: