Json仅显示具有特定ID的对象 [英] Json show only object with a specific id
问题描述
我想知道如何将具有特定ID的json对象打印到div? 我的json文件如下所示(缩短了),并由我使用的网上商店程序自动生成:
I would like to know how to print a json object with a specific id to a div?! My json file looks like this (shortened) and is automatically generated by a webshop program I use:
"shipping":{
"methods":{
"core|13490|40699":{
"id":"core|13490|40699",
"price":{
"price_excl":"0.0000","price_incl":"0.0000"
}
},
"core|10292|40695":{
"id":"core|10292|40695",
"price":{
"price_excl":"21.0084","price_incl":"25.0000"
}
}
}
},
和我的脚本是这样的:
window.onload = function(){
$.getJSON('http://shop.com/cart/?format=json', function(data){
$.each(data.cart.shipping.methods, function(index, method){
$('<span></span>')
.html('<strong>' + method.price.price_incl + '</strong>')
.appendTo('.cart-shipping');
});
});
};
我想归档的是,仅显示ID为"core | 10292 | 40695"的运输方式的运输价格.使用代码,我会同时显示两个运输价格.此外,我想知道如何将这些价格格式化为实际"价格,而不是25.000.
What i try to archieve is that only the shipping price of shipping method with id "core|10292|40695" is shown. With the code I have both shipping prices are shown. Further I would like to know how to format those prices into ' real' prices instead of 25.000.
您可能看到的,我对JSON,jquery/ajax还是陌生的,但我愿意学习.以上是我搜索该网站的档案,但我的问题是我真的不知道.
I'm pretty new to JSON, jquery/ajax as you probably can see, but I'm willing to learn. The above I archieved with searching this site but my question is something I really can't figure out.
推荐答案
您是否尝试过使用简单的if语句?例如:
Have you tried using a simple if statement? Eg:
if (index == "core|10292|40695") {
$('<span></span>').html('<strong>' + method.price.price_incl + '</strong>').appendTo('.cart-shipping');
}
关于格式化货币,我确定有大量可用信息
In regards to formatting the currency, I'm sure there's plenty of information available
这篇关于Json仅显示具有特定ID的对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!