Reputation: 14315
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"
}
}
Upvotes: 0
Views: 116
Reputation: 146201
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 />');
});
});
Upvotes: 1
Reputation: 78991
You can access the numeric keys as arrays like
console.log(dataJson[1].id);
Upvotes: 0
Reputation: 27364
You can use for
loop.
for(i = 0; i <= dataJson.length; i++)
{
console.log(dataJson[i].name);
}
Upvotes: 0
Reputation: 298216
Once you've parsed the JSON, it's just a regular JS object:
for (var id in data) {
var thing = data[id];
console.log(thing.name);
}
Upvotes: 1