Parsing JSON Strings in Metro Applications without JSON.NET
As you attempt to develop Metro applications on Visual Studio 2011, you may encounter the challenge of parsing JSON data without the JSON.NET library. Unfortunately, this library currently lacks support for Metro applications.
Utilizing System.Json Namespace Classes
To overcome this obstacle, you can leverage the System.Json namespace classes introduced in .NET 4.5. Add a reference to the System.Runtime.Serialization assembly to access these classes.
Parsing JSON Text
Invoke the JsonValue.Parse() method to parse JSON text and obtain a JsonValue object. For instance:
JsonValue value = JsonValue.Parse(@"{ ""name"":""Prince Charming"", ...");
If the input contains a JSON object, you can cast the value to a JsonObject:
JsonObject result = value as JsonObject;
Extracting Data from JsonObject
Once you have a JsonObject, you can retrieve specific data elements using the [] operator. For example:
Console.WriteLine("Name .... {0}", (string)result["name"]); Console.WriteLine("Artist .. {0}", (string)result["artist"]); Console.WriteLine("Genre ... {0}", (string)result["genre"]);
The System.Json namespace classes exhibit similarities to those in the System.Xml.Linq namespace. By employing these techniques, you can effectively parse JSON data in your Metro applications.
The above is the detailed content of How to Parse JSON Strings in Metro Apps without JSON.NET?. For more information, please follow other related articles on the PHP Chinese website!