反序列化奇怪的JSON字典

时间:2014-04-06 09:01:11

标签: c# json json.net

我不能为我的生活弄清楚如何让这个JSON反序列化为我可以使用并在代码中读取的东西。我创建了一个类,并在每个属性上使用JsonProperty来处理json名称中句点的无效使用。但我无法弄清楚如何创建根对象。带数字的属性是随机的,生活是字典。但是recsindb和recsonpage是静态名称。

{
    "1": {
        "orders.orderid":"538",
        "entity.customerid":"109",
        "entity.entityid":"538",
    },
    "2": {
        "orders.orderid":"536",
        "entity.customerid":"108",
        "entity.entityid":"536",
    },
    "recsindb":"2",
    "recsonpage":"2"
}

1 个答案:

答案 0 :(得分:1)

您可以使用自定义JsonConverter来处理这种情况 像这样定义您的数据类:

[JsonConverter(typeof(CustomConverter))]
public class RootObject
{
    public Dictionary<string, Order> Orders { get; set; }
    public string RecsInDB { get; set; }
    public string RecsOnPage { get; set; }
}

public class Order
{
    [JsonProperty("orders.orderid")]
    public string OrderID { get; set; }

    [JsonProperty("entity.customerid")]
    public string CustomerID { get; set; }

    [JsonProperty("entity.entityid")]
    public string EntityID { get; set; }
}

创建一个自定义转换器来处理根对象,如下所示:

class CustomConverter : JsonConverter
{
    public override bool CanConvert(Type objectType)
    {
        return (objectType == typeof(RootObject));
    }

    public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
    {
        JObject jo = JObject.Load(reader);
        RootObject obj = new RootObject();
        obj.RecsInDB = jo["recsindb"].ToString();
        obj.RecsOnPage = jo["recsonpage"].ToString();
        obj.Orders = new Dictionary<string, Order>();
        foreach (JProperty prop in jo.Properties())
        {
            if (prop.Name != "recsindb" && prop.Name != "recsonpage")
            {
                obj.Orders.Add(prop.Name, prop.Value.ToObject<Order>());
            }
        }
        return obj;
    }

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

然后你可以像这样反序列化:

RootObject obj = JsonConvert.DeserializeObject<RootObject>(json);

现在,您可以像平常一样使用数据类。这是一个演示:

class Program
{
    static void Main(string[] args)
    {
        string json = @"
        {
            ""1"": {
                ""orders.orderid"":""538"",
                ""entity.customerid"":""109"",
                ""entity.entityid"":""538"",
            },
            ""2"": {
                ""orders.orderid"":""536"",
                ""entity.customerid"":""108"",
                ""entity.entityid"":""536"",
            },
            ""recsindb"":""2"",
            ""recsonpage"":""2""
        }";

        RootObject obj = JsonConvert.DeserializeObject<RootObject>(json);

        Console.WriteLine("RecsInDB: " + obj.RecsInDB);
        Console.WriteLine("RecsOnPage: " + obj.RecsOnPage);
        foreach (var kvp in obj.Orders)
        {
            Console.WriteLine("Order #" + kvp.Key);
            Console.WriteLine("   OrderID " + kvp.Value.OrderID);
            Console.WriteLine("   CustomerID " + kvp.Value.CustomerID);
            Console.WriteLine("   EntityID " + kvp.Value.EntityID);
        }
    }
}

输出:

RecsInDB: 2
RecsOnPage: 2
Order #1
   OrderID 538
   CustomerID 109
   EntityID 538
Order #2
   OrderID 536
   CustomerID 108
   EntityID 536