Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/276.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 使用JSON.Net自定义反序列化程序反序列化JSON_C#_Json_Json.net_Deserialization - Fatal编程技术网

C# 使用JSON.Net自定义反序列化程序反序列化JSON

C# 使用JSON.Net自定义反序列化程序反序列化JSON,c#,json,json.net,deserialization,C#,Json,Json.net,Deserialization,我有这样的JSON: { "MobileSiteContents": { "au/en": [ "http://www.url1.com", "http://www.url2.com", ], "cn/zh": [ "http://www.url2643.com", ] } } public class MobileSiteContentsContentSectionItem : ContentSectionIte

我有这样的JSON:

{
  "MobileSiteContents": {
    "au/en": [
      "http://www.url1.com",
      "http://www.url2.com",

    ],
    "cn/zh": [
      "http://www.url2643.com",

    ]
  }
}
public class MobileSiteContentsContentSectionItem : ContentSectionItem
{
    public string[] Urls { get; set; }
}

public abstract class ContentSectionItem
{
    public string Culture { get; set; }
}
我正试图将其反序列化为一个类的
IEnumerable
,如下所示:

{
  "MobileSiteContents": {
    "au/en": [
      "http://www.url1.com",
      "http://www.url2.com",

    ],
    "cn/zh": [
      "http://www.url2643.com",

    ]
  }
}
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;
}
公共IEnumerable解析(字符串json) { var jobject=jobject.Parse(json); var result=新列表(); foreach(jobject.Children()中的变量项) { var culture=item.Path; string[]url=new[]{”“};//=这是我在这里讨论的部分。。。 添加(新的MobileSiteContentsContentSectionItem{Culture=Culture,URL=URL}); } 返回结果; }
我使用Json.Net尝试了这个方法,效果很好

public IEnumerable<MobileSiteContentsContentSectionItem> Parse(string json)
{
     dynamic jobject = Newtonsoft.Json.JsonConvert.DeserializeObject(json);

     var result = new List<MobileSiteContentsContentSectionItem>();

     var urls = new List<string>();
     foreach (var item in jobject.MobileSiteContents)
     {
         var culture = item.Name;
         foreach(var url in item.Value)
            urls.Add(url.Value);

         result.Add(new MobileSiteContentsContentSectionItem { Culture = culture, Urls = urls.ToArray() });
     }

     return result;
}
公共IEnumerable解析(字符串json) { 动态jobject=Newtonsoft.Json.JsonConvert.DeserializeObject(Json); var result=新列表(); var url=新列表(); foreach(jobject.MobileSiteContents中的var项) { var culture=item.Name; foreach(item.Value中的变量url) url.Add(url.Value); 添加(新的MobileSiteContentsContentSectionItem{Culture=Culture,URL=URL.ToArray()}); } 返回结果; }
你走对了方向。以下是您需要进行的更正:

  • 您正在对顶级对象的子对象进行迭代,希望获得位于下一级对象中的数据。您需要导航到
    MobileSiteContents
    属性的值,并迭代该属性的子级
  • 获取
    JObject
    Children()
    时,请使用重载将其强制转换为
    JProperty
    对象;这将使提取所需数据变得更加容易
  • JProperty
    项的
    名称
    中获取
    区域性
  • 要获取
    url
    ,请获取
    JProperty
    项的
    ,并使用
    ToObject()
    将其转换为字符串数组
  • 以下是更正后的代码:

    public IEnumerable<MobileSiteContentsContentSectionItem> Parse(string json)
    {
        var jObject = JObject.Parse(json);
    
        var result = new List<MobileSiteContentsContentSectionItem>();
    
        foreach (var item in jObject["MobileSiteContents"].Children<JProperty>())
        {
            var culture = item.Name;
            string[] urls = item.Value.ToObject<string[]>();
    
            result.Add(new MobileSiteContentsContentSectionItem { Culture = culture, Urls = urls });
        }
    
        return result;
    }
    
    au/en
      http://www.url1.com
      http://www.url2.com
    cn/zh
      http://www.url2643.com