简体   繁体   中英

Deserialize JSON response using Newtonsoft.Json

I have a web service which return response in JSON format as below.

{"123":{"Name":"Abcd", "Age":"30"},"231":{"Name":"xyz", "Age":"20"}, "543":{"Name":"pqr", "Age":"35"}}

I want to deserialize this response in C# and wants to display it.

How can I do with Newtonsoft.Json library.

Please help me.

I'm going to assume that "123", "231", and "543" are identifiers and not constant property names. In that case what you have is a dictionary of objects. First, define a class that maps to the object.

public class Something
{
    public string Name { get; set; }
    public string Age { get; set; }
}

Then deserialize into a dictionary of those objects.

var whatever = JsonConvert.DeserializeObject<Dictionary<string, Something>>(json);

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