【问题标题】:Json.Net deserialize JSON objects with index as name [duplicate]Json.Net以索引为名称反序列化JSON对象[重复]
【发布时间】:2014-02-23 14:41:58
【问题描述】:

我正在尝试使用 Json.NET 从 Web 服务解析 JSON,该 Web 服务以以下格式返回数据:

{
    "0": {
        "ID": 193,
        "Title": "Title 193",
        "Description": "Description 193",
        "Order": 5,
        "Hyperlink": "http://someurl.com"
    },
    "1": {
        "ID": 228,
        "Title": "Title 228",
        "Description": "Description 228",
        "Order": 4,
        "Hyperlink": "http://someurl.com"
    },
    "2": {
        "ID": 234,
        "Title": "Title 234",
        "Description": "Description 234",
        "Order": 3,
        "Hyperlink": "http://someurl.com"
    }
}

我使用json2sharp 从 JSON 生成一个类:

public class __invalid_type__0
{
    public int ID { get; set; }
    public string Title { get; set; }
    public string Description { get; set; }
    public int Order { get; set; }
    public string Hyperlink { get; set; }
}

public class __invalid_type__1
{
    public int ID { get; set; }
    public string Title { get; set; }
    public string Description { get; set; }
    public int Order { get; set; }
    public string Hyperlink { get; set; }
}

public class __invalid_type__2
{
    public int ID { get; set; }
    public string Title { get; set; }
    public string Description { get; set; }
    public int Order { get; set; }
    public string Hyperlink { get; set; }
}

public class RootObject
{
    public __invalid_type__0 __invalid_name__0 { get; set; }
    public __invalid_type__1 __invalid_name__1 { get; set; }
    public __invalid_type__2 __invalid_name__2 { get; set; }
}

然后我清理了班级并留下了以下内容:

public class Articles
{
    public int ID { get; set; }
    public string Title { get; set; }
    public string Description { get; set; }
    public int Order { get; set; }
    public string Hyperlink { get; set; }
}

public class FeaturedArticles
{
    public List<Articles> articles { get; set; }
}

当我尝试将数据加载到我的单例中以在应用程序中使用时:

    private void fetchFeaturedArticles()
    {
        var client = new RestClient (_featuredArticlesJsonUrl);
        var request = new RestRequest (Method.GET);
        var response = client.Execute (request);
        _featuredArticles = JsonConvert.DeserializeObject<FeaturedArticles> (response.Content);

        foreach (Articles a in _featuredArticles.Articles)
            Console.WriteLine (a.Title);
    }

我发现文章没有反序列化。

我已验证 JSON 数据是从 Web 服务返回的。我认为问题存在于我的 JSON 提要的结构中,其中从提要返回的每个项目的名称都与返回项目的索引相同。

我是使用 Json.NET 的新手,所以我不确定应该如何进行;我无法更改 JSON 提要的结构,但需要使用它的数据。有人有什么建议吗?

【问题讨论】:

  • 尝试使用 Dictionary 而不是 List
  • @jason Dictionary&lt;int,Article&gt; 似乎更合适

标签: c# json json.net xamarin


【解决方案1】:

您不需要 FeaturedArticles 类,您可以像这样将 JSON 反序列化为 Dictionary&lt;string, Articles&gt;

private void fetchFeaturedArticles()
{
    var client = new RestClient (_featuredArticlesJsonUrl);
    var request = new RestRequest (Method.GET);
    var response = client.Execute (request);

    Dictionary<string, Articles> _featuredArticles = JsonConvert.DeserializeObject<Dictionary<string, Articles>>(response.Content);

    foreach (string key in _featuredArticles.Keys)
    {
        Console.WriteLine(_featuredArticles[key].Title);
    }

}

演示:https://dotnetfiddle.net/ZE1BMl

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-06-28
    • 2015-12-05
    • 1970-01-01
    • 2014-08-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多