I am returning a JSON object (as a String) through a servlet. The JSON object looks like this:
{
"3": "Martin Luther",
"30": "Boris Becker",
"32": "Joseph Goebels",
"19": "Leonardo Da Vinci"
}
My jQuery looks like this (the submission of data is correct because I get a proper looking result from the servlet):
$.ajax({
type: "GET",
url: "MyServlet",
data: queryString + "count=" + variables,
success: function(resultObj) {
$.each(resultObj, function(key, value) {
$("#resultCount").html(key + ", " + value);
});
}
});
However when I try to print the results, that is the variables key and value, I get a number for the key but not a number from the JSONObject and an empty string instead of the value.
Essentially the question is about how to “extract” the information from a JSON object.
Your JSON is not an array. It should look like this:
or even better:
Then you can loop: