4

Is there a JSON parser for .NET 4.0?


Ideally something like:

String jsonText = GetTheJsonFromTheInternet();
var json = JsonValue.Parse(jsonText);

and now i have a nested set of key-value pairs.

Use JSON.net

What i dont want, is to have to create a set of objects to match the JSON. i asking about parsing JSON, not deserializing JSON. You can pretend it's because i don't know the structure of the JSON.

Similar to how you parse XML:

String xmlText = GetTheXmlFromTheInternet();
XmlDocument doc = new XmlDocument();
doc.LoadXml(XmlText);

and now i have a nested set of names-values-attributes. You don't create objects to represent the XML DOM tree; you parse a string of XML and now it's easy to navigate and find things.

Why not just use JsonValue.Parse()?

Because:

  • JsonValue (found in System.Json.dll) was not available until .NET Framework 4.5.
  • and Visual Studio 2010 cannot target .NET Framework 4.5. (only Visual Studio 2012)
  • and Visual Studio 2012 requires Windows 8

And writing my own JSON parser would take a few days (to get it correct and good).

i've gone through the JSON.net documentation. i can't tell if it supports JSON parsing.

14
  • 8
    Visual Studio 2012 runs fine on Windows 7. Commented May 20, 2013 at 15:23
  • From what I understand of your question, I think you want to parse the JSON to a dictionary instead? Commented May 20, 2013 at 15:24
  • 4
    Visual Studio 2012 doesn't require Win8, I've been running it just fine on Win7 Commented May 20, 2013 at 15:25
  • 1
    This question seem really really hostile. Commented May 20, 2013 at 15:51
  • 4
    The question only seems hostile because people that ask questions on SO are sick and tired of either getting a "you're stupid, use this thing" response or having their questions constantly closed as duplicates of something else that actually has nothing at all to do with the issue. It's to the point where you have to not only phrase your question, you also have to supply a long list of reasons why you're not using something else or why it's not a duplicate question. Commented Feb 18, 2015 at 15:16

1 Answer 1

5

Use Newtonsoft.Json.JsonConvert.DeserializeObject. It will return either JObject or JArray. You can use all Linq tricks on them.

You can also assign the result of JsonConvert.DeserializeObject to dynamic and use duck typing

Some examples:

Example

String jsonText = 
    @"{
        ""files"":[{
            ""url"":""http://us.battle.net/auction-data/x/auctions.json"",
            ""lastModified"":1369051860000
        }]
    }";

JToken data = JObject.Parse(jsonText);

String url = (String)data["files"][0]["url"];
Int64 lastModified = (Int64)data["files"][0]["lastModified"];
Sign up to request clarification or add additional context in comments.

1 Comment

@sixlettervariables yes if you are sure it is not an array. using JsonConvert.DeserializeObject is safer

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.