序列化json字符串c#嵌套对象 [英] Serialize json string c# nested objects

查看:88
本文介绍了序列化json字符串c#嵌套对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

下面是json字符串



below is json string

st1: {
       id: 1,
       name: "Alabama",
       shortname: "AL",
       link: "javascript:window.alert('You can create links to the pages.');",
       comment: "Name Surname<br>Business Development Manager<br>Phone: (000) 123-4567<br>Fax: (000) 123-4567<br>E-mail: name@domainname.com",
       image: "photo.jpg",
       color_map: "#ff5959",
       color_map_over: "#f00001"
   },
   st2: {
       id: 2,
       name: "Alaska",
       shortname: "AK ",
       link: "",
       comment: "",
       image: "",
       color_map: "#7798BA",
       color_map_over: "#366CA3"
   },
   st3: {
       id: 3,
       name: "Arizona",
       shortname: "AZ ",
       link: "",
       comment: "",
       image: "",
       color_map: "#7798BA",
       color_map_over: "#366CA3"
   },





我想用课程或列表或任何其他选项序列化



我尝试了什么:





I want to serialize with class or list or any other option

What I have tried:

class

 public class jsonresultclass
    {

        public jsonresultclass()
        {
            mapdata = new List<mapdata>();
        }
        public List<mapdata> mapdata { get; set; }

    }



    public class mapdata
    {
        public int id { get; set; }
        public string name { get; set; }
        public string shortname { get; set; }
        public string link { get; set; }
        public string comment { get; set; }
        public string image { get; set; }
        public string color_map { get; set; }
        public string color_map_over { get; set; }
    }

call  In

 private List<jsonresultclass> GetUsers()
        {


            var usersList = new List<jsonresultclass>  
            {  
                new jsonresultclass  
                {  
                  
                   mapdata = new   List<mapdata> { 

                       new mapdata {
                            id = 1, 
                            name= "Alabama",
                            shortname= "AL",
                            link= "javascript:window.alert('You can create links to the pages.');",
                            comment= "Name Surname<br>Business Development Manager<br>Phone: (000) 123-4567<br>Fax: (000) 123-4567<br>E-mail: name@domainname.com",
                            image= "photo.jpg",
                            color_map= "#ff5959", 
                            color_map_over= "#f00001",
                       },
                        new mapdata {
                            id= 2,
                            name= "Alaska",
                            shortname= "AK ",
                            link= "",
                            comment= "",
                            image= "",
                            color_map= "#7798BA", 
                            color_map_over= "#366CA3",
                       },
                       new mapdata {
                            id= 3,
                            name= "Arizona",
                            shortname= "AZ ",
                            link= "",
                            comment= "",
                            image= "",
                            color_map= "#7798BA", 
                            color_map_over= "#366CA3",
                       },
                   },                   
                    
                }
               
            };
          
            return usersList;
        }</jsonresultclass></jsonresultclass>

推荐答案

您可以使用Newtonsoft.Json实现此目的。



You can use Newtonsoft.Json to achieve this.

List<jsonresultclass> usersList = GetUsers();

string json = Newtonsoft.Json.JsonConvert.SerializeObject(usersList);





谢谢,



Thanks,


这篇关于序列化json字符串c#嵌套对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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