Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the simplest C# function to parse a JSON string into an object? [closed]

Tags:

json

c#

.net

wpf

What is the simplest C# function to parse a JSON string into a object and display it (C# XAML WPF)? (for example object with 2 arrays - arrA and arrB)

like image 952
Rella Avatar asked May 18 '10 18:05

Rella


People also ask

What is C language example?

C language is a system programming language because it can be used to do low-level programming (for example driver and kernel). It is generally used to create hardware devices, OS, drivers, kernels, etc. For example, Linux kernel is written in C. It can't be used for internet programming like Java, .Net, PHP, etc.


2 Answers

Just use the Json.NET library. It lets you parse Json format strings very easily:

JObject o = JObject.Parse(@" {     ""something"":""value"",     ""jagged"":     {         ""someother"":""value2""     } }");  string something = (string)o["something"]; 

Documentation: Parsing JSON Object using JObject.Parse

like image 59
Philip Daubmeier Avatar answered Sep 22 '22 21:09

Philip Daubmeier


DataContractJsonSerializer serializer =      new DataContractJsonSerializer(typeof(YourObjectType));  YourObjectType yourObject = (YourObjectType)serializer.ReadObject(jsonStream); 

You could also use the JavaScriptSerializer, but DataContractJsonSerializer is supposedly better able to handle complex types.

Oddly enough JavaScriptSerializer was once deprecated (in 3.5) and then resurrected because of ASP.NET MVC (in 3.5 SP1). That would definitely be enough to shake my confidence and lead me to use DataContractJsonSerializer since it is hard baked for WCF.

like image 34
Justin Niessner Avatar answered Sep 20 '22 21:09

Justin Niessner