平时DataTable转换一下就可以生成JSON,像这种里面又嵌套的怎么能生成下面这种格式呢,
{
{name:"中国", province:[ { name:"黑龙江", cities:{ city:["哈尔滨","大庆"] },
{name:"广东", cities:{ city:["广州","深圳","珠海"] }
}
能否这样定义:
public class model {
public string name { get; set; }
public List<province> { get; set; }
}
------解决方案--------------------------------------------------------
可以。看代码
protected void Page_Load(object sender, EventArgs e)
{
model m = new model()
{
name = "GD",
Province = new List<string>() { "A", "B" }
};
System.Web.Script.Serialization.JavaScriptSerializer serializer = new System.Web.Script.Serialization.JavaScriptSerializer();
var output = serializer.Serialize(m);
}
public class model
{
public string name { get; set; }
public List<string> Province { get; set; }
}
------解决方案--------------------------------------------------------
楼主那样的显示主要是设置Model的格式,参考
class Program
{
static void Main(string[] args)
{
Model m = new Model();
//城市
List<CityModel> cityList = new List<CityModel>();
CityModel city = new CityModel();
city.city.Add("哈尔滨");
city.city.Add("大庆");
//省
List<Province> proList = new List<Province>();