简体   繁体   中英

how to deserialize Json-Object in asp.net

I am able to get the element to Code-Behind by Using the Ajax-Call But I am unable to Deserialize the element in Code-Behind in asp.net

This is my Ajax call

function responseData2() {

    var demodata = [];

    var oListbox = $("#submitlistbox2").each(function () {
        var data = $(this).text() + " " + $(this).val() + "\n";
        alert("The Names are: " + data);

        demodata.push({
            var_name_data: data,
        });
    });

    $.ajax({
        url: "url",
        type: "POST",
        contentType: "application/json; charset=utf-8",
        cache: false,
        data: "{ 'selectedJobSheet': '" + demodata + "'}",
        success: function (data) {
            data = $.parseJSON(data.d);
            alert(data);
            alert("success");
        },
        error: function (response) {
            alert(response);
            alert("error");
        }
    });
}

My Code-Behind: How to retrive the element In Code behind:My ajax call returning data in Single string value "[object Object]"

[WebMethod]
[ScriptMethod(ResponseFormat = ResponseFormat.Json)]
public static object Details4(string selectedJobSheet)
{    
    try
    {
        string constr = ConfigurationManager.ConnectionStrings["Constr"].ConnectionString;
        using (SqlConnection con = new SqlConnection(constr))
        {
            using (SqlCommand cmd = new SqlCommand("select customer_id,first_name from jobsheetDetails", con))
            {
                string _data = "";
                cmd.CommandType = CommandType.Text;
                SqlDataAdapter da = new SqlDataAdapter(cmd);
                DataSet ds = new DataSet();
                da.Fill(ds);
                if (ds.Tables[0].Rows.Count > 0)
                {
                    _data = JsonConvert.SerializeObject(ds.Tables[0]);
                }
                return _data;
            }

        }
    }
    catch (Exception)
    {
        throw;
    }
}

How to deserialize the element and how bind the element to list or array

Consider the following code.

function responseData2() {
  var demodata = [];
  var oListbox = $("#submitlistbox2").each(function() {
    var data = $(this).text() + " " + $(this).val() + "\n";
    alert("The Names are: " + data);
    demodata.push({
      var_name_data: data,
    });
  });
  $.ajax({
    url: "url",
    type: "POST",
    contentType: "application/json; charset=utf-8",
    cache: false,
    data: { selectedJobSheet: demodata },
    success: function(results) {
      data = $.parseJSON(results.d);
      alert(data);
      alert("success");
    },
    error: function(response) {
      alert(response);
      alert("error");
    }
  });
}

This will pass the Object to your post data. The way you had constructed it, you were passing a string and would not contain the data you expected. .NET can just read this as a Request like normal.

Since demodata is an Array of Objects, when you write:

"{ 'selectedJobSheet': '" + demodata + "'}"

This will create a String that is:

{ 'selectedJobSheet': '[object Object],[object Object],[object Object]'}

So it will not be passing the details you want. You can try it with $.param() :

data: $.param({ 'selectedJobSheet': demodata });

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM