C#对象到JSON的转换 [英] C# Object to JSON convert
本文介绍了C#对象到JSON的转换的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想在JSON中获取下一个结构:
I would like to get next structure in JSON:
{
'for-sale': { name: 'For Sale', type: 'folder' },
'vehicles': { name: 'Vehicles', type: 'folder' },
'rentals': { name: 'Rentals', type: 'folder' },
}
我可以使用JavaScriptSerializer
将.net类转换为JSON格式.但是我班上的巫婆结构必须具有上述示例数据?
I can use JavaScriptSerializer
to convert .net class to JSON format. But witch structure my class must have for above sample data?
推荐答案
您是否尝试过:
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,而应使用以下类:
You could also use the DataContractJsonSerializer and instead you would use the following classes:
[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; }
}
这篇关于C#对象到JSON的转换的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文