Home > ASP.NET > Serialize and Deserialize JSON using JSON.NET

Serialize and Deserialize JSON using JSON.NET

An awesome tool to convert json string to objects and back to json – JSON.NET
Get it from here
http://james.newtonking.com/projects/json-net.aspx

   //Deserialize
   WebClient wc = new WebClient();         
   string jsonStr = wc.DownloadString("http://example/api/GetAllBrandIds.aspx");
   JObject obj = JObject.Parse(jsonStr);
   JToken jt = obj["IdType"];
   BrandSet bs;
   List<BrandSet> list = new List<BrandSet>();
   foreach(JToken j in jt)
   {
   bs = new BrandSet(); 
   bs.ID = Convert.ToInt32((string)(j["Id"]));                
   bs.ImageType = Convert.ToInt32((string)(j["ImageType"]));
   list.Add(bs);
   }
  //Serialize back to json
  string jsonStr = JsonConvert.SerializeObject(list);
  

Happy coding:)

Advertisements
  1. No comments yet.
  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: