2

I have a POST request which receives a JSON String as a result. The fields, the values, or how deep the array is? These all are unknown. So, I need to loop through each of the json value, its index and its value and perform actions based on it.

$.post(
    "test.php", 
    { action: 'someaction', param: '2' },
    function(data) {
      //now data is a json string
      $.each(data, function() {
       key = data.key; //i need to retrieve the key
       value = data.value; //i need to retrieve the value also

       //now below here, I want to perform action, based on the values i got as key and values
      }
    },
    "json"
);

How can i get the values of JSON seperated as key and value?

3 Answers 3

5

Sorry, guys, but I solved it myself. Please dont be angry with me. (I will delete the question if it is required by the community.)

$.post(
    "test.php", 
    { action: 'someaction', param: '2' },
    function(data) {
      //now data is a json string
      $.each(data, function(key,value) {
       alert(key+value);
       //now below here, I want to perform action, based on the values i got as key and values
      }
    },
    "json"
);
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for posting your solution. You should select this as answer.
3

Well, JSON gets parsed into JavaScript objects. You can traverse them using for...in:

for(var key in data) {
    if(data.hasOwnProperty(key)) {
        var value = data[key];
    }
}

Comments

0

This will help: Iterating a JavaScript object's properties using jQuery

Comments

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.