简体   繁体   中英

What type of C# object does the following JSON map to?

I want to desalinize the following JSON into a strongly typed C# object:

{"myThings":{"One thing":"aaaaa","Two thing":"bbbbb"}}

One thing and Two thing aren't always called One thing and Two thing , these change every time, and there could be more or less myThings.

I can't figure out what type of object myThings should be, I am thinking a KeyValuePair but am unsure if Json.Net supports deserialising to a key/value pair, or if this is even correct. Can somebody confirm?

Thanks

class MyThings
{
    public string OneThing { get; set; }
    public string TwoThing { get; set; }
}

or

class MyThings
{
    public Dictionary<string, string> Dictionary { get; set; }
}

If the number of "X things" is unknown and variable use the Dictionary<string, string> . If you are always going to have {"One thing":"aaaaa","Two thing":"bbbbb"} use the first class.

EDIT: Given your edit, use the Dictionary<string, string>

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