如何使用jsonconvert.serializeobject生成Json?

时间:2015-10-21 09:25:06

标签: c#-4.0 json.net

我需要生成json,例如关注using ClassC#中的对象。

{
  "title": "Joseph Sagayam",
  "url": "#person-Joseph",
  "description": "DBA",
  "actions": [
    {
      "icon": "fa-user-plus",
      "url": "#add-contact"
    },
    {
      "icon": "fa-comment-o",
      "url": "#message-contact"
    },
    {
      "icon": "fa-birthday-cake",
      "url": "#birthday"
    }
  ]
}

如果我需要添加打击json值

{
  "people": {
    "categoryName": "People",
    "results": [
      {
        "title": "Joseph Sagayam",
        "url": "#person-Joseph",
        "description": "DBA",
        "actions": [
          {
            "icon": "fa-user-plus",
            "url": "#add-contact"
          },
          {
            "icon": "fa-comment-o",
            "url": "#message-contact"
          },
          {
            "icon": "fa-birthday-cake",
            "url": "#birthday"
          }
        ]
      }
    ]
  }
}

该代码该怎么做?

请告诉我如何做到这一点。

2 个答案:

答案 0 :(得分:3)

尝试以下代码:

<强>类别:

public class Action
{
    public string icon { get; set; }
    public string url { get; set; }
}

public class YourModelClass
{
    public string title { get; set; }
    public string url { get; set; }
    public string description { get; set; }
    public List<Action> actions { get; set; }
}

<强>代码:

var collection = new List<YourModelClass>();
dynamic collectionWrapper = new { myRoot = collection };
var output = JsonConvert.SerializeObject(collectionWrapper);

代码编辑:

var collection = new List<YourModelClass>();

List<Action> newAction = new List<Action>();
newAction.Add(new Action() { icon = "fa-user-plus", url = "#add-contact" });
newAction.Add(new Action() { icon = "fa-comment-o", url = "#message-contact" });
newAction.Add(new Action() { icon = "fa-birthday-cake", url = "#birthday" });

dynamic collectionWrapper = new
{
    myRoot = new YourModelClass()
    {
        title = "Joseph Sagayam",
        url = "#person-Joseph",
        description = "DBA",
        actions = newAction.ToList()
    }
};
var output = JsonConvert.SerializeObject(collectionWrapper);

代码更新:

var collection = new List<YourModelClass>();

dynamic collectionWrapper = new
{
    myRoot = new YourModelClass()
    {
        title = "Joseph Sagayam",
        url = "#person-Joseph",
        description = "DBA",
        actions = new List<Action> 
        { 
            new Action() { icon = "fa-user-plus", url = "#add-contact" },
            new Action() { icon = "fa-comment-o", url = "#message-contact" },
            new Action() { icon = "fa-birthday-cake", url = "#birthday" }
        }
    }
};
var output = JsonConvert.SerializeObject(collectionWrapper);

<强>输出:

然后你会得到如下所示的输出

{
  "title": "Joseph Sagayam",
  "url": "#person-Joseph",
  "description": "DBA",
  "actions": [
    {
      "icon": "fa-user-plus",
      "url": "#add-contact"
    },
    {
      "icon": "fa-comment-o",
      "url": "#message-contact"
    },
    {
      "icon": "fa-birthday-cake",
      "url": "#birthday"
    }
  ]
}

上次更新:

<强>类别:

public class Action
{
    public string icon { get; set; }
    public string url { get; set; }
}

public class Results
{
    public string title { get; set; }
    public string url { get; set; }
    public string description { get; set; }
    public List<Action> actions { get; set; }
}

public class YourModelClass
{
    public string categoryName { get; set; }
    public List<Results> results { get; set; }
}   

<强>代码:

var collection = new List<YourModelClass>();

dynamic collectionWrapper = new
{
    people = new YourModelClass()
    {
        categoryName = "People",
        results = new List<Results> 
        { 
            new Results() 
            { 
                title = "Joseph Sagayam", 
                url = "#person-Joseph", 
                description = "DBA",
                actions = new List<Action> 
                {
                    new Action() { icon = "fa-user-plus", url = "#add-contact" },
                    new Action() { icon = "fa-comment-o", url = "#message-contact" },
                    new Action() { icon = "fa-birthday-cake", url = "#birthday" }
                }
            },
            new Results() 
            { 
                title = "Joseph Sagayam", 
                url = "#person-Joseph", 
                description = "DBA",
                actions = new List<Action> 
                {
                    new Action() { icon = "fa-user-plus", url = "#add-contact" },
                    new Action() { icon = "fa-comment-o", url = "#message-contact" },
                    new Action() { icon = "fa-birthday-cake", url = "#birthday" }
                }
            },
            new Results() 
            { 
                title = "Joseph Sagayam", 
                url = "#person-Joseph", 
                description = "DBA",
                actions = new List<Action> 
                {
                    new Action() { icon = "fa-user-plus", url = "#add-contact" },
                    new Action() { icon = "fa-comment-o", url = "#message-contact" },
                    new Action() { icon = "fa-birthday-cake", url = "#birthday" }
                }
            }
        }
    }
};
var output = JsonConvert.SerializeObject(collectionWrapper);

<强>输出:

然后你会得到如下所示的输出

{
  "people": {
    "categoryName": "People",
    "results": [
      {
        "title": "Joseph Sagayam",
        "url": "#person-Joseph",
        "description": "DBA",
        "actions": [
          {
            "icon": "fa-user-plus",
            "url": "#add-contact"
          },
          {
            "icon": "fa-comment-o",
            "url": "#message-contact"
          },
          {
            "icon": "fa-birthday-cake",
            "url": "#birthday"
          }
        ]
      },
      {
        "title": "Joseph Sagayam",
        "url": "#person-Joseph",
        "description": "DBA",
        "actions": [
          {
            "icon": "fa-user-plus",
            "url": "#add-contact"
          },
          {
            "icon": "fa-comment-o",
            "url": "#message-contact"
          },
          {
            "icon": "fa-birthday-cake",
            "url": "#birthday"
          }
        ]
      },
      {
        "title": "Joseph Sagayam",
        "url": "#person-Joseph",
        "description": "DBA",
        "actions": [
          {
            "icon": "fa-user-plus",
            "url": "#add-contact"
          },
          {
            "icon": "fa-comment-o",
            "url": "#message-contact"
          },
          {
            "icon": "fa-birthday-cake",
            "url": "#birthday"
          }
        ]
      }
    ]
  }
}

希望这会对你有所帮助。

答案 1 :(得分:0)

你需要的一切都应该在这里 http://www.newtonsoft.com/json

Product product = new Product();
product.Name = "Apple";
product.Expiry = new DateTime(2008, 12, 28);
product.Sizes = new string[] { "Small" };

string json = JsonConvert.SerializeObject(product);
// {
//   "Name": "Apple",
//   "Expiry": "2008-12-28T00:00:00",
//   "Sizes": [
//     "Small"
//   ]
// }