Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

parse json string

I have the following Json string

{ "Users" : [ 
    { "Name" : "user99",
      "Value" : "test"
    },
    { "Name" : "test2",
      "Value" : "test"
    }
 ] 
}

I am trying to parse it and print out each name and value - what is the easiest way to do this ? I tried jQuery.parseJSON but I do not know how to use it I guess

Sample code would be great

like image 402
G-Man Avatar asked Feb 21 '11 20:02

G-Man


1 Answers

var json = '{"Users":[{"Name":"user999","Value":"test"},{"Name":"test2","Value":"test"}]}';

var json_parsed = $.parseJSON(json);

for (var u = 0; u < json_parsed.Users.length; u++){
    var user = json_parsed.Users[u];
    $('body').append($('<p>').html('User: '+user.Name+'<br />Value: '+user.Value));
}

Results in:

<p>User: user999<br />Value: test</p>
<p>User: test2<br />Value: test</p>

jsFiddle Example: http://jsfiddle.net/bradchristie/XtzjZ/1/

like image 59
Brad Christie Avatar answered Oct 18 '22 01:10

Brad Christie