如何在C#列表中分配多级属性? [英] How to assign multi level property in C# list?

查看:116
本文介绍了如何在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-----
        });
}





以下是格式我希望以json格式获取数据






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 : ""
        }
    },
country: {
    id : "",
    name : "",
    Lats : "",
    Longs : "",
    states : {
        id : "",
        name : "",
        Lats : "",
        Longs : "",
        citys : {
            id : "",
            name ; "",
            countryid : "",
            stateid : ""
        }
    }
}

}





我的尝试:





What I have tried:

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





以下是格式我希望以json格式获取数据




国家:{

id:,

名称:,

Lats:,

Longs:,

声明:{

id:,

姓名:,

Lats:,

多头:,

城市:{

id:,

名称; ,

countryid:,

stateid:

}

}

}



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 : ""
}
}
}

推荐答案

因此,对于一个,您预期的JSON格式不正确。以下是您预期输出的示例



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

{ "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

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>





我建议您使用JSON.NET库(newtonsoft)来序列化/反序列化您的json。 br />


有关JSON.net的更多信息,请访问此链接: Json.NET - Newtonsoft [ ^ ]



从那里你可以使用C#类,为你想要的任何属性赋值,当你准备将它转换为JSON你只需使用



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

string json = JsonConvert.SerializeObject(model);


这篇关于如何在C#列表中分配多级属性?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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