如何使用jsonconvert.serializeobject生成Json? [英] How can i Generate Json using jsonconvert.serializeobject?

查看:4864
本文介绍了如何使用jsonconvert.serializeobject生成Json?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要生成 json 如下使用类和对象 code>。

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

并且如果我需要添加blow json value

  {
people:{
categoryName:People,
结果:[
{
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
}
]
}
]
} $ b}

该代码应该怎么办?



请告诉我该如何操作。

解决方案



  

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

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

代码:
$ b

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

代码编辑


$ b b

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

代码更新


$ b b

  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}
}
} b $ b};
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;组; }
public string url {get;组; }
}

public class结果
{
public string title {get;组; }
public string url {get;组; }
public string description {get;组; }
public List< Action> actions {get;组; }
}

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

代码:
$ b

  var collection = new List< YourModelClass>(); 

dynamic collectionWrapper = new
{
people = new YourModelClass()
{
categoryName =People,
results = new List< ;结果>
{
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}
}
} b $ b}
}
};
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约瑟夫·萨加姆,
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
}
]
}
]
}
}

希望这会帮助你。


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.

解决方案

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.

这篇关于如何使用jsonconvert.serializeobject生成Json?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆