google book api deserialize jsonconvert返回null

时间:2013-07-21 01:47:02

标签: windows-phone-8 httpclient json.net

我试图将json deserealize为`JsonConvert.DeserializeObject(responseBodyAsText)对象。问题是我无法退回任何东西。我如何填充我从json下面生成的类以及以何种顺序?我需要按顺序打电话给他们吗?

              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 books api json return

{
 "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; }
    }

2 个答案:

答案 0 :(得分:1)

您需要将您的json转换为RootObject而不是VolumeInfo,所以这一行:

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

成为这一行:

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

答案 1 :(得分:0)

使用http://jsonclassgenerator.codeplex.com/为您收到的JSON生成类。

然后使用 var obj = JsonConvert.Deseiralize(responseBodyAsText); 。我测试了这个并且它完美无缺。