Generic JSON parser in .NET / WPF?

I read a lot of tutorials on how to deserialize a JSON object for a specific object using DataContractJsonSerializer. However, I would like to deserialize my object in a dictionary consisting of strings, arrays, or dictionaries such as System.Json with SilverLight when I say JsonObject.Parse (myJSONstring).

Is there an equivalent to System.Json that can be used in my WPF project?

(just a short background: I am retrieving JSON objects that have a lot of information, and I just want to use a little bit to populate the String array)

Greetings

Nick

+7
json c # serialization
source share
4 answers

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

+3
source share

Just use the built-in JavaScriptSerializer.NET.

var jss = new JavaScriptSerializer(); var data = jss.Deserialize<dynamic>(jsonString); //"data" actually implements IDictionary<string, object> var p1 = data["Property1"]; var p2 = data["Property2"]; 

Do not forget to specify "System.Web.Extensions"

+8
source share

I have been using JayRock successfully: http://jayrock.berlios.de/

 public class JayRockMarshaller : IMarshaller { public ICollection Read(string text) { return (ICollection)new ImportContext().Import(new JsonTextReader(new StringReader(text))); } public string Write(ICollection objectToMarshal) { var writer = new StringWriter(); new ExportContext().Export(objectToMarshal, new JsonTextWriter(writer)); return writer.ToString(); } } 

Works for both dictionaries and lists, like a dream.

+1
source share

Also look at https://github.com/jlarsson/Kiwi.Json , it processes all kinds of data types, and you can easily create your own converter if the built-in does not fit.

There is a blog where you can find samples on this, for example: http://dancewithcode.wordpress.com/2012/03/24/case-study-custom-json-converter-for-datatable/

0
source share

All Articles