Sunday 25 August 2019

javascript - 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.


Answer



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.


No comments:

Post a Comment

php - file_get_contents shows unexpected output while reading a file

I want to output an inline jpg image as a base64 encoded string, however when I do this : $contents = file_get_contents($filename); print &q...