我想在JSON中获得下一个结构:
{
'for-sale': { name: 'For Sale', type: 'folder' },
'vehicles': { name: 'Vehicles', type: 'folder' },
'rentals': { name: 'Rentals', type: 'folder' },
}
我可以使用JavaScriptSerializer
将.net类转换为JSON格式。但是我的班级必须拥有上面的样本数据?
答案 0 :(得分:3)
你试过这个:
public class MyClass
{
[JsonProperty("for-sale")]
public IList<Item> forSale { get; set; }
public IList<Item> vehicles { get; set; }
public IList<Item> rentals { get; set; }
}
public class Item
{
public string name { get; set; }
public string type { get; set; }
}
您也可以使用DataContractJsonSerializer,而是使用以下类:
[DataContract]
public class MyClass
{
[DataMember(Name = "for-sale")]
public IList<Item> forSale { get; set; }
[DataMember]
public IList<Item> vehicles { get; set; }
[DataMember]
public IList<Item> rentals { get; set; }
}
[DataContract]
public class Item
{
[DataMember]
public string name { get; set; }
[DataMember]
public string type { get; set; }
}
答案 1 :(得分:0)
看起来你需要一个
var theStructure = Dictionary<string, Dictionary<string,string>>()
并向其添加数据,如:
theStructure.Add("for-sale", new Dictionary<string, string>() {("For Sale", "folder")});