1

How do I map JSON object to C# object ?

  var Data = JSON.stringify({
                    FirstName: "sdfsdf",
                    LastName: "sdfsdf", Age: "sdfsdf", Descrp: "sdfsdf"
                });

for example converting Data to C# class with same properties.
I tried this :

 var Data = JSON.stringify({
                FirstName: "sdfsdf",
                LastName: "sdfsdf", Age: "sdfsdf", Descrp: "sdfsdf"
            });
            $.ajax({
                type: "POST",
                url: "Services/CRUD.asmx/CreatePerson",
                data: Data,
                contentType: "application/json; charset=utf-8",
                dataType: "json",
                success: function (msg) {
                    alert(msg.d);

                }
            });

but when I checked with firebug this error returned :

{"Message":"Invalid web service call, missing value for parameter: \u0027PersonObj\u0027.","StackTrace":"   at System.Web.Script.Services.WebServiceMethodData.CallMethod(Object target, IDictionary`2 parameters)\r\n   at System.Web.Script.Services.WebServiceMethodData.CallMethodFromRawParams(Object target, IDictionary`2 parameters)\r\n   at System.Web.Script.Services.RestHandler.InvokeMethod(HttpContext context, WebServiceMethodData methodData, IDictionary`2 rawParams)\r\n   at System.Web.Script.Services.RestHandler.ExecuteWebServiceCall(HttpContext context, WebServiceMethodData methodData)","ExceptionType":"System.InvalidOperationException"}
Shahin
  • 12,543
  • 39
  • 127
  • 205

3 Answers3

4

You can definitely do that, but you need to make sure the client-side object matches your server-side class structure, property names, and parameter name exactly.

It sounds like your server-side method is expecting a PersonObj parameter, but you aren't structuring it that way on the client-side. Try something like this:

var Data = JSON.stringify({ 
  PersonObj: {
    FirstName: 'foo',
    LastName: 'bar',
    Age: 99,
    Descrp: 'foo person'
  }
});   

I've written about doing this in-depth here: http://encosia.com/using-complex-types-to-make-calling-services-less-complex/

Dave Ward
  • 59,815
  • 13
  • 117
  • 134
1

What type is your PersonObj parameter? Assuming it is a Person like this:

public class Person
{
   public string FirstName { get;set;}
   public string LastName { get;set;}
   public string Age { get;set;}
   public string Descrp { get;set;}
}

Your WebService method signation should be: public static returntype CreatePerson(string jsonPersonObject) {}

And your Ajax data parameter should be:

$.ajax({
       type: "POST",
       url: "Services/CRUD.asmx/CreatePerson",
       data: {'jsonPersonObject' : Data },
       contentType: "application/json; charset=utf-8",
       dataType: "json",
       success: function (msg) {
          alert(msg.d);
       }
});

And then within CreatePerson you will have to parse the jsonPersonObject into a Person.

Common ways to do this are with Json.NET or JavaScriptSerializer from System.Web.Extensions.dll. More in-depth info can be found here or here

Community
  • 1
  • 1
s_hewitt
  • 4,252
  • 24
  • 24
  • You shouldn't manually serialize or deserialize objects inside ASMX services. The framework will do this for you automatically if you take advantage of it (set the input parameters to the type you want it to deserialize to): http://encosia.com/asp-net-web-services-mistake-manual-json-serialization/ – Dave Ward Jul 26 '11 at 19:05
  • Good to know. Since very little context for the question was given, I'll leave the answer so when it's needed it will be available. – s_hewitt Jul 26 '11 at 20:06
1

do this:

 var context = new object();
 context.firstname = "sdfsdf";
 context.lname = "sdfsdf";
 context.age="sdfsdf";
 context.descrp= "sdfsdf"
 ajaxCall("functionname", context, ajaxCallSuccess, ajaxCallFailure, "pagename.aspx");

use json2 plugin to stringify your context obeject and pass this way

function ajaxCall(funtionname, reqObject, successFn, errorFn, theaspxPage) {

    var dataObject = JSON.stringify(reqObject);

    //Call the page method  
    $.ajax({
        async: false,
        type: "POST",
        url: aspxPage + "/" + fn,
        contentType: "application/json;",
        data: "{'reqObject':" + dataObject + "}",
        dataType: "json",
        success: successFn,
        error: errorFn
    });
};

make sure your c# method is static and accepts an object if you wanna pass as parameters just modify the ajaxcall data:dataobject, instead of reqobject:''

abhijit
  • 1,958
  • 3
  • 28
  • 39