c# - JSON.NET get specific values -
iam new json.net , iam trying informations gooogle book api. send request book api , response 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 tried use json.net this: jobject responeobject = jobject.parse(responsestring); jtoken items = responeobject["items"]; jtoken item= items[0]; string booktitle; booktitle=item["title"].value<string>();
i 1 item... cant volumeinformations, example title...
where mistake?
hans
use 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);
Comments
Post a Comment