我有这个JSON定义:
{
"id": 1400,
"types": {
"type one": {
"url": "http://www.example.com",
"desc": "type one desc"
},
"type two": {
"url": "http://www.example.com",
"desc": "type two desc"
}
}
}
我需要创建一个C#类,当序列化生成上面的JSON时。
我遇到的问题是“第一类”和“第二类”。如果我的班级看起来像这样:
public class mytypes{
public int id { get; set; }
public List<mytype> types { get; set; }
}
mytype是:
public class mytype {
public string url { get; set; }
public string desc { get; set; }
}
数据来自数据库,这会生成一个“类型”数组,而不是一个“类型”对象,其中包含描述作为定义的对象(类型一,类型二)。
如何更改我的类以在“类型”内生成“类型一”和“类型二”,而不是数组?
答案 0 :(得分:1)
而不是使用:
public List<mytype> types { get; set; }
您必须使用Dictionary
,因此该属性将为:
public Dictionary<string,mytype> types { get; set; }