1

iam new to JSON.NET and iam trying to get some informations from the gooogle book api. I send a request to the book api and get a response like this:

>     {
 "kind": "books#volumes",
 "totalItems": 1,
 "items": [
  {
   "kind": "books#volume",
   "id": "cqBNpxozvxsC",
   "etag": "M3um0RHW0ak",
   "selfLink": "https://www.googleapis.com/books/v1/volumes/cqBNpxozvxsC",
   "volumeInfo": {
    "title": "Rothfuss,Name d.Windes",
    "authors": [
     "Patrick Rothfuss"
    ],
    "publisher": "Klett-Cotta",
    "publishedDate": "2010",
    "industryIdentifiers": [
     {
      "type": "ISBN_10",
      "identifier": "3608938788"
     },
     {
      "type": "ISBN_13",
      "identifier": "9783608938784"
     }
    ],
    "pageCount": 876,
    "printType": "BOOK",
    "contentVersion": "0.0.1.0.preview.1",
    "imageLinks": {
     "smallThumbnail": "http://bks9.books.google.de/books?id=cqBNpxozvxsC&printsec=frontcover&img=1&zoom=5&edge=curl&source=gbs_api",
     "thumbnail": "http://bks9.books.google.de/books?id=cqBNpxozvxsC&printsec=frontcover&img=1&zoom=1&edge=curl&source=gbs_api"
    },
    "language": "de",
    "previewLink": "http://books.google.de/books?id=cqBNpxozvxsC&printsec=frontcover&dq=isbn:9783608938784&hl=&cd=1&source=gbs_api",
    "infoLink": "http://books.google.de/books?id=cqBNpxozvxsC&dq=isbn:9783608938784&hl=&source=gbs_api",
    "canonicalVolumeLink": "http://books.google.de/books/about/Rothfuss_Name_d_Windes.html?hl=&id=cqBNpxozvxsC"
   },
   "saleInfo": {
    "country": "DE",
    "saleability": "NOT_FOR_SALE",
    "isEbook": false
   },
   "accessInfo": {
    "country": "DE",
    "viewability": "PARTIAL",
    "embeddable": true,
    "publicDomain": false,
    "textToSpeechPermission": "ALLOWED",
    "epub": {
     "isAvailable": false
    },
    "pdf": {
     "isAvailable": false
    },
    "webReaderLink": "http://books.google.de/books/reader?id=cqBNpxozvxsC&hl=&printsec=frontcover&output=reader&source=gbs_api",
    "accessViewStatus": "SAMPLE"
   }
  }
 ]
}

now i tried to use json.net like this: JObject responeObject = JObject.Parse(responsestring); JToken items = responeObject["items"]; JToken item= items[0]; string booktitle; booktitle=item["title"].Value<String>();

i get the one item... but i cant get the volumeinformations, for example the title...

where is my mistake?

Hans

1 Answer 1

1

Use this code

 public class SampleResponse
{

    [JsonProperty("kind")]
    public string Kind { get; set; }

    [JsonProperty("totalItems")]
    public int TotalItems { get; set; }

    [JsonProperty("items")]
    public Item[] Items { get; set; }
}

public class AccessInfo
{

    [JsonProperty("country")]
    public string Country { get; set; }

    [JsonProperty("viewability")]
    public string Viewability { get; set; }

    [JsonProperty("embeddable")]
    public bool Embeddable { get; set; }

    [JsonProperty("publicDomain")]
    public bool PublicDomain { get; set; }

    [JsonProperty("textToSpeechPermission")]
    public string TextToSpeechPermission { get; set; }

    [JsonProperty("epub")]
    public Epub Epub { get; set; }

    [JsonProperty("pdf")]
    public Pdf Pdf { get; set; }

    [JsonProperty("webReaderLink")]
    public string WebReaderLink { get; set; }

    [JsonProperty("accessViewStatus")]
    public string AccessViewStatus { get; set; }
}

 public class Epub
{

    [JsonProperty("isAvailable")]
    public bool IsAvailable { get; set; }
}

 public class ImageLinks
{

    [JsonProperty("smallThumbnail")]
    public string SmallThumbnail { get; set; }

    [JsonProperty("thumbnail")]
    public string Thumbnail { get; set; }
}

public class IndustryIdentifier
{

    [JsonProperty("type")]
    public string Type { get; set; }

    [JsonProperty("identifier")]
    public string Identifier { get; set; }
}


public class Item
{

    [JsonProperty("kind")]
    public string Kind { get; set; }

    [JsonProperty("id")]
    public string Id { get; set; }

    [JsonProperty("etag")]
    public string Etag { get; set; }

    [JsonProperty("selfLink")]
    public string SelfLink { get; set; }

    [JsonProperty("volumeInfo")]
    public VolumeInfo VolumeInfo { get; set; }

    [JsonProperty("saleInfo")]
    public SaleInfo SaleInfo { get; set; }

    [JsonProperty("accessInfo")]
    public AccessInfo AccessInfo { get; set; }
}

 public class Pdf
{

    [JsonProperty("isAvailable")]
    public bool IsAvailable { get; set; }
}

  public class SaleInfo
{

    [JsonProperty("country")]
    public string Country { get; set; }

    [JsonProperty("saleability")]
    public string Saleability { get; set; }

    [JsonProperty("isEbook")]
    public bool IsEbook { get; set; }
}

  public class VolumeInfo
{

    [JsonProperty("title")]
    public string Title { get; set; }

    [JsonProperty("authors")]
    public string[] Authors { get; set; }

    [JsonProperty("publisher")]
    public string Publisher { get; set; }

    [JsonProperty("publishedDate")]
    public string PublishedDate { get; set; }

    [JsonProperty("industryIdentifiers")]
    public IndustryIdentifier[] IndustryIdentifiers { get; set; }

    [JsonProperty("pageCount")]
    public int PageCount { get; set; }

    [JsonProperty("printType")]
    public string PrintType { get; set; }

    [JsonProperty("contentVersion")]
    public string ContentVersion { get; set; }

    [JsonProperty("imageLinks")]
    public ImageLinks ImageLinks { get; set; }

    [JsonProperty("language")]
    public string Language { get; set; }

    [JsonProperty("previewLink")]
    public string PreviewLink { get; set; }

    [JsonProperty("infoLink")]
    public string InfoLink { get; set; }

    [JsonProperty("canonicalVolumeLink")]
    public string CanonicalVolumeLink { get; set; }
}

to Deserialize

var obj = JsonConvert.DeserializeObject<SampleResponse>(Your_String);
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.