2013-07-21 6 views
0

Я пытаюсь дезериализовать json как объект JsonConvert.DeserializeObject (responseBodyAsText). проблема в том, что я не могу ничего вернуть в ней. как я могу заполнить классы, созданные ниже из json и в каком порядке ?. мне нужно вызвать их по порядку?google book api deserialize jsonconvert return null

   httpClient = new HttpClient(); 

      httpClient.MaxResponseContentBufferSize = 256000; 
      httpClient.DefaultRequestHeaders.Add("user-agent", "Mozilla/5.0 (compatible; MSIE 10.0; Windows NT 6.2; WOW64; Trident/6.0)"); 

      string responseBodyAsText; 


      HttpResponseMessage response = await httpClient.GetAsync("https://www.googleapis.com/books/v1/volumes?q=harry+potter"); 
      response.EnsureSuccessStatusCode(); 


      responseBodyAsText = await response.Content.ReadAsStringAsync(); 
       responseBodyAsText = responseBodyAsText.Replace("<br>", Environment.NewLine); // Insert new lines 
       var jarray = JsonConvert.DeserializeObject<VolumeInfo>(responseBodyAsText); 

это мой Google книги апи JSON вернуться

{ 
"kind": "books#volumes", 
"totalItems": 884, 

       "items": [ 
       { 
        "kind": "books#volume", 
        "id": "jk87_y-ubE0C", 
        "etag": "Bv3tg9bC2Kk", 
        "selfLink": "https://www.googlea", 
          "volumeInfo": { 
           "title": "Harry Potter a", 
           "authors": [ 
             "J.K. Rowling" 
           ], 
           "publisher": "Pottermore", 
           "publishedDate": "2012-03-27", 
           "description": "Harry Potter ", 
           "industryIdentifiers": [ 
           { 
            "type": "ISBN_10", 
            "identifier": "1781100047" 
           } 
           ], 
           "printType": "BOOK", 
           "categories": [ 
             "Juvenile Fiction" 
           ], 
           "averageRating": 4.0, 
           "ratingsCount": 3560, 
           "contentVersion": "0.1.1.0.preview.2", 
           "imageLinks": 
            { 
           "smallThumbnail": "http://bks5.", 
            "thumbnail": "http://bks5.book" 
            }, 
             "language": "en", 
             "previewLink": "http:", 
             "infoLink": "http://b", 
             "canonicalVolumeLink": "http:" 
          }, 
          "saleInfo": { 
            "country": "PK", 
            "saleability": "NOT_FOR_SALE", 
            "isEbook": false 
          }, 
        "accessInfo": { 
          "country": "PK", 
          "viewability": "NO_PAGES", 
          "embeddable": false, 
          "publicDomain": false, 
           "textToSpeechPermission": "ALLOWED", 
          "epub": { 
           "isAvailable": true 
           }, 
            "pdf": { 
           "isAvailable": true 
           }, 
           "webReaderLink": "http://books.google.com/books/reader?id=jk87_y-ubE0C&hl=&printsec=frontcover&output=reader&source=gbs_api", 
           "accessViewStatus": "NONE" 
           }, 

           "searchInfo": { 
            "textSnippet": "Harry Potter is due to start his fifth year at Hogwarts School of Witchcraft and Wizardry." 
           } 
          } 
         ] 
        } 

это мои классы. я надеюсь, что я сгенерировали их правильно

public class IndustryIdentifier 
    { 
     public string type { get; set; } 
     public string identifier { get; set; } 
    } 

    public class ImageLinks 
    { 
     public string smallThumbnail { get; set; } 
     public string thumbnail { get; set; } 
    } 

    public class VolumeInfo 
    { 
     public string title { get; set; } 
     public List<string> authors { get; set; } 
     public string publisher { get; set; } 
     public string publishedDate { get; set; } 
     public string description { get; set; } 
     public List<IndustryIdentifier> industryIdentifiers { get; set; } 
     public string printType { get; set; } 
     public List<string> categories { get; set; } 
     public double averageRating { get; set; } 
     public int ratingsCount { get; set; } 
     public string contentVersion { get; set; } 
     public ImageLinks imageLinks { get; set; } 
     public string language { get; set; } 
     public string previewLink { get; set; } 
     public string infoLink { get; set; } 
     public string canonicalVolumeLink { get; set; } 
    } 

    public class SaleInfo 
    { 
     public string country { get; set; } 
     public string saleability { get; set; } 
     public bool isEbook { get; set; } 
    } 

    public class Epub 
    { 
     public bool isAvailable { get; set; } 
    } 

    public class Pdf 
    { 
     public bool isAvailable { get; set; } 
    } 

    public class AccessInfo 
    { 
     public string country { get; set; } 
     public string viewability { get; set; } 
     public bool embeddable { get; set; } 
     public bool publicDomain { get; set; } 
     public string textToSpeechPermission { get; set; } 
     public Epub epub { get; set; } 
     public Pdf pdf { get; set; } 
     public string webReaderLink { get; set; } 
     public string accessViewStatus { get; set; } 
    } 

    public class SearchInfo 
    { 
     public string textSnippet { get; set; } 
    } 

    public class Item 
    { 
     public string kind { get; set; } 
     public string id { get; set; } 
     public string etag { get; set; } 
     public string selfLink { get; set; } 
     public VolumeInfo volumeInfo { get; set; } 
     public SaleInfo saleInfo { get; set; } 
     public AccessInfo accessInfo { get; set; } 
     public SearchInfo searchInfo { get; set; } 
    } 

    public class RootObject 
    { 
     public string kind { get; set; } 
     public int totalItems { get; set; } 
     public List<Item> items { get; set; } 
    } 

ответ

1

Вам нужно преобразовать JSON в RootObject и не VolumeInfo, так что эта строка:

var jarray = JsonConvert.DeserializeObject<VolumeInfo>(responseBodyAsText); 

становится эта линия:

var jarray = JsonConvert.DeserializeObject<RootObject>(responseBodyAsText); 
0

Использование http://jsonclassgenerator.codeplex.com/ для создания классов для получаемого вами JSON.

Затем используйте var obj = JsonConvert.Deseiralize (responseBodyAsText);. Я тестировал это, и он отлично работает.