2

This is my web service

[WebMethod]
[ScriptMethod(ResponseFormat = ResponseFormat.Json)]
public Dictionary<string,List<string>> GetCategorias()
{
    var diccionario = new Dictionary<string, List<string>>();
    var categoria = "Recursos Humanos";
    diccionario.Add(categoria,new List<string>());
    diccionario[categoria].Add("Busqueda de recursos");
    diccionario[categoria].Add("Busqueda de recursos humanos");

    var categoria1 = "Informatica";
    diccionario.Add(categoria1, new List<string>());
    diccionario[categoria1].Add("IT");
    diccionario[categoria1].Add("Departamento de Insfractructura");

    //var serializer = new JavaScriptSerializer();
    //string json = serializer.Serialize((object)diccionario);

    return diccionario;
}

I received the dictionary in Javascript as:

 function get_Categorias_Comunidades() {
        $.ajax({
            type: "POST",
            url: "Lista_Categoria_comunidad.asmx/GetCategorias",
            data: "{}",
            contentType: "application/json; charset=utf-8",
            dataType: "json",
            success: llamada_Webservice,
            error: llamada_Error
        });
    }
    function llamada_Webservice(peticion) {
        debugger;

    }

How do I parse the keys and values to an array?

isNaN1247
  • 17,793
  • 12
  • 71
  • 118

1 Answers1

1

Something like this

function llamada_Webservice(peticion) {
var categories = peticion;
    for(item in categories{ // Data is saved in the variable named d if it's ASP.NET WebService
        var categoria = item; // The name
        var list = categories[item]; // The array that you could loop thru

    }

}
Simon Edström
  • 6,461
  • 7
  • 32
  • 52
  • i give the wrong in the second linevar categories = peticion.d; Microsoft JScript runtime error: 'd[...].key' is null or not an object – Lucas Valle Dec 14 '12 at 20:52
  • Try to remove the .d If you select the network tab in Chrome and inspect the response you could se the JSON structure. Paste it in your quesiton – Simon Edström Dec 14 '12 at 20:56
  • Now you should look at the response in the network tab. Like this: http://i.stack.imgur.com/1QmML.jpg – Simon Edström Dec 15 '12 at 08:25