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?

1
  • Use parseJSON(). It's already part of jQuery. Commented Dec 14, 2012 at 19:46

1 Answer 1

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

    }

}
Sign up to request clarification or add additional context in comments.

3 Comments

i give the wrong in the second line<i>var categories = peticion.d;</i> Microsoft JScript runtime error: 'd[...].key' is null or not an object
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
Now you should look at the response in the network tab. Like this: i.sstatic.net/1QmML.jpg

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.