Friday, 1 March 2013

Json Serialization/Deserialization


JSON string:

[
  {
    "Name": "Ajay",
    "City": "Kalol"
  }
]


 Code To Serialize/Deserialize JSON : 

  DataTable dt = new DataTable();
  dt.Columns.Add("Name");
  dt.Columns.Add("City");
  DataRow dr = dt.NewRow();
  dr["Name"] = "Ajay";
  dr["City"] = "Kalol";
  dt.Rows.Add(dr);
  string ConvertToJson = JsonConvert.SerializeObject(dt, Formatting.Indented,
   new JsonSerializerSettings { NullValueHandling = NullValueHandling.Include,
                                TypeNameHandling = TypeNameHandling.Objects,
                DateFormatHandling = DateFormatHandling.MicrosoftDateFormat });


DataTable DeserializefromJson = (DataTable)JsonConvert.DeserializeObject(str,
                                                         (typeof(DataTable)));

No comments:

Post a comment