【问题标题】:How can i Generate Json using jsonconvert.serializeobject?如何使用 jsonconvert.serializeobject 生成 Json?
【发布时间】:2015-10-21 09:25:06
【问题描述】:

我需要生成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"
          }
        ]
      }
    ]
  }
}

应该如何处理这段代码?

请告诉我该怎么做。

【问题讨论】:

    标签: c#-4.0 json.net


    【解决方案1】:

    试试下面的代码:

    类:

    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"
              }
            ]
          }
        ]
      }
    }
    

    希望这会对你有所帮助。

    【讨论】:

    • 感谢您的回复。您能告诉我如何添加值吗?
    • 真的很有帮助,也请Up回答。
    • 如果我需要添加更多类似 {"people":{"categoryName": "People","re​​sults":[{ "title": "Joseph Sagayam", "url": " #person-Joseph”、“描述”:“DBA”、“操作”:[ {“图标”:“fa-user-plus”、“url”:“#add-contact”}、{“图标”:“ fa-comment-o", "url": "#message-contact" }, { "icon": "fa-birthday-cake", "url": "#birthday" } ] }]}} 有什么变化我必须做什么?
    • 您能告诉我如何进行更改吗?
    • 您的意思是在 json 输出字符串中添加这些值?
    【解决方案2】:

    您需要的一切都应该在这里 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"
    //   ]
    // }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-02-23
      • 2016-01-17
      • 2011-01-28
      • 2010-11-28
      • 1970-01-01
      相关资源
      最近更新 更多