开发者

Getting the data inside the C# web service from Jsonified string

In my JS I use Jquery's $ajax functions to call the web service and send the jsonified data to it. The data is in the following format:

var countries = {

   "1A": {
        id: "1A",
        name: "Andorra"        
    },
    "2B": {
        id: 2B
        name: "Belgium"       
    },

    ..etc
};

var jsonData = JSON.stringify({ data: data });

//then $ajax is called and it makes the call to the c# web service


On the c# side the web service needs to unpack this data, currently it comes in as string[][] data type. How do I convert it to the format so I can refer to the properties such as

.id and .name? Assuming I have a class called Sample with these properties? Thanks!

EDIT:

Here is my JS code:

var jsonData = JSON.stringify(countries);

     $.ajax({
         type: 'POST',
         url: 'http://localhost/MyService.asmx/Foo',
         contentType: 'application/json; charset=utf-8',
         data: jsonData,
         success: function (msg) {                 
             alert(msg.d);
         },
         error: function (xhr, status) {
              switch (status) {
                 case 404:
                     alert('File not found');
                     break;
                 case 500:
                     alert('Server error');
                     break;
                 case 0:
                     alert('Request aborted');
                     break;
                 default:
                     alert('Unknown 开发者_如何学Cerror ' + status);
             } 
         }
     });

inside c# web service I have:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;
using System.Data;
using System.Collections;
using System.IO;
using System.Web.Script.Services;

[WebMethod]
[ScriptMethod]
public string Foo(IDictionary<string, Country> countries)   
{

    return "success";
}


I don't know what string[][] you are talking about and where does it come from but it is better to work with strong types. So define a custom type that will model your data:

public class Country
{
    public string Id { get; set; }
    public string Name { get; set; }
}

and then have the web method take a dictionary:

[WebMethod]
[ScriptMethod]
public string Foo(IDictionary<string, Country> countries)
{
    // Here you will have countries["1A"].Name = "Andorra" and 
    // countries["2B"].Name = Belgi
    ...    
    return "success";
}

and then simply call this method (see UPDATE for proper data):

var data = {
   "1A": {
        id: "1A",
        name: "Andorra"        
    },
    "2B": {
        id: 2B
        name: "Belgi"       
    }
};

$.ajax({
    type: 'POST',
    url: '/myservice.asmx/Foo',
    contentType: 'application/json; charset=utf-8',
    data: { JSON.stringify(data) },
    success: function(msg) {
        alert(msg.d);
    }
});

UPDATE:

Actually the request should be formatted like this:

var data = {
    countries: {
        "1A": {
            id: "1A",
            name: "Andorra"
        },
        "2B": {
            id: "2B",
            name: "Belgium"
        }
    }
};


Have you tried the JavaScriptSerializer? As you said, assuming you have a class with these properties, it should/might be able to deserialize the JSON string to that type.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜