0

I have a json string {"email" : "Hello", "username" : "Not taken"}

how do I iterate through this json to display the text (Like Hello)

I want to display the following text

Email : Hello
Username : Not taken

I tried the following:

    arr = json
    $.each(arr, function(k, v) {
      message += k + ':' + v + '<br />';  
    });
Musa
  • 96,336
  • 17
  • 118
  • 137
jewelwast
  • 2,657
  • 4
  • 17
  • 13
  • 4
    What happened when you tried? – SLaks Feb 28 '13 at 20:07
  • 1
    You'll have to parse the json first (if it is actually json). – Musa Feb 28 '13 at 20:08
  • possible duplicate of [I have a nested data structure / JSON, how can I access a specific value?](http://stackoverflow.com/questions/11922383/i-have-a-nested-data-structure-json-how-can-i-access-a-specific-value) – Felix Kling Feb 28 '13 at 20:14

1 Answers1

0

You need to change the JSON string to a Javascript object. Use JSON.parse().

Demo: jsFiddle

Output:

output

Script:

var json = '{"email" : "Hello", "username" : "Not taken"}',
    arr = window.JSON.parse( json ),
    message = '';

$.each(arr, function(k, v) {
    message += k + ': ' + v + '<br />';  
});

document.getElementById( 'message' ).innerHTML = message;

HTML:

<div id="message"></div>
ThinkingStiff
  • 64,767
  • 30
  • 146
  • 239