2016-08-16 2 views
2

OpenLibrary.org 서적 데이터베이스에 쿼리하고 있지만 그 중 하나는 ISBN으로 책을 요청할 때 ISBN ID가 데이터 구조의 가장 바깥 부분으로 포함된다는 것입니다.JSON 응답의 가장 바깥 쪽 레벨은 어떻게 무시합니까?

{ "ISBN : 0192821474": { "실제로 신경 쓰는 물건"} }

나는 길에 들어있는 래퍼 클래스를 생성 할 때; 래퍼 클래스의 이름이 ISBN0192821474이므로 복잡합니다. 나는 "Xamasoft JSON Class Generator"를 사용하고 있습니다.

필자가해야 할 일은 가장 바깥 쪽 요소를 "건너 뛰고"텍스트를 내 응답의 실제 내용으로 가져 오는 것입니다.

어떻게해야할까요? 나는 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); 

구문 분석을 위해 정규식을 작성할 수는 있지만 분명히 올바른 방법은 아닙니다. 따라서 지침이 필요합니다.

+2

다음

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

는 데모입니다 약간의 문서이다. Newtonsoft (http://www.newtonsoft.com/json/help/html/SerializingJSONFragments.htm)에서 작성한 예제에서는이를 수행하는 방법에 대한 명확한 예제를 제공합니다. P – starlight54

답변

0

예, Json.Net의 LINQ-to-JSON API (JObjects)을 사용하면 쉽게이 작업을 수행 할 수 있습니다.

나는이 같은 작은 재사용 가능한 도우미 메서드 만들 것 :

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

관련 문제