【发布时间】:2013-10-17 16:28:00
【问题描述】:
我的 JSON 看起来像这样:
{
"MobileSiteContents": {
"au/en": [
"http://www.url1.com",
"http://www.url2.com",
],
"cn/zh": [
"http://www.url2643.com",
]
}
}
我正在尝试将其反序列化为 IEnumerable 的类,如下所示:
public class MobileSiteContentsContentSectionItem : ContentSectionItem
{
public string[] Urls { get; set; }
}
public abstract class ContentSectionItem
{
public string Culture { get; set; }
}
这可能吗?
我意识到我可能需要为此使用自定义 JsonConverter,但找不到任何示例。
我开始编写使用JObject.Parse 进行转换的方法,但不确定这是否是正确/最有效的方法:
public IEnumerable<MobileSiteContentsContentSectionItem> Parse(string json)
{
var jobject = JObject.Parse(json);
var result = new List<MobileSiteContentsContentSectionItem>();
foreach (var item in jobject.Children())
{
var culture = item.Path;
string[] urls = new[] { "" }; //= this is the part I'm having troble with here...
result.Add(new MobileSiteContentsContentSectionItem { Culture = culture, Urls = urls });
}
return result;
}
【问题讨论】:
标签: c# json json.net deserialization