0

I am calling a webservice through ajax call and pass array of object to it after stringify it.

Data after stringifying

data = '[{"para1":"pic 1","para2":"drop 1"},{"para1":"pic 2","para2":"drop 2"}]'

 $.ajax({
        type: 'POST',
        url: "path to url/method",
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        data: JSON.stringify({
            "data": data
        }),

How to receive it and parse in C# webservice.

    [WebMethod]
    [ScriptMethod(ResponseFormat = ResponseFormat.Json)]
    public string insertroute(string data){
       //how to parse data here
    }
ghost...
  • 975
  • 3
  • 16
  • 33
  • parsing can be done pretty easily with a library: http://stackoverflow.com/questions/4109807/parsing-json-data-with-c-sharp – Michael B. Mar 31 '15 at 18:36

2 Answers2

1

Your service method will receive the data as a string. You can use the following code to deserialize into a list of objects.

var serializer = new JavaScriptSerializer();
var result = serializer.Deserialize(mail, typeof(List<object>));

As a practice, it would be better if you made a model that matches your json object and passed that into the typeof(List<model>)

Praveen Paulose
  • 5,741
  • 1
  • 15
  • 19
0

You need to Convert a string to JSON? You can use something like

var json = JObject.Parse(data);

JObject is from

Newtonsoft.Json

But, you can just send JSON and deal with JSON and sendback JSON no need to stringify it.

Thanks

Steve

Steve Drake
  • 1,968
  • 2
  • 19
  • 41