March 28, 2011

Looping through Dynamic JSON String

Question by Starx

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?

Answer by Starx

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"
);

Author: Nabin Nepal (Starx)

Hello, I am Nabin Nepal and you can call me Starx. This is my blog where write about my life and my involvements. I am a Software Developer, A Cyclist and a Realist. I hope you will find my blog interesting. Follow me on Google+

...

Please fill the form - I will response as fast as I can!