I have the following array of json object
[{"id":10,"name":"sinu"},{"id":20,"name":"shinto"}]
How can i parse this using jquery and get names, id of each one.
thanks in advance
Parsing JSON Data in JavaScript In JavaScript, you can easily parse JSON data received from the web server using the JSON. parse() method. This method parses a JSON string and constructs the JavaScript value or object described by the string. If the given string is not valid JSON, you will get a syntax error.
Use the JavaScript function JSON. stringify() to convert it into a string. const myJSON = JSON. stringify(obj);
Description: Takes a well-formed JSON string and returns the resulting JavaScript value.
parse() JSON parsing is the process of converting a JSON object in text format to a Javascript object that can be used inside a program. In Javascript, the standard way to do this is by using the method JSON. parse() , as the Javascript standard specifies.
var data = [{"id":10,"name":"sinu"},{"id":20,"name":"shinto"}];
jQuery(data).each(function(i, item){
$('#output').append(item.id + ' ' + item.name + '<br>');
})
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="output"></div>
var data = [{"id":10,"name":"sinu"},{"id":20,"name":"shinto"}];
jQuery(data).each(function(i, item){
console.log(item.id, item.name)
})
Use json2.js and parse your array of json object. See the working code below,
var data = '[{"id":10,"name":"sinu"},{"id":20,"name":"shinto"}]';
var obj = JSON.parse(data);
var append= '';
$.each(obj, function(i, $val)
{
append+= $val.id + ',';
});
$('#ids').text(append);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<script src="https://github.com/douglascrockford/JSON-js/blob/master/json2.js"></script>
<label id='ids'/>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With