February 26, 2013
How to access this kind of Json using jquery?
Question by Amit Prajapati
i am beginer in Jquery and javascript can any one help me to how to acces this kind of json data using jquery.if any one have idea to better way to access this data than please help me. this json data are come form remote url.
{
"1": {
"id": "1",
"name": "name1",
"large_name": "http://critiques-demo.she.com/beauty/makeuptool/images/models/model1.jpg",
"thumb_name": "http://critiques-demo.she.com/beauty/makeuptool/images/models/model1_thumb.jpg"
},
"2": {
"id": "2",
"name": "name2",
"large_name": "http://critiques-demo.she.com/beauty/makeuptool/images/models/model2.jpg",
"thumb_name": "http://critiques-demo.she.com/beauty/makeuptool/images/models/model2_thumb.jpg"
},
"3": {
"id": "3",
"name": "name3",
"large_name": "http://critiques-demo.she.com/beauty/makeuptool/images/models/model3.jpg",
"thumb_name": "http://critiques-demo.she.com/beauty/makeuptool/images/models/model3_thumb.jpg"
},
"4": {
"id": "4",
"name": "name4",
"large_name": "http://critiques-demo.she.com/beauty/makeuptool/images/models/model4.jpg",
"thumb_name": "http://critiques-demo.she.com/beauty/makeuptool/images/models/model4_thumb.jpg"
},
"5": {
"id": "8",
"name": "Name8",
"large_name": "http://critiques-demo.she.com/beauty/makeuptool/images/models/model8.jpg",
"thumb_name": "http://critiques-demo.she.com/beauty/makeuptool/images/models/model8_thumb.jpg"
}
}
Answer by Sheikh Heera
Use $.jsonParse to parse the json string then loop using $.each
$.each(data, function(k, v){
document.write(v['id'] + ' ' + v['name']+'<br />');
});
Output:
1 name1
2 name2
3 name3
4 name4
8 Name8
In above example k
represents the key and v
represnts value (each object
) and in this case the first key
and object
is ( "1"
is key and {...}
is object )
"1": {
"id": "1",
"name": "name1",
"large_name": "http://critiques-demo.she.com/beauty/makeuptool/images/models/model1.jpg",
"thumb_name": "http://critiques-demo.she.com/beauty/makeuptool/images/models/model1_thumb.jpg"
}
Using v['id']
you can retrive the 1
, v['name']
will return the name1
and so on. If you use $.getJSON then you don’t need to parse it because it’ll be parsed by jQuery
and you can use it like
$.getJSON('url', function(data){
$.each(data, function(k, v){
document.write(v['id'] + ' ' + v['name']+'<br />');
});
});