2013-07-21 66 views
0

我試圖deserealize JSON作爲`JsonConvert.DeserializeObject(responseBodyAsText)對象。問題是我無法返回任何東西。我如何填充我從JSON生成的類以及以何種順序?我需要給他們打電話嗎?谷歌圖書API反序列化jsonconvert返回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); 

這是我的谷歌圖書API 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);