Json.Net使用索引作为名称反序列化JSON对象 [英] Json.Net deserialize JSON objects with index as name

查看:92
本文介绍了Json.Net使用索引作为名称反序列化JSON对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am attempting to parse JSON from a web service using Json.NET, the web service returns data in the following format:

{
    "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生成一个类:

I used json2sharp to generate a class from the 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; }
}

然后我清理了课程,剩下以下内容:

I then cleaned up the class and was left with the following:

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

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

When I attempt to load the data into my singleton for use in the app:

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

我发现这些文章没有反序列化.

I find that the Articles do not get deserialized.

我已经验证了JSON数据是从Web服务返回的.我认为该问题存在于我的JSON Feed的结构中,其中从Feed中返回的每个项目都被赋予了一个名称,该名称等于该项目所返回的索引.

I've verified that the JSON data is returned from the web service. I believe the issue exists in the structure of my JSON feed, where each item returned from the feed is given a name which equals the index the item is being returned as.

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

I am new to using Json.NET so I'm not sure how I should proceed; I cannot change the structure of the JSON feed but need to consume it's data. Anyone have any recommendations?

推荐答案

您不需要FeaturedArticles类,您可以像这样将JSON反序列化为Dictionary<string, Articles>:

You don't need FeaturedArticles class, you can deserialize the JSON into a Dictionary<string, Articles> like this:

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

这篇关于Json.Net使用索引作为名称反序列化JSON对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆