我有一个像这样的JSON:
var parse = [
{
"variation_id":"34",
"attributes":{
"attribute_pa_rozmiar":"m"
},
"image_src":"http://localhost/Fraise-WP/wp-content/uploads/2013/06/Jellyfish-300x300.jpg",
"image_link":"http://localhost/Fraise-WP/wp-content/uploads/2013/06/Jellyfish.jpg",
"image_title":"Jellyfish",
"price_html":"",
"availability_html":"<p class=\"stock \">10 w magazynie</p>",
"sku":"",
"weight":"4 kg",
"dimensions":"",
"min_qty":1,
"max_qty":"10",
"backorders_allowed":false,
"is_in_stock":true,
"is_downloadable":false,
"is_virtual":false,
"is_sold_individually":"no"
},
{
"variation_id":"33",
"attributes":{
"attribute_pa_rozmiar":"s"
},
"image_src":"http://localhost/Fraise-WP/wp-content/uploads/2013/06/Desert-300x300.jpg",
"image_link":"http://localhost/Fraise-WP/wp-content/uploads/2013/06/Desert.jpg",
"image_title":"Desert",
"price_html":"",
"availability_html":"<p class=\"stock \">5 w magazynie</p>",
"sku":"",
"weight":"4 kg",
"dimensions":"",
"min_qty":1,
"max_qty":"5",
"backorders_allowed":false,
"is_in_stock":true,
"is_downloadable":false,
"is_virtual":false,
"is_sold_individually":"no"
}
][
{
"variation_id":"34",
"attributes":{
"attribute_pa_rozmiar":"m"
},
"image_src":"http://localhost/Fraise-WP/wp-content/uploads/2013/06/Jellyfish-300x300.jpg",
"image_link":"http://localhost/Fraise-WP/wp-content/uploads/2013/06/Jellyfish.jpg",
"image_title":"Jellyfish",
"price_html":"",
"availability_html":"<p class=\"stock \">10 w magazynie</p>",
"sku":"",
"weight":"4 kg",
"dimensions":"",
"min_qty":1,
"max_qty":"10",
"backorders_allowed":false,
"is_in_stock":true,
"is_downloadable":false,
"is_virtual":false,
"is_sold_individually":"no"
},
{
"variation_id":"33",
"attributes":{
"attribute_pa_rozmiar":"s"
},
"image_src":"http://localhost/Fraise-WP/wp-content/uploads/2013/06/Desert-300x300.jpg",
"image_link":"http://localhost/Fraise-WP/wp-content/uploads/2013/06/Desert.jpg",
"image_title":"Desert",
"price_html":"",
"availability_html":"<p class=\"stock \">5 w magazynie</p>",
"sku":"",
"weight":"4 kg",
"dimensions":"",
"min_qty":1,
"max_qty":"5",
"backorders_allowed":false,
"is_in_stock":true,
"is_downloadable":false,
"is_virtual":false,
"is_sold_individually":"no"
}
]
我需要的是在attributes
内运行搜索,查找此对象是否具有,例如。 attribute_pa_rozmiar=m
并获得相应的image_src
。很抱歉这样一个臃肿的JSON,但这是我能把它拿出来的唯一方法。
答案 0 :(得分:0)
我尝试根据找到的exaples运行$ .each和$ .map函数,但都没有。
请尝试$.grep
:
var filtered = $.grep(parse, function(item) {
return item.attributes.attribute_pa_rozmiar == "m";
});
var images = $.map(filtered, function(item) {
return item.image_src;
});
答案 1 :(得分:0)
试试这个..它使用$ .each并且工作正常
$.each(parse, function(i, item) {
$.each(item, function(j, innerItem){
// perform search on innerItem.a or innerItem.b
});
});
<强>更新强>
根据您的新更新,您的JSON首先无效。你有额外的] [在可变数据的中间。 我创建了另一个JSFiddle来解决你的问题。参见工作演示:
http://jsfiddle.net/vendettamit/kjzPC/
PS - 当您必须发布此类长代码时,您可以使用在线格式化程序然后发布它。 :)