Eli Revah Eli Revah - 10 months ago 79
C# Question

How to parse JSON without JSON.NET library?

I'm trying to build a Metro application for Windows 8 on Visual Studio 2011.
and while I'm trying to do that, I'm having some issues on how to parse

library (It doesn't support the metro applications yet).

Anyway, I want to parse this:

"name":"Prince Charming",
"genre":"Rock and Metal",

dtb dtb
Answer Source

You can use the classes found in the System.Json Namespace which were added in .NET 4.5.

The JsonValue.Parse() Method parses JSON text and returns a JsonValue:

JsonValue value = JsonValue.Parse(@"{ ""name"":""Prince Charming"", ...");

If you pass a string with a JSON object, you should be able to cast the value to a JsonObject:

JsonObject result = value as JsonObject;

Console.WriteLine("Name .... {0}", (string)result["name"]);
Console.WriteLine("Artist .. {0}", (string)result["artist"]);
Console.WriteLine("Genre ... {0}", (string)result["genre"]);
Console.WriteLine("Album ... {0}", (string)result["album"]);

The classes are quite similar to those found in the System.Xml.Linq Namespace.