0

I know this is one of the most commonly asked questions in ASP.net but my scenario is a bit different.

I have a json object which I am converting to a string and passing it to web method via jquery ajax.

function Save() {
            var content = $('#radGridSMT_ctl00 tr').not('#radGridSMT_ctl00 thead tr');
            var header = $('#radGridSMT_ctl00_Header thead tr');
            var result = [];
            for (var i = 0; i < content.length - 1; i++) {
                item = {}
                var headerName;
                item[header[0].children[0].textContent] = content[i].children[0].textContent;
                item[header[0].children[1].textContent] = content[i].children[1].textContent;
                item[header[0].children[2].textContent] = content[i].children[2].textContent;
                item[header[0].children[3].textContent] = content[i].children[3].textContent;
                item[header[0].children[4].textContent] = content[i].children[4].textContent;
                item[header[0].children[5].textContent] = content[i].children[5].textContent;
                item[header[0].children[6].textContent] = content[i].children[6].textContent;
                item[header[0].children[7].textContent] = content[i].children[7].textContent;
                item[header[0].children[8].textContent] = content[i].children[8].textContent;
                item[header[0].children[9].textContent] = content[i].children[9].textContent;
                item[header[0].children[10].textContent] = content[i].children[10].textContent;
                item[header[0].children[11].textContent] = content[i].children[11].textContent;
                item[header[0].children[12].textContent] = content[i].children[12].textContent;
                result.push(item);
            }
            result = JSON.stringify({ data: result });
            $.ajax({
                type: "POST",
                url: "SMTRequiredHour.aspx/SaveData",
                contentType: "application/json; charset=utf-8",
                dataType: "json",
                data: result,
                success: function (response) {

                },
                failure: function (response) {

                }
            });
        }

Now when I receive it in web method is comes in the form of an object.

    [WebMethod]
    public static void SaveData(object data)
    {
        CreateDatatable(data);
        var d = initialData;
        var obj = JsonConvert.DeserializeObject(data);
    }

The JsonConvert.DeserializeObject takes a string not an object and that's the problem. I am unable to convert JSON object to string.

I can't create strong type object because the columns in the json object are generate dynamically and may not be same every time.

How to parse JSON object to a string so that I can deserialize it.

user728630
  • 2,025
  • 5
  • 22
  • 35
  • method `JsonConvert.DeserializeObject(String)` is to Deserializes the JSON to a .NET object ? You already have the `object data`, am i right? – Jacky Feb 19 '16 at 07:27
  • Did you try use `string data` instead of `object data`? So you will be able deserialize your JSON – feeeper Feb 19 '16 at 07:30
  • Yes I tried accepting the JSON string as a string in web method but then the ajax call doesn't hit the web method. – user728630 Feb 19 '16 at 07:42
  • @jacky: Yes, i have the data but in the form of object. I need to it in the form of string so that i can pass it to DeserializeObject(string). – user728630 Feb 19 '16 at 07:43
  • 1
    @Satindersingh: I explicitly said that I can't create strong type objects. – user728630 Feb 19 '16 at 07:46
  • Possible duplicate of [Pass JSON Object To MVC Controller as an Argument](http://stackoverflow.com/questions/12069171/pass-json-object-to-mvc-controller-as-an-argument) – tede24 Feb 19 '16 at 07:50
  • @user728630 try to remove `contentType` and `dataType` from your ajax call and change `object data` to `string data` – feeeper Feb 19 '16 at 07:55
  • 1
    in order to convert object to json, you can try Newtonsoft NuGet package. (http://www.newtonsoft.com/json). By the way, if your web method is a void method, I dont think there is anything return back to client after web method has been called. – Chinh Phan Feb 19 '16 at 07:59
  • @feeper: I tried that just now and web method doesn't get called. – user728630 Feb 19 '16 at 08:00
  • @ChinhPhan: That worked ! Lol I was using Newtonsoft already but did not know that I should serialize it first. Thanks a lot ! – user728630 Feb 19 '16 at 08:04
  • Great. you are welcome. – Chinh Phan Feb 19 '16 at 08:13

1 Answers1

0

What does data.ToString() return?

If it doesn't solve it, try to send it as text from the JavaScript side. You could use data: JSON.stringify(result), And then set the data type to string in the web method.

Terje Kolderup
  • 367
  • 3
  • 12