Click here to Skip to main content
15,911,896 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I want to convert the json array string to list according to it's key value but i am unable to it.
I am new to json

What I have tried:

public class Userinfo
    {
        public List<Name> name { get; set; }

        public List<Address> lstaddress {get;set;}

    }


    public class Name
    {
        public string Fname;
        public string LName;
    }

    public class Address
    {
        public string address1 { get; set; }
        public string address2 { get; set; }
        public int pincode { get; set; }

    }


public  string  DisplayData()
     {
         try
         {
             Userinfo u = new Userinfo();
             Address a = new Address();
             Name N = new Name();
             List<Name> nn = new List<Name>();
             List<Address> aa = new List<Address>();


             a.address1 = "abc";
             a.address2 = "def";
             a.pincode = 000000;
             aa.Add(a);

             N.Fname = "test";
             N.LName = "data";

             nn.Add(N);
             u.lstaddress = aa;
             u.name = nn;

             string aaa= new JavaScriptSerializer().Serialize(u).ToString();







             return "True";
         }
         catch(Exception ex)
         {
             return "error";
         }
     }


sample String:-

{"name":[{"Fname":"test","LName":"data"}],"lstaddress":[{"address1":"abc","address2":"def","pincode":0}]}
Posted
Updated 1-Feb-19 2:26am

Your string has two faults: "name" is incorrect, it should be "Name" as C# is case sensitive; and "lstaddress" should be "Address" to match the C# class:
{"Name":[{"Fname":"test","LName":"data"}],"Address":[{"address1":"abc","address2":"def","pincode":0}]}
Is correct and should deserialize OK to the C# classes you show.
 
Share this answer
 
I have written an article to answer questions like these: Working with JSON in C# & VB[^] ... It gives you the tools and shows you how to do what you want plus more.
 
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