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

Posted by gnomixa on Stack Overflow See other posts from Stack Overflow or by gnomixa
Published on 2011-01-17T18:28:01Z Indexed on 2011/01/17 20:53 UTC
Read the original article Hit count: 234

Filed under:
|
|
|

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 error ' + 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";
}

© Stack Overflow or respective owner

Related posts about c#

Related posts about jQuery