Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to read json response as name value pairs in JQuery

I want to read json response as name and value pairs in my JQuery code. Here is my sample JSON response that I return from my java code:

String jsonResponse = "{"name1":"value1", "name2:value2"};

in my JQuery, if I write jsonResponse.name1, I will get value as "value1". Here is my JQuery code

$.ajax({
    type: 'POST',
    dataType:'json',
    url: 'http://localhost:8080/calculate',
    data: request, 
    success: function(responseData) {
        alert(responseData.name1);
    },
    error: function(XMLHttpRequest, textStatus, errorThrown) {
        //TODO  
    }
});

Here I want to read "name1" from jsonResponse instead of hardcoding in JQuery. Something like looping throug the response getting each name and value. Any suggestions?

like image 719
jgg Avatar asked Oct 04 '10 20:10

jgg


People also ask

What is parseJSON in jQuery?

parseJSON( json )Returns: String or Number or Object or Array or Booleanversion deprecated: 3.0. Description: Takes a well-formed JSON string and returns the resulting JavaScript value.

How to add value in JSON object in jQuery?

jQuery: Select values from a JSON object using jQuery Select values from a JSON object using jQuery. JavaScript Code : colors = { "color1": "Red", "color2": "Green", 'color3': "Blue" }; $. each(colors, function(key, value) { $('#divSelect').


1 Answers

success: function(responseData) {
    for (var key in responseData) {
        alert(responseData[key]);
    }
}

It is important to note that the order in which the properties will be iterated is arbitrary and shouldn't be relied upon.

like image 113
Darin Dimitrov Avatar answered Sep 20 '22 12:09

Darin Dimitrov