开发者

how to deserialize JSON data on the server side after sending from client side

开发者 https://www.devze.com 2023-03-27 16:04 出处:网络
this way i am sending my json data this way i construct my json data like below one function Post(carousel, first, last, per_pag开发者_运维问答e, page) {

this way i am sending my json data

this way i construct my json data like below one


function Post(carousel, first, last, per_pag开发者_运维问答e, page) {
  var json = "{'Name':'" + $("input[id*='txtName']").val() +
            "','Subject':'" + $("input[id*='txtSubject']").val() +
            "','Email':'" + $("input[id*='txtEmail']").val() +
            "','Message':'" + jQuery.trim($('.message').val()) + "'}";



    $.ajax({
        type: "POST",
        url: "Feedback.aspx/SaveData",
        data: json ,
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        success: function (data) {
           'here is my success code'
        }
    });
}

so i just want to know how to write the code in the server side with c# which will deserialize my json data to a feedback class.

my feedback class has same property like name,subject,email,message etc. so tell me how to fill the feedback class with deserialization.

please help me with code. thanks


You actually don't need to do anything. ASP.NET will do it for you. Just define your method with the appropriate parameters, and it will work automagically:

[WebMethod]
public static void SaveData(string Name, string Subject, string Email, string Message)
{
    // Do something
}

Or since you already have this class defined, you just need to wrap your javascript fields around an object:

var json = "{'msg':{'Name':'" + $("input[id*='txtName']").val() +
            "','Subject':'" + $("input[id*='txtSubject']").val() +
            "','Email':'" + $("input[id*='txtEmail']").val() +
            "','Message':'" + jQuery.trim($('.message').val()) + "'}}";

[WebMethod]
public static void SaveData(Feedback msg)
{
    // Do something
}


You can use System.Web.Script.Serialization.JavaScriptSerializer, check out the example at the end of this page.


Take a look at the following library:

Json.NET

You can deserialise the JSON like so:

string json = "{\"Name\":\"name\",\"Subject\":\"subject\",\"Email\":\"email\",\"Message\":\"message\"}";
FeedBack feedBack = Newtonsoft.Json.JsonConvert.DeserializeObject<FeedBack>(json);

....

public class FeedBack
{
    public string Name { get; set; }
    public string Subject { get; set; }
    public string Email { get; set; }
    public string Message { get; set; }
}

Or you can make use of the built in .NET Framework JSON serializer class:

JavaScriptSerializer Class

JavaScriptSerializer serializer = new JavaScriptSerializer();
FeedBack feedBack = serializer.Deserialize<FeedBack>(json);


try this: using System.Web.Script.Serialization;

feedback fb = new feedback ();

fb = JSONSerializer.ConvertFromJSON<feedback>(json);

class JSONSerializer
{
    public static string GetJSONString(object data)
    {
        JavaScriptSerializer serializer = new JavaScriptSerializer();
        return serializer.Serialize(data);
    }
    public static T ConvertFromJSON<T>(String json)
    {
        JavaScriptSerializer serializer = new JavaScriptSerializer();
        return serializer.Deserialize<T>(json);
    }
}
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号