Jquery Parse Json Example

How to convert json into jquery object

September 23, 2020

Simple Json

var obj = $.parseJSON('{ "Country": "India","State": "Maharashtra","City":"Mumbai" }');
console.log(obj.Country);
console.log(obj.State);
console.log(obj.City);

Array Json (Example 1)

var obj = $.parseJSON('{ "List": [{ "Country": "India","State": "Maharashtra","City":"Mumbai" },{ "Country": "India","State": "Gujarat","City":"Surat" }] }');
 
// FIRST ELEMENT
console.log(obj.List[0].Country);
console.log(obj.List[0].State);
console.log(obj.List[0].City);
 
// SECOND ELEMENT
console.log(obj.List[1].Country);
console.log(obj.List[1].State);
console.log(obj.List[1].City);
 
//LOOP
for (var i = 0; i < obj.List.length; i++)
{
    console.log(obj.List[i].Country);
    console.log(obj.List[i].State);
    console.log(obj.List[i].City);
}

Array Json (Example 2)

var obj = $.parseJSON('[{ "Country": "India","State": "Maharashtra","City":"Mumbai" },{ "Country": "India","State": "Gujarat","City":"Surat" }]');
 
// FIRST ELEMENT
console.log(obj[0].Country);
console.log(obj[0].State);
console.log(obj[0].City);
 
// SECOND ELEMENT
console.log(obj[1].Country);
console.log(obj[1].State);
console.log(obj[1].City);
 
//LOOP
for (var i = 0; i < obj.length; i++) {
    console.log(obj[i].Country);
    console.log(obj[i].State);
    console.log(obj[i].City);
}

Post Comments(0)

Leave a reply

Will not be displayed in comment box .

Loading...