将JSON数组反序列化为字典,其中的键与JSON分开提供

时间:2016-06-08 08:48:16

标签: c# json dictionary json.net deserialization

我有一个像这样的JSON文件:

{
    "price": ["123.50", "124.6", "126.30"],
    "order": ["23", "30", "20"]
}

我想填充我的对象Product

public class Product {
  public Dictionary<string, Object> priceInfo;
  public Dictionary<string, Object> orderInfo;
}

我在每个JSON对象中都有一个描述,每个值(这里有数组)可能在Product类中,例如:

String[] defPriceInfo = {"price", "avgprice", "maxprice"};

最后,我会使用Product访问priceInfo.TryGetValue("avgprice", ...)对象的这些值,这会将值返回给我

  

124.6

我在Stack Overflow中搜索但是我没有找到类似的问题。 实际上我试图覆盖JsonConverter.ReadJson但它没有工作;问题是个性化的密钥&#34;我想要的。

Edit1 :我对ReadJson()方法有这个但是错了。

public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
        {
            object res = new Object();
            var tokenType = reader.TokenType;
            if (tokenType == JsonToken.StartObject)
            {
                 object obj = serializer.Deserialize(reader);
                 res = new Dictionary<string, string>();
            }
            else if (tokenType == JsonToken.StartArray)
            {
                res = serializer.Deserialize(reader);
            }
            return res;
        }

2 个答案:

答案 0 :(得分:3)

以下是使用自定义JsonConverter作为通用解决方案的一种方法。这个想法是你设置一个public,static,readonly,string数组,其中包含你想要特别处理的每个字典的键,然后用[JsonConverter]属性标记每个字典字段,该属性指定静态字段的位置包含keys数组。然后转换器使用指定的密钥从JSON数组填充字典。 (请注意,如果JSON数组属性名称与类中的字典成员名称不同,那么您还需要[JsonProperty]属性将它们绑定在一起。)

以下是如何设置示例Product类:

public class Product
{
    public static readonly string[] defPriceInfo = { "price", "avgprice", "maxprice" };
    public static readonly string[] defOrderInfo = { "first", "second", "third" };

    [JsonProperty("price")]
    [JsonConverter(typeof(ArrayToDictionaryConverter), typeof(Product), "defPriceInfo")]
    public Dictionary<string, object> priceInfo;

    [JsonProperty("order")]
    [JsonConverter(typeof(ArrayToDictionaryConverter), typeof(Product), "defOrderInfo")]
    public Dictionary<string, object> orderInfo;
}

以下是自定义ArrayToDictionaryConverter的代码:

class ArrayToDictionaryConverter : JsonConverter
{
    private string[] keysArray;

    public ArrayToDictionaryConverter(Type containingObjectType, string keysArrayFieldName)
    {
        FieldInfo field = containingObjectType.GetField(keysArrayFieldName);
        if (field == null)
            throw new Exception("Could not find " + keysArrayFieldName + " field on type " + containingObjectType.Name + ".");
        if (!field.Attributes.HasFlag(FieldAttributes.Static) || field.FieldType != typeof(String[]))
            throw new Exception("The " + keysArrayFieldName + " field on " + containingObjectType.Name + " must be declared as static string[].");
        keysArray = (string[])field.GetValue(null);
    }

    public override bool CanConvert(Type objectType)
    {
        return (objectType == typeof(Dictionary<string, object>));
    }

    public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
    {
        JArray array = JArray.Load(reader);
        Dictionary<string, object> dict = new Dictionary<string, object>();

        for (int i = 0; i < array.Count; i++)
        {
            string key = i < keysArray.Length ? keysArray[i] : "key" + i;
            dict.Add(key, (string)array[i]);
        }

        return dict;
    }

    public override bool CanWrite
    {
        get { return false; }
    }

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

以下是一个快速演示,展示了它的工作原理:

using System;
using System.Collections.Generic;
using System.Reflection;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;

public class Program
{
    public static void Main()
    {
        string json = @"
            {
                ""price"": [""123.50"", ""124.6"", ""126.30""],
                ""order"": [""23"", ""30"", ""20""]
            }";

        try
        {
            Product prod = JsonConvert.DeserializeObject<Product>(json);
            foreach (var kvp in prod.priceInfo)
            {
                Console.WriteLine(kvp.Key + ": " + kvp.Value);
            }
            foreach (var kvp in prod.orderInfo)
            {
                Console.WriteLine(kvp.Key + ": " + kvp.Value);
            }
        }
        catch (Exception e)
        {
            Console.WriteLine(e.Message);
            if (e.InnerException != null) Console.WriteLine(e.InnerException.Message);
        }
    }
}

输出:

price: 123.50
avgprice: 124.6
maxprice: 126.30
first: 23
second: 30
third: 20

答案 1 :(得分:0)

您可以首先将JSON反序列化为POCO,例如使用类似这样的类:

public class TempProduct 
{
  public Price price {get; set;}
  public Order order {get; set;}

  public class Price
  {
    public string price {get; set;}
    public string avgprice {get; set;}
    public string maxprice {get; set;}
  }

  public class Order
  {
    public string orderType1 {get; set;}
    public string orderType2 {get; set;}
    public string orderType3 {get; set;}
  }
}

然后,您可以从中填充您的Product类。

var prod = new Product();

prod.priceInfo = new Dictionary<string,Object>();
prod.priceInfo.Add("price", tempProd.price.price);
prod.priceInfo.Add("avgprice", tempProd.price.avgprice);
prod.priceInfo.Add("maxprice", tempProd.price.maxprice);