如何创建json feed

时间:2014-03-07 07:21:09

标签: c# json

任何人都解释我如何开发以下类型的json。  Json feed包含具有数据库内容的父节点和子节点。

{
    "contacts": [
        {
                "id": "c200",
                "name": "Ravi Tamada",
                "email": "ravi@gmail.com",
                "address": "xx-xx-xxxx,x - street, x - country",
                "gender" : "male",
                "phone": {
                    "mobile": "+91 0000000000",
                    "home": "00 000000",
                    "office": "00 000000"
                }
        },
        {
                "id": "c201",
                "name": "Johnny Depp",
                "email": "johnny_depp@gmail.com",
                "address": "xx-xx-xxxx,x - street, x - country",
                "gender" : "male",
                "phone": {
                    "mobile": "+91 0000000000",
                    "home": "00 000000",
                    "office": "00 000000"
                }
        },


  ]
}

1 个答案:

答案 0 :(得分:3)

转到this site并粘贴您的json。它将为您创建以下类

public class Phone
{
    public string mobile { get; set; }
    public string home { get; set; }
    public string office { get; set; }
}

public class Contact
{
    public string id { get; set; }
    public string name { get; set; }
    public string email { get; set; }
    public string address { get; set; }
    public string gender { get; set; }
    public Phone phone { get; set; }
}

public class RootObject
{
    public List<Contact> contacts { get; set; }
}

创建 RootObject ,填充属性,然后将其序列化。

就是这样。

var root = new RootObject();
//fill the properties
string json = JsonConvert.SerializeObject(root);