Click here to Skip to main content
15,900,461 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
below is json string

Java
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:

C#
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>
Posted
Updated 5-Mar-16 22:27pm
v2
Comments
Dhruvin bhatt 17-Mar-16 13:56pm    
i want to structure of class or list and how to fill data.

1 solution

You can use Newtonsoft.Json to achieve this.

C#
List<jsonresultclass> usersList = GetUsers();

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


Thanks,
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900