0

可能重复:
在 C# 中解析 JSON

我正在尝试使用 JSON.NET 在 ASP.NET (4.5) Web 应用程序中反序列化来自 openlibrary.org 的 JSON 字符串。

我的目标是能够从单个 .Net 对象中读取以下 5 个属性。

我的示例 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

然而,书变成了空的。

4

3 回答 3

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

于 2012-12-22T02:17:54.017 回答
1

我认为它可以反序列化为字典。

new System.Web.Script.Serialization.JavaScriptSerializer().Deserialize<Dictionary<string, BookInfoClass>>(jsonString);
于 2012-12-22T04:14:34.463 回答
1

尝试使用JSON.Net

或者

JavaScriptSerializer

或者

DataContractSerializer

于 2012-12-22T03:13:19.403 回答