简体   繁体   English

将JSON反序列化为C#

[英]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. 我的C#尖锐对象应该是什么样子,我想将以下JSON字符串反序列化为C#对象。

{   
   "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 这是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. 您可以使用VS2012 / 2013功能“ Edit/Paste Special/Paste JSON As Classes来简化此过程。

There is a online tool you can make C# class from your String 有一个在线工具,您可以从您的String中创建C#类

JSON to Csharp JSON转换为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; }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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