Parsing JSON in Metro Applications without JSON.NET
In developing Metro applications for Windows 8 on Visual Studio 2011, you may encounter challenges in parsing JSON data. To address this, Microsoft has introduced classes within the System.Json Namespace, accessible in .NET 4.5 onwards.
Parsing JSON using System.Json
To begin parsing JSON, add a reference to the System.Runtime.Serialization assembly. Utilize JsonValue.Parse() to parse JSON text and obtain a JsonValue. If the JSON string represents an object, you can cast the value to a JsonObject.
Example Code
using System.Json; // Parse JSON text into a JsonValue JsonValue value = JsonValue.Parse(@"{ ""name"":""Prince Charming"", ..."); // Cast the value to a JsonObject JsonObject result = value as JsonObject; // Access property values 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"]);
Similarities to System.Xml.Linq
The classes in the System.Json Namespace resemble those in System.Xml.Linq. This allows for straightforward navigation and manipulation of JSON data within your Metro applications.
The above is the detailed content of How Can I Parse JSON Data in Windows 8 Metro Apps without JSON.NET?. For more information, please follow other related articles on the PHP Chinese website!