如何使用json.net将复杂的分隔符(“Column”/“Data”)json反序列化为c#对象

时间:2022-08-23 09:37:18

I need to parse the following JSON into a List of PackageEntity Object.

我需要将以下JSON解析为PackageEntity对象的List。

Since this json is divided into Column and Data, I am having trouble to do so in an intelligent way.

由于这个json分为Column和Data,我很难以智能的方式这样做。

The JSON looks like:

JSON看起来像:

{
  "COLUMNS": ["NSHIPMENTID", "NSHIPPINGCOMPANYID", "NUSERID", "NWEIGHT", "NHEIGHT"],

  "DATA": [
      [7474, null, 12363, "16", "2"],
      [7593, null, 12363, "64", "7"]
  ]
}

I would like to deserialize it to a list of the following class:

我想将它反序列化为以下类的列表:

public class PackageEntity
{
    public int NSHIPMENTID  { get; set; }
    public string NSHIPPINGCOMPANYID  { get; set; }
    public int NUSERID  { get; set; }
    public decimal NWEIGHT  { get; set; }
    public decimal NHEIGHT { get; set; }
} 

what i did so far:

到目前为止我做了什么:

JObject JsonDe = JObject.Parse(responseString);
                int length = JsonDe.Property("DATA").Value.ToArray().Count();

                List<PackageEntity> _list = new List<PackageEntity>();

                for (int i = 0; i < length; i++)
                {
                    PackageEntity pD = new PackageEntity();
                    pD.NSHIPMENTID = JsonDe.Property("DATA").Value.ToArray()[i][0].ToString();
                    pD.NSHIPPINGCOMPANYID = JsonDe.Property("DATA").Value.ToArray()[i][1].ToString();
                    pD.NUSERID = JsonDe.Property("DATA").Value.ToArray()[i][2].ToString();
                    pD.NWEIGHT = JsonDe.Property("DATA").Value.ToArray()[i][3].ToString();
                    pD.NHEIGHT = JsonDe.Property("DATA").Value.ToArray()[i][4].ToString();
                    _list.Add(pD);
                }

1 个解决方案

#1


1  

You can use the following generic custom JsonConverter to deserialize your data:

您可以使用以下通用自定义JsonConverter来反序列化您的数据:

public class ColumnarDataToListConverter<T> : JsonConverter
{
    public override bool CanConvert(Type objectType)
    {
        return objectType == typeof(List<T>);
    }

    public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
    {
        if (reader.TokenType == JsonToken.Null)
            return null;
        var list = existingValue as List<T> ?? new List<T>();
        var obj = JObject.Load(reader);
        var columns = obj["COLUMNS"] as JArray;
        var data = obj["DATA"] as JArray;
        if (data == null)
            return list;
        list.AddRange(data
            .Select(item => new JObject(columns.Zip(item, (c, v) => new JProperty((string)c, v))))
            .Select(o => o.ToObject<T>(serializer)));
        return list;
    }

    public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
    {
        throw new NotImplementedException();
    }
}

Then use it as follows:

然后使用如下:

var settings = new JsonSerializerSettings { Converters = new[] { new ColumnarDataToListConverter<PackageEntity>() } };
var list = JsonConvert.DeserializeObject<List<PackageEntity>>(responseString, settings);

Note the use of Enumerable.Zip() to pair up the entries in the column array with the entries in each row of the data array into a temporary JObject for deserialization.

请注意,使用Enumerable.Zip()将列数组中的条目与数据数组的每一行中的条目配对成一个临时JObject以进行反序列化。

#1


1  

You can use the following generic custom JsonConverter to deserialize your data:

您可以使用以下通用自定义JsonConverter来反序列化您的数据:

public class ColumnarDataToListConverter<T> : JsonConverter
{
    public override bool CanConvert(Type objectType)
    {
        return objectType == typeof(List<T>);
    }

    public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
    {
        if (reader.TokenType == JsonToken.Null)
            return null;
        var list = existingValue as List<T> ?? new List<T>();
        var obj = JObject.Load(reader);
        var columns = obj["COLUMNS"] as JArray;
        var data = obj["DATA"] as JArray;
        if (data == null)
            return list;
        list.AddRange(data
            .Select(item => new JObject(columns.Zip(item, (c, v) => new JProperty((string)c, v))))
            .Select(o => o.ToObject<T>(serializer)));
        return list;
    }

    public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
    {
        throw new NotImplementedException();
    }
}

Then use it as follows:

然后使用如下:

var settings = new JsonSerializerSettings { Converters = new[] { new ColumnarDataToListConverter<PackageEntity>() } };
var list = JsonConvert.DeserializeObject<List<PackageEntity>>(responseString, settings);

Note the use of Enumerable.Zip() to pair up the entries in the column array with the entries in each row of the data array into a temporary JObject for deserialization.

请注意,使用Enumerable.Zip()将列数组中的条目与数据数组的每一行中的条目配对成一个临时JObject以进行反序列化。