【问题标题】:How can I ignore the outermost level of a JSON response?如何忽略 JSON 响应的最外层?
【发布时间】:2016-08-16 00:49:49
【问题描述】:

我正在查询 OpenLibrary.org 图书数据库,但其中一个怪癖是,当您按 ISBN 请求图书时,结果包括 ISBN id 作为数据结构的最外层部分,如下所示:

{"ISBN:0192821474": { "我真正关心的东西" } }

当我生成一个阻碍的包装类时;这也很复杂,因为包装类最终被命名为 ISBN0192821474。为此,我正在使用“Xamasoft JSON 类生成器”。

我需要做的是“跳过”最外层的元素并将其文本作为我响应的实际内容。

最好的方法是什么?我有 Newtonsoft.Json 和 RestSharp,我希望我能以某种方式将结构“走”得更深一层并从那里开始工作。

例如,在下面的代码中,如果 response.Content 是子节点的内容,它会很好地工作。

        var client = new RestClient("http://openlibrary.org");
        var request = new RestRequest("/api/books?bibkeys=ISBN:0192821474&jscmd=data&format=json", Method.GET);
        IRestResponse response = client.Execute(request);
        var content = response.Content; // raw content as string
        var x   = JsonConvert.DeserializeObject<Example.OpenLibrary>(response.Content);

我确信我可以编写一个正则表达式来解析它,但显然这不是“正确”的方法,所以我需要一些指导。

【问题讨论】:

标签: c# json json.net restsharp


【解决方案1】:

是的,您可以使用 Json.Net 的 LINQ-to-JSON API (JObjects) 轻松做到这一点。

我会做一个像这样的可重复使用的辅助方法:

public static T DeserializeAndUnwrap<T>(string json)
{
    JObject jo = JObject.Parse(json);
    return jo.Properties().First().Value.ToObject<T>();
}

这是一个演示:

public class Program
{
    public static void Main(string[] args)
    {
        var client = new RestClient("http://openlibrary.org");
        var request = new RestRequest("/api/books?bibkeys=ISBN:0192821474&jscmd=data&format=json", Method.GET);
        IRestResponse response = client.Execute(request);
        var content = response.Content; // raw content as string

        Book book = DeserializeAndUnwrap<Book>(content);
        Console.WriteLine(book.title);
        if (book.authors.Any()) Console.WriteLine("By " + book.authors.First().name);
        Console.WriteLine(book.number_of_pages + " pages");
        Console.WriteLine("Published " + book.publish_date);
    }

    public static T DeserializeAndUnwrap<T>(string json)
    {
        JObject jo = JObject.Parse(json);
        return jo.Properties().First().Value.ToObject<T>();
    }
}

public class Book
{
    public Publisher[] publishers { get; set; }
    public string pagination { get; set; }
    public Identifiers identifiers { get; set; }
    public Classifications classifications { get; set; }
    public string title { get; set; }
    public string url { get; set; }
    public string notes { get; set; }
    public int number_of_pages { get; set; }
    public Subject[] subjects { get; set; }
    public string publish_date { get; set; }
    public string key { get; set; }
    public Author[] authors { get; set; }
    public string by_statement { get; set; }
    public Publish_Places[] publish_places { get; set; }
}

public class Identifiers
{
    public string[] lccn { get; set; }
    public string[] openlibrary { get; set; }
    public string[] isbn_10 { get; set; }
    public string[] oclc { get; set; }
    public string[] librarything { get; set; }
    public string[] goodreads { get; set; }
}

public class Classifications
{
    public string[] dewey_decimal_class { get; set; }
}

public class Publisher
{
    public string name { get; set; }
}

public class Subject
{
    public string url { get; set; }
    public string name { get; set; }
}

public class Author
{
    public string url { get; set; }
    public string name { get; set; }
}

public class Publish_Places
{
    public string name { get; set; }
}

输出:

The anthropic cosmological principle
By John D. Barrow
706 pages
Published 1996

小提琴:https://dotnetfiddle.net/LGiztM

【讨论】:

    猜你喜欢
    • 2012-02-12
    • 1970-01-01
    • 1970-01-01
    • 2011-12-17
    • 2021-03-02
    • 1970-01-01
    • 2022-11-21
    • 2012-10-13
    • 2013-12-15
    相关资源
    最近更新 更多