Click here to Skip to main content
15,896,526 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
C#
public class country {
    public int id {get;set;}
    public string name {get;set;}
    public int Lats {get;set;}
    public int Longs {get;set;}
    public List<states> states {get;set;}
}

public class states {
    public int id {get;set;}
    public string name {get;set;}
    public int Lats {get;set;}
    public int Longs {get;set;}
    public id countryid {get;set;}
    public List<citys> citys {get;set;}
}


public class citys {
    public int id {get;set;}
    public string name {get;set;}
    public id countryid {get;set;}
    public id stateid {get;set;}
}


//-------------

//Data Access

public DataTable getCountry(){
    DataTable dt = new DataTable();
    _conn = new Connetion();
    using(_cmd = new SqlCommand()){
        ------param------------
        using(SqlDataAdepter da = new SqlDataAdepter(_cmd)){
            da.Fill(dt)
        }
    }
    return dt;
}


public DataTable getStates(){
    DataTable dt = new DataTable();
    _conn = new Connetion();
    using(_cmd = new SqlCommand()){
        ------param------------
        using(SqlDataAdepter da = new SqlDataAdepter(_cmd)){
            da.Fill(dt)
        }
    }
    return dt;
}


public DataTable getCity(){
    DataTable dt = new DataTable();
    _conn = new Connetion();
    using(_cmd = new SqlCommand()){
        ------param------------
        using(SqlDataAdepter da = new SqlDataAdepter(_cmd)){
            da.Fill(dt)
        }
    }
    return dt;
}

//-------------

//Method

public void AllLists() {
    DataTable dt = new DataTable();
    country c = new country();
    dt = getCountry();
    country = (from DataRow dr in dt.Rows
        select new country(){
            countryid = dr["id"],
            states = ------how to add a states----,
            states.citys = ------------how to add citys-----
        });
}


below is format how i want the Data
in json format

Java
country: {
    id : "",
    name : "",
    Lats : "",
    Longs : "",
    states : {
        id : "",
        name : "",
        Lats : "",
        Longs : "",
        citys : {
            id : "",
            name ; "",
            countryid : "",
            stateid : ""
        }
    },
country: {
    id : "",
    name : "",
    Lats : "",
    Longs : "",
    states : {
        id : "",
        name : "",
        Lats : "",
        Longs : "",
        citys : {
            id : "",
            name ; "",
            countryid : "",
            stateid : ""
        }
    }
}

}


What I have tried:

C#
public class country {
    public int id {get;set;}
    public string name {get;set;}
    public int Lats {get;set;}
    public int Longs {get;set;}
    public List<states> states {get;set;}
}

public class states {
    public int id {get;set;}
    public string name {get;set;}
    public int Lats {get;set;}
    public int Longs {get;set;}
    public id countryid {get;set;}
    public List<citys> citys {get;set;}
}


public class citys {
    public int id {get;set;}
    public string name {get;set;}
    public id countryid {get;set;}
    public id stateid {get;set;}
}


//-------------

//Data Access

public DataTable getCountry(){
    DataTable dt = new DataTable();
    _conn = new Connetion();
    using(_cmd = new SqlCommand()){
        ------param------------
        using(SqlDataAdepter da = new SqlDataAdepter(_cmd)){
            da.Fill(dt)
        }
    }
    return dt;
}


public DataTable getStates(){
    DataTable dt = new DataTable();
    _conn = new Connetion();
    using(_cmd = new SqlCommand()){
        ------param------------
        using(SqlDataAdepter da = new SqlDataAdepter(_cmd)){
            da.Fill(dt)
        }
    }
    return dt;
}


public DataTable getCity(){
    DataTable dt = new DataTable();
    _conn = new Connetion();
    using(_cmd = new SqlCommand()){
        ------param------------
        using(SqlDataAdepter da = new SqlDataAdepter(_cmd)){
            da.Fill(dt)
        }
    }
    return dt;
}

//-------------

//Method

public void AllLists() {
    DataTable dt = new DataTable();
    country c = new country();
    dt = getCountry();
    country = (from DataRow dr in dt.Rows
        select new country(){
            countryid = dr["id"],
            states = ------how to add a states----,
            states.citys = ------------how to add citys-----
        });
}


below is format how i want the Data
in json format

country: {
id : "",
name : "",
Lats : "",
Longs : "",
states : {
id : "",
name : "",
Lats : "",
Longs : "",
citys : {
id : "",
name ; "",
countryid : "",
stateid : ""
}
}
}
Posted
Updated 9-Oct-16 6:11am
v4
Comments
Patrice T 8-Oct-16 23:59pm    
What is "multi level property" ?
nik varma 9-Oct-16 0:03am    
inside the class the their are two list, in which i want to bond the data, as shown in the property nested list, please check the output that i want

please help if you know

1 solution

So for one, your expected JSON is malformed. Here is an example of your expected output

JavaScript
{ "country": {
    "id" : "",
    "name" : "",
    "Lats" : "",
    "Longs" : "",
    "states" : [{
        "id" : "",
        "name" : "",
        "Lats" : "",
        "Longs" : "",
        "citys" : {
            "id" : "",
            "name" : "",
            "countryid" : "",
            "stateid" : ""
            }
        },{
        "id" : "",
        "name" : "",
        "Lats" : "",
        "Longs" : "",
        "citys" : {
            "id" : "",
            "name" : "",
            "countryid" : "",
            "stateid" : ""
            }
        }]
    },
"country": {
    "id" : "",
    "name" : "",
    "Lats" : "",
    "Longs" : "",
    "states" : [{
        "id" : "",
        "name" : "",
        "Lats" : "",
        "Longs" : "",
        "citys" : {
            "id" : "",
            "name" : "",
            "countryid" : "",
            "stateid" : ""
            }
        },{
        "id" : "",
        "name" : "",
        "Lats" : "",
        "Longs" : "",
        "citys" : {
            "id" : "",
            "name" : "",
            "countryid" : "",
            "stateid" : ""
            }
        }]
    }
}


Then your class structure needs to be as follows

C#
public class Citys
{
    public string id { get; set; }
    public string name { get; set; }
    public string countryid { get; set; }
    public string stateid { get; set; }
}

public class State
{
    public string id { get; set; }
    public string name { get; set; }
    public string Lats { get; set; }
    public string Longs { get; set; }
    public Citys citys { get; set; }
}

public class Country
{
    public string id { get; set; }
    public string name { get; set; }
    public string Lats { get; set; }
    public string Longs { get; set; }
    public List<state> states { get; set; }
}

public class RootObject
{
    public Country country { get; set; }
}
</state>


I suggest you use JSON.NET library (newtonsoft) to serialize/deserialize your json.

For more info on JSON.net see this link: Json.NET - Newtonsoft[^]

From there you can just use the C# classes, assign values to whatever properties you want and when you are ready to convert that to JSON you just use
C#
string json = JsonConvert.SerializeObject(model);
 
Share this answer
 
Comments
nik varma 9-Oct-16 12:24pm    
Thanks David, please can you tell me how can i bind a data from database as i have mentioned the same in above how i am trying now.. Is this correct way, what i am trying?
David_Wimbley 9-Oct-16 13:46pm    
You can get your data from the database however youd like. There are a ton of ways to do so, whatever manner that meet your needs is the way you should go. I For example, I prefer entity framework.

All you have to do is start assigning values to the properties of the classes by instantiating your classes and assign values as normal. Short of showing you how to say var obj = new Object(); obj.Name = "David"; I'm not sure what else you want to know.

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