0

Possible Duplicate:
Parse JSON in C#

I'm trying to deserialize a JSON string from the openlibrary.org in an ASP.NET (4.5) web application using JSON.NET.

My aim is to be able to read the 5 properties below from a single .Net object.

The example JSON I have is:

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

I can get it to work fine without the first line, but I'm a bit lost trying to work out how I should structure my classes.

I'm new to JSON and haven't played with C#/VB.NET in a few years so getting very lost.

Update

I have the following code:

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)

And the class Book:

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

However, book turns up empty.

Community
  • 1
  • 1
ataru
  • 1
  • 2

3 Answers3

1

There is a website called json2csharp - generate c# classes from json:

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

The json format is a little off, remove the {"ISBN:0201558025": since you have the ISBN as the bib_key

Jeremy Thompson
  • 61,933
  • 36
  • 195
  • 321
  • Thanks, I had tried that but wasn't sure about the ISBN bit. The problem I'm having now is that my book object isn't receiving the details, which lead me to think I may be missing a class. – ataru Dec 22 '12 at 14:09
1

Try using JSON.Net

or

JavaScriptSerializer Class

or

DataContractSerializer class

Murali Murugesan
  • 22,423
  • 17
  • 73
  • 120
1

I think it can be deserialized as a Dictionary.

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