Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert JSON string to C# dictionary

Tags:

json

c#

I have a JSON string

{
  "Date":"21/11/2010"
  "name": "TEST"
  "place":"xyz"
}

I want to convert it into a C# dictionary without using a third party library

like image 629
sameer karjatkar Avatar asked Aug 08 '13 07:08

sameer karjatkar


People also ask

Can we convert string to JSON in C?

Using JsonConverterJsonConvert class has a method to convert to and from JSON string, SerializeObject() and DeserializeObject() respectively. It can be used where we won't to convert to and from a JSON string.

How do I convert a JSON to a string?

Use the JavaScript function JSON.stringify() to convert it into a string. const myJSON = JSON.stringify(obj); The result will be a string following the JSON notation.

How will you transform a JSON file to C# objects?

Use the JavaScriptSerializer class to provide serialization and deserialization functionality for AJAX-enabled ASP.NET web applications. The JavaScriptSerializer. Deserialize() method converts the specified JSON string to the type of the specified generic parameter object.


1 Answers

You can do it natively since net 3.5 with jsonserializer.

var jss = new JavaScriptSerializer();
var dict = jss.Deserialize<Dictionary<string,string>>(jsonText);
var place = dict["place"]; // "xyz"

Here is a simple tutorial for your case: Quick JSON Serialization/Deserialization in C#

Requires the System.Web.Extensions reference. If you can't find it, your program is probably using a Client target framework. Use a "Full" target framework.

like image 185
zewa666 Avatar answered Nov 16 '22 02:11

zewa666