Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to parse an array of json object in jquery

Tags:

json

jquery

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

like image 590
Linto P D Avatar asked May 08 '15 03:05

Linto P D


People also ask

How do you parse an array of JSON objects?

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.

How can I convert JSON to string?

Use the JavaScript function JSON. stringify() to convert it into a string. const myJSON = JSON. stringify(obj);

What does the JSON parse () method do when we initiate an Ajax request?

Description: Takes a well-formed JSON string and returns the resulting JavaScript value.

How do you parse an object in JavaScript?

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.


2 Answers

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)
})
like image 114
Diego ZoracKy Avatar answered Oct 09 '22 09:10

Diego ZoracKy


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'/>
like image 21
JGV Avatar answered Oct 09 '22 11:10

JGV