3

Is there a native library that would allow me to parse a Json formatted string? Say, for example, I had the string:

string inp = "{ \"title\": \"My Title\" }";

Is there a class where I can construct an object using that Json formatted string and find the value for title?

Edit: My app is a console application.

6
  • What kind of C# application? Certain projects (like MVC) have libraries available to handle this, otherwise you're looking at using another third-party one. -- e.g. JavaScriptSerializer Commented Sep 25, 2012 at 22:46
  • I am trying to find one that can be used from a Console app. Commented Sep 25, 2012 at 22:49
  • @BradChristie: You can generally use most libraries from most environments. Commented Sep 25, 2012 at 22:49
  • @TheDude: Both alternatives in my answer work with a console app. Commented Sep 25, 2012 at 22:49
  • 1
    @Eric: although you can, I generally wouldn't find myself including an MVC library in a WCF project just to use one object or feature. Commented Sep 25, 2012 at 22:49

4 Answers 4

5

You can use the DataContractJsonSerializer to deserialize a Json formatted string into an object

http://msdn.microsoft.com/en-us/library/bb410770.aspx

For better performance, if you're willing to install a NuGet package,

http://json.codeplex.com/

is quite popular.

Both alternatives work for a console application.

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

2 Comments

DataContractJsonSerializer is what I am looking for!
DataContractJsonSerializer has many issues. Even Microsoft seems to be using Json.Net lately. Watch out for DCJS issues with DateTimes.
3

Not a native but a powerful one: Json.NET

  • Flexible JSON serializer for converting between .NET objects and JSON
  • LINQ to JSON for manually reading and writing JSON
  • High performance, faster than .NET's built-in JSON serializers
  • Write indented, easy to read JSON
  • Convert JSON to and from XML
  • Supports .NET 2, .NET 3.5, .NET 4, Silverlight, Windows Phone and Windows 8.

2 Comments

There is a native solution now in .NET. I agree though that Json.NET is generally superior.
@EricJ. I also prefer Json.NET, way more flexible.
1

Depending on the type of project, JavaScriptSerializer may be available. Also allows you to create parsers that can populate custom objects.

1 Comment

A link to a previous post I made regarding the JavaScriptConverter can be found here: stackoverflow.com/a/4999004/298053
1

You should check out Json.Net http://json.codeplex.com/ It has all sorts of great tools for supporting Json in. Net

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.