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 JSON
without JSON.NET
library (It doesn't support the metro applications yet).
Anyway, I want to parse this:
{
"name":"Prince Charming",
"artist":"Metallica",
"genre":"Rock and Metal",
"album":"Reload",
"album_image":"http:\/\/up203.siz.co.il\/up2\/u2zzzw4mjayz.png",
"link":"http:\/\/f2h.co.il\/7779182246886"
}
I use this...but have never done any metro app development, so I don't know of any restrictions on libraries available to you. (note, you'll need to mark your classes as with DataContract and DataMember attributes)
So, if you had a class like this...
Then you would use it like this...
For those who do not have 4.5, Here is my library function that reads json. It requires a project reference to
System.Web.Extensions
.Usually, json is written out based on a contract. That contract can and usually will be codified in a class (
T
). Sometimes you can take a word from the json and search the object browser to find that type.Example usage:
Given the json
You could parse it into a
RadComboBoxClientState
object like this:Have you tried using
JavaScriptSerializer
? There's alsoDataContractJsonSerializer
You can use the classes found in the System.Json Namespace which were added in .NET 4.5. You need to add a reference to the System.Runtime.Serialization assembly
The JsonValue.Parse() Method parses JSON text and returns a JsonValue:
If you pass a string with a JSON object, you should be able to cast the value to a JsonObject:
The classes are quite similar to those found in the System.Xml.Linq Namespace.
You can use
DataContractJsonSerializer
. See this link for more details.