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 />';
});
You need to change the JSON string to a Javascript object. Use JSON.parse()
.
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;
<div id="message"></div>
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.