加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

c# – 使用带有JSON.Net的自定义反序列化器反序列化JSON

发布时间:2020-12-15 08:23:35 所属栏目:百科 来源:网络整理
导读:我有这样的 JSON: { "MobileSiteContents": { "au/en": [ "http://www.url1.com","http://www.url2.com",],"cn/zh": [ "http://www.url2643.com",] }} 我正在尝试将其反序列化为IEnumerable类,如下所示: public class MobileSiteContentsContentSectionIte
我有这样的 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; }
}

那可能吗?
我意识到我可能需要为此使用Custom 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;
}

解决方法

你走在正确的轨道上.以下是您需要进行的更正:

>您正在迭代顶层对象的子节点,期望获得实际位于对象中的数据更低一级.您需要导航到MobileSiteContents属性的值并迭代它的子项.
>当您使用JObject的Children()时,使用允许您将它们转换为JProperty对象的重载;这将使您更容易提取所需的数据.
>从JProperty项目的名称中获取文化
>要获取网址,请获取JProperty项的值,并使用ToObject< string []>()将其转换为字符串数组.

这是更正后的代码:

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;
}

如果您喜欢简洁的代码,可以将其减少为“单行”:

public IEnumerable<MobileSiteContentsContentSectionItem> Parse(string json)
{
    return JObject.Parse(json)["MobileSiteContents"]
        .Children<JProperty>()
        .Select(prop => new MobileSiteContentsContentSectionItem
        {
            Culture = prop.Name,Urls = prop.Value.ToObject<string[]>()
        })
        .ToList();
}

演示:

class Program
{
    static void Main(string[] args)
    {
        string json = @"
        {
            ""MobileSiteContents"": {
                ""au/en"": [
                    ""http://www.url1.com"",""http://www.url2.com"",""cn/zh"": [
                    ""http://www.url2643.com"",]
            }
        }";

        foreach (MobileSiteContentsContentSectionItem item in Parse(json))
        {
            Console.WriteLine(item.Culture);
            foreach (string url in item.Urls)
            {
                Console.WriteLine("  " + url);
            }
        }
    }

    public static IEnumerable<MobileSiteContentsContentSectionItem> Parse(string json)
    {
        return JObject.Parse(json)["MobileSiteContents"]
            .Children<JProperty>()
            .Select(prop => new MobileSiteContentsContentSectionItem()
            {
                Culture = prop.Name,Urls = prop.Value.ToObject<string[]>()
            })
            .ToList();
    }

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

    public abstract class ContentSectionItem
    {
        public string Culture { get; set; }
    }
}

输出:

au/en
  http://www.url1.com
  http://www.url2.com
cn/zh
  http://www.url2643.com

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读