本文介绍了使用Newtonsoft json.net在C#中反序列化JSON对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有JSON数组.我想将此映射/反序列化为以下类.

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数组:

[{
    "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);

这很好.

但是在我的情况下,json数组(如Data1Data1000)中大约有1000个数据来自数据库.而且我不想在我的类" DataItem "中定义1000个数据,例如:

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;}
}

是否有任何方法可以使用一些动态类或对象来映射这1000个数据(从Data1到Data1000).

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

我正在考虑使用1000个长度的数组并像这样映射它们,我知道这是行不通的.

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];
}

在我的程序中:

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

PS .我无法更改JSON Array的结构.

P.S. 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
}

所以问题(如您先前所述)是您有一个Json数组.我的方法只是展示热度,以便从数组中读取一个对象.

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.

现在要弄清楚.如果要反序列化数组,建议使用JsonConvert.DeserializeObject<T>()方法反序列化到List<JObject>的输入,然后使用我建议您从上面的代码片段制作的DeserializeFromJson()方法.

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.

结合这些,您可以实现以下目标:

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>();
    }
}

这篇关于使用Newtonsoft json.net在C#中反序列化JSON对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-31 09:07