Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Print JSON object with jQuery

Tags:

json

jquery

I have some JSON data looking like this:

"Extras": {
  "T01": "Value 1",
  "T02": "Value 2",
  "T03": "Value 3",
  // etc.
}

I need each of these values in a list, and so I've tried this:

$.each(data.result, function(i){
  $("#result").append('<li>'+data.result.Extras[]+'</li>');
});

Which obviously doesn't work, I just can't seem to figure out what to do. I've tried data.result.Extras with no luck (as I just get [object][Object]).

Any ideas what I can do to get all of the values in a list? Thanks!

like image 959
pshoeg Avatar asked May 15 '26 22:05

pshoeg


1 Answers

You are iterating the data.result, I believe you need to iterate the Extras.

$.each(data.result.Extras, function(k, v){
  $("#result").append('<li>'+v+'</li>');
});

Note: In the above function k is key and v is value in the object Extras. For ex: For the first iterate k would be T01 and v would be Value 1.

The above should produce and output of

<ul id="result">
   <li>Value 1</li>
   <li>Value 2</li>
   <li>Value 3</li>
</ul>
like image 193
Selvakumar Arumugam Avatar answered May 18 '26 15:05

Selvakumar Arumugam



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!