我正在查询OpenLibrary.org图书数据库,但其中的一个奇怪之处在于,当您按ISBN请求图书时,结果将ISBN id作为数据结构的最外层,如下所示:
{"ISBN:0192821474":{"我真正关心的东西"}}
当我生成一个包装器类,得到的方式;这也很复杂,因为包装器类最终被命名为ISBN0192821474。我使用的是"Xamasoft JSON类生成器"。
我需要能够做的是"跳过"最外层的元素,并获得它的文本作为我的响应的实际内容。
最好的方法是什么?我有Newtonsoft。Json和RestSharp,我希望我能以某种方式"走"更深一层的结构,并从那里工作。
例如,在下面的代码中,如果响应,它将很好地工作。内容为子节点的内容。 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);
我确信我可以写一个正则表达式来解析它,但显然这不是"正确"的方式,所以我需要一些指导。
是的,您可以使用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