反序列化奇数 JSON 字典



我一生都无法弄清楚如何让这个 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"
}

您可以使用自定义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

相关内容

  • 没有找到相关文章

最新更新