Sandra Schlichting
Sandra Schlichting

Reputation: 25986

How to iterate over this hash?

I have this code

$.ajax({
    type: "GET",
    url: "/cgi-bin/ajax.pl",
    contentType: "application/json; charset=utf-8",
    dataType: "json",
    data: { "id" : id },

    // ...

    success: function(result){
    if (result.error) {
        alert('result.error: ' + result.error);
    } else {
        printObject(result);
    }
    }
});

where printObject outputs

responseText: Content-Type: application/json; charset=utf-8

{"276":"{\"var1\":[\"he\"],\"var2\":[\"he\",\"ki\",\"mt\"],\"var3\":\"07/06-2011\",\"var4\":[],\"var5\":\"ind\"}", ...

Question

How do I iterate over this hash?

Each array should just be treated as one variable. Ie. var2 would be he,ki,mt.

Upvotes: 2

Views: 347

Answers (1)

Teja Kantamneni
Teja Kantamneni

Reputation: 17472

The result you got should already be parsed as json and it is available in result object. Just do result.276.var2 to get the array.

Upvotes: 2

Related Questions