Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Generic JSON parser in .NET / WPF?

I've read lots of tutorials on how to deserialize a JSON object to an object of a particular using DataContractJsonSerializer. However, I'd like to deserialize my object to a Dictionary consisting of either Strings, Arrays or Dictionaries, such as System.Json does with SilverLight when I say JsonObject.Parse(myJSONstring).

Is there an equivalent to System.Json that I can use in my WPF project?

(just a short background: I'm fetching JSON objects that have way to much info, and I just want to use a little bit to fill out a String array)

Cheers

Nik

like image 300
niklassaers Avatar asked Dec 03 '09 15:12

niklassaers


2 Answers

Just use .NET's built-in JavaScriptSerializer.

var jss = new JavaScriptSerializer();
var data = jss.Deserialize<dynamic>(jsonString);

//"data" actually implements IDictionary<string, object>
var p1 = data["Property1"];
var p2 = data["Property2"];

Don't forget to reference "System.Web.Extensions"

like image 69
Serge Shultz Avatar answered Sep 22 '22 04:09

Serge Shultz


Take a look at the C# section (scoll to the bottom) of http://json.org/, they have several implementations of serializers and parsers that should help.

like image 20
Rory Avatar answered Sep 22 '22 04:09

Rory