2012-12-22 40 views
0

可能重複:
Parse JSON in C#.NET類需要反序列化JSON字符串

我試圖從反序列化在ASP.NET中openlibrary.org JSON字符串(4.5 )使用JSON.NET的Web應用程序。

我的目標是能夠從單個.Net對象讀取下面的5個屬性。

example JSON 我是:

{"ISBN:0201558025": 
    { 
     "bib_key": "ISBN:0201558025", 
     "preview": "noview", 
     "thumbnail_url": "http://covers.openlibrary.org/b/id/135182-S.jpg", 
     "preview_url": "http://openlibrary.org/books/OL1429049M/Concrete_mathematics", 
     "info_url": "http://openlibrary.org/books/OL1429049M/Concrete_mathematics" 
    } 
} 

我可以得到它沒有第一線工作得很好,但我有點失去試圖找出我應該怎麼構建我的班。

我是JSON的新手,幾年之後還沒有在C#/ VB.NET中玩過,所以很迷茫。

更新

我有以下代碼:

Dim url = "http://openlibrary.org/api/books?bibkeys=ISBN:0201558025&format=json" 
Dim webClient = New System.Net.WebClient 
Dim json = webClient.DownloadString(url) 

Dim book As Book = JsonConvert.DeserializeObject(Of Book)(json) 

和類圖書:

Public Class Book 
    Public bib_key As String 
    Public preview As String 
    Public preview_url As String 
    Public info_url As String 
End Class 

然而,書變成了空。

回答

1

有一個叫json2csharp網站 - 生成JSON的C#類:

public class RootObject 
{ 
    public string bib_key { get; set; } 
    public string preview { get; set; } 
    public string thumbnail_url { get; set; } 
    public string preview_url { get; set; } 
    public string info_url { get; set; } 
} 

JSON格式是有點過,去掉{ 「ISBN:0201558025」:既然你有ISBN爲bib_key

+0

謝謝,我曾試過,但不知道有關的國際標準書號位。我現在面臨的問題是,我的圖書對象沒有收到詳細信息,這導致我認爲我可能錯過了一堂課。 – ataru

1

我認爲它可以反序列化的詞典。

new System.Web.Script.Serialization.JavaScriptSerializer().Deserialize<Dictionary<string, BookInfoClass>>(jsonString);