简体   繁体   中英

Deserialize json to c#

How should my C# sharp object look like, I would like to deserialize the following JSON string to a C# object.

{   
   "PersonId": "XXXXXXXXXXXXXX",
   "Name": "XXXXXXXX",  
   "HobbiesCollection":
        {"Hobby":
            [
                {
                    "type": "RUNNING",
                    "id": 44,
                    "description": "sprinting and sprinting?"
                },
                {
                    "type": "RUNNING",
                    "id": 45,
                    "description": "jogging and jogging"
                }
            ]
        }   
}

Here is what is generated in VS2013

public class Rootobject
{
    public string PersonId { get; set; }
    public string Name { get; set; }
    public Hobbiescollection HobbiesCollection { get; set; }
}

public class Hobbiescollection
{
    public Hobby[] Hobby { get; set; }
}

public class Hobby
{
    public string type { get; set; }
    public int id { get; set; }
    public string description { get; set; }
}

You can use VS2012/2013 feature Edit/Paste Special/Paste JSON As Classes to simplify this process.

There is a online tool you can make C# class from your String

JSON to Csharp

public class Hobby
{
    public string type { get; set; }
    public int id { get; set; }
    public string description { get; set; }
}

public class HobbiesCollection
{
    public List<Hobby> Hobby { get; set; }
}

public class RootObject
{
    public string PersonId { get; set; }
    public string Name { get; set; }
    public HobbiesCollection HobbiesCollection { get; set; }
}

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