If you want to create a WCF operation to receive this JSON input, you will need to define a data contract that will map to this input. There are several tools that do this automatically, including the one I wrote a while ago at http://jsontodatacontract.azurewebsites.net/ (more about how this tool was written on in this blog post ). The tool generated this class, which you can use:
// Type created for JSON at <<root>> [System.Runtime.Serialization.DataContractAttribute()] public partial class Person { [System.Runtime.Serialization.DataMemberAttribute()] public int age; [System.Runtime.Serialization.DataMemberAttribute()] public string name; [System.Runtime.Serialization.DataMemberAttribute()] public string[] messages; [System.Runtime.Serialization.DataMemberAttribute()] public string favoriteColor; [System.Runtime.Serialization.DataMemberAttribute()] public string petName; [System.Runtime.Serialization.DataMemberAttribute()] public string IQ; }
Then you need to define an operation contract to get this. Since JSON needs to go into the request body, the most natural HTTP method to use is POST , so you can define the operation as shown below: the "POST" method and the "Bare" style (which means that your JSON directly displays the parameter). Note that you can even omit the Method and BodyStyle , since "POST" and WebMessageBodyStyle.Bare are their default values, respectively).
[WebInvoke(Method = "POST", BodyStyle = WebMessageBodyStyle.Bare)] public Person FindPerson(Peron lookUpPerson) { Person found = null;
Now in the method you have the input mapped to lookupPerson . How you implement the logic of your method is up to you.
Update after comment
One example of calling a service using JavaScript (via jQuery) can be found below.
var input = '{ "age":100, "name":"foo", "messages":["msg 1","msg 2","msg 3"], "favoriteColor" : "blue", "petName" : "Godzilla", "IQ" : "QuiteLow" }'; var endpointAddress = "http://your.server.com/app/service.svc"; var url = endpointAddress + "/FindPerson"; $.ajax({ type: 'POST', url: url, contentType: 'application/json', data: input, success: function(result) { alert(JSON.stringify(result)); } });
carlosfigueira
source share