3

I'm very new to JSON, and I need to parse some that an API is providing. A quick google search turned up JSON.NET, so I'm trying to use it now to parse this JSON into a list object. First of all, is JSON.NET the best library to use for this?

This is what I'm trying to do: I have a class called Item, for example. The json has many "elements" (if that's what they are called), and each contains 3 fields: an integer named id, a string named name, and a datetime named creationTime. I would like to parse all of these Item "elements" from the json into a list of Item objects. I have created 3 fields in the Item class to match the JSON. How can this be done using JSON.NET?

I've tried:

List<Item> fav = (List<Item>)new JsonSerializer().Deserialize(new JsonReader((TextReader)new StreamReader(response.GetResponseStream())));

but it doesn't seem to work. I get a casting error - it just can't process it into a list enclosure, but I'm not even sure whether it's able to process the JSON into one Item class (JSON.NET is not very well-documented, but I'm going to heavily debug it tomorrow).

Can you give me some sample code to explain how I can parse it with JSON.NET?

Thanks!

UPDATE: By the way, forgot to mention - my project is going to be targeting .NET Framework 2.0, so I'm using the legacy version of JSON.NET: 1.3.1. Are there any HUGE advantages that may make the project worth converting to .NET 3.5, while undermining the minimal system requirements?

UPDATE #2: I have decided to use the JavascriptSerializer class in System.Web.Extensions.dll instead of JSON.NET, and a question about that is posted here. Thanks!

5
  • 1
    "Doesn't seem to work" isn't very detailed. What happens? And yes, I'd say that LINQ is a pretty huge advantage to .NET 3.5 in general, as well as meaning that you can use the more up-to-date version of JSON.NET. Commented Sep 19, 2009 at 9:10
  • @Jon well first of all, as far as I know, this is the first time that you have replied to one of my posts, so thank you! I unfortunately do not know LINQ, but I wish to learn it! I'm going to try to start using the more recent version then. What doesn't work is that I get a casting error - it just can't convert it to such a list enclosure. At least, that's what I believe it means, as JSON.NET is not very well-documented. Has anybody used JSON.NET before, and if so, could you explain how you would approach such a problem? Thanks! ;) Commented Sep 19, 2009 at 9:15
  • I suspect the problem is that you are specifically asking for help with the older version of JSON.NET; many people will only be familiar with the up-to-date version. Commented Sep 20, 2009 at 7:54
  • Comments don't "bump" the post... only edits and answers. Commented Sep 20, 2009 at 21:14
  • @Marc oh, I never knew that! Thanks! Commented Sep 20, 2009 at 22:28

2 Answers 2

1

I realise this question is about 3 years old, but I thought I'd just add that if you want to build a JSON based Api, NancyFx is awesome: http://nancyfx.org/

EDIT: An example as requested, really easy. To get started just add the nancyfx asp package to a web project via NuGet. (if you're adding it to an existing app on a sub path, you'll need to add a location to the web.config, otherwise you're good to go)

using Nancy;
using Nancy.ModelBinding;

public class Api : NancyModule
        {
            public Api()
            {
                Get["/api/order/create"] = x => 
                { 
                    var order = this.Bind<Order>(); //xml/json negotiated based on content header

                    var result = ... // Do stuff here

                    return Response.AsJson(result); 
                };
            }       
        }
Sign up to request clarification or add additional context in comments.

3 Comments

Looks nice! Could you link/post a relevant code sample? Thanks.
Thanks - seems elegant and straightforward. But this question addresses a different topic: parsing JSON, rather than writing JSON. When you Bind<Order>(), is request JSON being parsed into order? If not: unfortunately, the currently accepted answer also doesn't match the question's topic - not sure why I accepted it 3 years ago. I ended up using the built-in serialization tools (stackoverflow.com/q/1450513/130164). Do you know if NancyFx can parse JSON, and if so, could you append a code sample of deserializing JSON input to your answer so this question gets back on track?
You're correct - "this.Bind<Order>()" takes the body of the request and deserializes it to "order", it's very neat. Nancy uses JSON.Net as it's default json parser, and ASP.Net MVC4 will also be using Json.Net by default. As an alternative there is also ServiceStack, but I've always been comfortable with Json.net myself.
0

I would recomend that you upgrade to .net framework 3.5 and use ASP.Net MVC to build your json services. See:

http://msmvps.com/blogs/omar/archive/2008/10/03/create-rest-api-using-asp-net-mvc-that-speaks-both-json-and-plain-xml.aspx

1 Comment

This link no longer works. Can you place the relevant information here?

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.