简体   繁体   中英

deserializing json with a changable key

I have this JSON:

{
    "data":
        {
            "110714":{"periphery_id":108},
            "110715":{"periphery_id":102},
            "110710":{"periphery_id":107}
        }
}

and classes that describe it:

public class Info
{
    [JsonProperty("periphery_id")]
    public int PeripheryId { get; set; }
}

public class Data
{
    [JsonProperty(//what should be here as i have keys like 110710,110715,110714)]
    public Info Info { get; set; }
}

public class Structure
{
    [JsonProperty("data")]
    public Data Data { get; set; }
}

How can I describe these objects by one class?

"110714":{"periphery_id":108},
"110715":{"periphery_id":102},
"110710":{"periphery_id":107}

I know the values of all of the numeric keys that would be in the JSON because I send them in a server request.

API server is external and can`t modify JSON structure

由于数据中的值具有更改的键,因此您可以使用字典将其映射到,因此,代替具有Info对象的Data,它将具有Dictionary<int, Info> ,然后您可以从中访问值

As Gronex suggested use Dictionary. But to make it work you need to implement custom json converter by inheriting JsonConverter and override ReadJson method.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM