You must Sign In to post a response.
  • Category: ASP.NET

    Json Deserialize in c#

    Json Deserialize in c#


    string json =
    {"forgotPassword_webResult":{"clientId":"4d62d41ed3f9a771bb5c09644dd04c1a","message":"SMS sent","status":true}}

    how to i convert

    JavaScriptSerializer jsSerializer = new JavaScriptSerializer();
    var ListAnswers = serializer.Deserialize<List<forgotPassword1>>("{'forgotPassword_webResult':{'clientId':'4d62d41ed3f9a771bb5c09644dd04c1a','message':'SMS sent','status':true}}");


    public class forgotPassword1
    {

    public List<errStatus1> forgotPassword_webResult = new List<errStatus1>();

    }
    public class errStatus1
    {
    public bool status { get; set; }

    public string message { get; set; }

    public string clientId { get; set; }
    }
  • #766762
    Hi
    try this code for Json Deserialize in c#



    SqlCommand cmd = new SqlCommand("select * from TestTable", con);
    {
    SqlDataAdapter sqlDa = new SqlDataAdapter(cmd);
    DataTable dt = new DataTable();
    sqlDa.Fill(dt);
    string test = JsonConvert.SerializeObject(dt); // Serialization
    //JsonConvert.DeserializeObject(test);
    DataTable dtt = (DataTable)JsonConvert.DeserializeObject(test, dt.GetType());
    return test;
    }

    Name : Dotnet Developer-2015
    Email Id :kumaraspcode2009@gmail.com

    'Not by might nor by power, but by my Spirit,' says the LORD Almighty.

  • #766773
    One way is to create a new model to Deserialize your Json
    see below sample snippet

    public class CustomerJson
    {
    [JsonProperty("customer")]
    public Customer Customer { get; set; }
    }

    public class Customer
    {
    [JsonProperty("first_name")]
    public string Firstname { get; set; }

    [JsonProperty("last_name")]
    public string Lastname { get; set; }

    ...
    }

    and then DE serialize it

    JsonConvert.DeserializeObject<List<CustomerJson>>(json);

    hope it helps

    Thanks
    Koolprasd2003
    Editor, DotNetSpider MVM
    Microsoft MVP 2014 [ASP.NET/IIS]

  • #766895

    public class Friends
    {

    public List<abcd> data {get;set;}
    }

    public class abcd
    {

    public string id {get;set;}
    public string name {get;set;}
    }
    Friends abcd= new JavaScriptSerializer().Deserialize<Friends>(result);


    SRI RAMA PHANI BHUSHAN KAMBHAMPATI


Sign In to post your comments