我有JSON数组。我想将其映射/反序列化为以下类。
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 数组:
[{
"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",
...
...
}]
在我的程序中,我这样做了:
IList<DataItem> allItem = JsonConvert.DeserializeObject<IList<DataItem>>(myJsonArray);
这工作正常。
但就我而言,json 数组中大约有 1000 个数据,例如来自数据库的Data1
到Data1000
。而且我不想在我的类"DataItem"中定义 1000 个数据,例如:
public sealed class DataItem{
...
...
public string Data1 {get; set;}
public string Data2 {get; set;}
.
.
.
public string Data1000 {get; set;}
}
有没有办法解决这个问题,使用一些动态类或对象来映射这 1000 个数据(Data1 到 Data1000)。
我正在考虑使用一个 1000 长度的数组并像这样映射它们,我知道这是行不通的。
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];
}
在我的程序中:
DataItem dataItem = new DataItem();
IList<DataItem> allItem = JsonConvert.DeserializeObject<IList<DataItem>>(myJsonArray);
附言我无法更改 JSON 数组的结构。
您可以尝试一些不同的方法:
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>();
}
你可以把它打包成一些花哨的方法,比如:
public DataItem DeserializeFromJson(string json)
{
// put that code in here
}
编辑:
所以问题(如你之前所说)是你有一个 Json 数组。我的方法只是显示热以从数组中读取一个对象。
现在把事情说清楚。如果你想反序列化数组,我建议使用JsonConvert.DeserializeObject<T>()
方法将输入反序列化为List<JObject>
DeserializeFromJson()
然后使用我建议你从上面的代码片段中制作的方法。
结合这些,您可以实现如下目标:
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>();
}
}
您可以使用字典来存储来自 JSON 字符串的属性的动态数量。
下面是将问题中提供的 JSON 字符串成功转换为字典列表的示例。您可以转换为字典数组,而不是列表。
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();
这将解决您的问题。