2

I have a remote URL from where I read a JSON Object that looks like this:

{"books":
 [
   {"title":"Book 1", "author":"Author1", "price":762, "inStock":15},
   {"title":"Book 2", "author":"Author2", "price":100, "inStock":1},
   {"title":"Book 3", "author":"Author3", "price":185.5, "inStock":5},
   {"title":"Book 4", "author":"Author 4", "price":1748, "inStock":3},
   {"title":"Book 5", "author":"Author 5", "price":999, "inStock":20},
   {"title":"Book 6", "author":"Author 6", "price":499.5, "inStock":3},
   {"title":"Book 7", "author":"Author 7", "price":564.5, "inStock":0}
 ]
}

I have created two classes Book.cs

public class Book
{
    public string title;

    public string author;

    public string price;

    public string inStock;
}

And Books.cs

public class Books
{
    public IList<Book> books { get; set; }
}

How to correctly parse the JSON so I can show the contents in the Razor HTML

This is my controller:

public ActionResult Index()
{
    var webClient = new WebClient();
    var json = webClient.DownloadString(@"http://www.myurl.json");
    Books[] books = JsonConvert.DeserializeObject<Books[]>(json);

    return View(books);
}
1
  • are you getting any error? Commented Feb 15, 2017 at 21:08

5 Answers 5

6

Your Books class already contains a collection property to represent each of your individual books, so you don't need to actually deserialize a Books[] but rather just a Books object :

// Since Books is already a container element, it will map the "books" property
// from your JSON object to the matching IList<Book> property
var books = JsonConvert.DeserializeObject<Books>(json);

Example

enter image description here

You can see a complete working example of this here and the example code demonstrated in the snippet below.

// YourClasses.cs
namespace Example
{
	public class Book
	{
		public string title;
	
		public string author;
	
		public string price;
	
		public string inStock;
	}
	
	public class Books
	{
		public IList<Book> books;
	}
}

// YourController.cs
namespace Example
{
	public class HomeController : Controller
	{
		[HttpGet]
		public ActionResult Index()
		{
			// Example JSON in lieu of API call
			var json = "{\"books\":[{\"title\":\"Book 1\", \"author\":\"Author1\", \"price\":762, \"inStock\":15},{\"title\":\"Book 2\", \"author\":\"Author2\", \"price\":100, \"inStock\":1},{\"title\":\"Book 3\", \"author\":\"Author3\", \"price\":185.5, \"inStock\":5},{\"title\":\"Book 4\", \"author\":\"Author 4\", \"price\":1748, \"inStock\":3},{\"title\":\"Book 5\", \"author\":\"Author 5\", \"price\":999, \"inStock\":20},{\"title\":\"Book 6\", \"author\":\"Author 6\", \"price\":499.5, \"inStock\":3},{\"title\":\"Book 7\", \"author\":\"Author 7\", \"price\":564.5, \"inStock\":0}]}";	
			var books = JsonConvert.DeserializeObject<Books>(json);
			return View(books);
		}
	}
}

// Index.cshtml
@model Example.Books
<ul>
    @foreach(var book in Model.books){
		<li>@book.title</li>
	}
</ul>

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you very much :) Works like a charm.
2

You don't want an array of Books. You just want the Books container:

Books books = JsonConvert.DeserializeObject<Books>(json);

Comments

1

Here is the Book class you need

public class Book
{
    public string title { get; set; }
    public string author { get; set; }
    public double price { get; set; }
    public int inStock { get; set; }
}

Here is the Books class you need

public class Books
{
    public List<Book> books { get; set; }
}

You need to have this in your code

public ActionResult Index()
{
    var webClient = new WebClient();
    var json = webClient.DownloadString(@"http://www.myurl.json");
    Books books = JsonConvert.DeserializeObject<Books>(json);

    return View(books);
}

Comments

0

To make sure your classes are correct for your JSON, you could "paste JSON as classes" in VS pro and then deserialise.

Comments

0

Declare Books as:

public class Books : List<Book> 

And desrialize it as indicated by @chadnt:

Books books = JsonConvert.DeserializeObject<Books>(json);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.