How can i Generate Json using jsonconvert.serializeobject?

我是研究僧i 提交于 2019-12-11 10:33:52

问题


I need to generate json like follows using Class and Objects in C#.

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

and also if i need to add blow json value

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

what should do to this code?

Please advise me how to do this.


回答1:


Try below code:

Class:

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; }
}

Code:

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

Code edit:

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);

Code update:

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);

Output:

Then you will get output as per below

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

Last update:

Class:

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; }
}   

Code:

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);

Output:

Then you will get output as per below

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

Hope this will help you.




回答2:


everything you need should be here 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"
//   ]
// }


来源:https://stackoverflow.com/questions/33255519/how-can-i-generate-json-using-jsonconvert-serializeobject

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!