2013-10-07 39 views
1

iam新增JSON.NET和iam試圖從gooogle書api獲取一些信息。 我送書API的請求,並得到這樣的迴應:JSON.NET獲取特定值

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

現在我試圖用json.net這樣的:JObject responeObject = JObject.Parse(responsestring); JToken items = responeObject["items"]; JToken item= items[0]; string booktitle; booktitle=item["title"].Value<String>();

我得到的一個項目......但我不能獲取音量信息,例如標題...

我的錯誤在哪裏?

漢斯

回答

1

使用此代碼

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

反序列化

var obj = JsonConvert.DeserializeObject<SampleResponse>(Your_String);