javascript JSON advanced
javascript JSON advanced
我找不到bug。也许你能帮助我。我的代码如下:
var data = {"product":[{"config":[{"id":"1","price":"100","sku":"10548796345","manufacturer":"Apple","name":"Product 1", "description":"Web site has two parts: the Site (which is what your site visitors see) and the Administrator (which is where you will want to do a lot of the site management). You need to log in to the Administrator separately. There is a link to the administrator on the top menu that you will see when you log .","cid":"1","qty":"102"}],"options":[{"color":[{"blue":"+10","count":"2"},{"red":"+20","count":"3"}]},{"size" :[{"S":"+10","count":"1"},{"M":"+20","count":"4"},{"L":"+30","count":"5"},{"XL":"+40","count":"2"}]}]},{"config":[{"id":"2","price":"100","sku":"10548796341","manufacturer":"Apple","name":"Product 2", "description":"Web site has two parts: the Site (which is what your site visitors see) and the Administrator (which is where you will want to do a lot of the site management). You need to log in to the Administrator separately. There is a link to the administrator on the top menu that you will see when you log in.","cid":"1","qty":"102"
}],"options":[{"color":[{"blue":"+10","count":"2"},{"red":"+20","count":"3"}]},{"size" :[{"S":"+10","count":"1"},{"M":"+20","count":"4"},{"L":"+30","count":"5"},{"XL":"+40","count":"2"}]}]}],"categories":[ {"id":1,"name":"Category 1", "description":"Category 1 description"}, {"id":2,"name":"Category 2", "description":"Category 2 description"}, {"id":3,"name":"Category 3", "description":"Category 3 description"}]};
复制并粘贴此代码到:http://json.parser.online.fr/
下面的代码是作品
data.categories.each(function(c){
var opt = new Option(c.name, c.id);
try {category_selector.add(opt, null)} catch (err) {category_selector.add(opt)}
});
为什么这段代码不像上面的代码那样工作(返回undefined):
data.product.each(function(p){
var el = new Element('div.preview'),
name = new Element('h3', {'html': '<a href="#!product/product?product_id='+parseInt(p.config.id)+'">' + p.config.name + '</a>'}).inject(el),
desc = new Element('span', {'html': p.config.description}).inject(name, 'after');
el.inject(container);
});
p。S
如果我将代码编辑为:
data.product.each(function(p, i){
var el = new Element('div.preview'),
name = new Element('h3', {'html': '<a href="#!product/product?product_id='+parseInt(p.config[i].id)+'">' + p.config[i].name + '</a>'}).inject(el);
el.inject(container);
});
它将只返回一个产品和控制台错误:p.config[i] is undefined…
p。2:
data.obj[1].config.each(function(p){
// [1] - return first product; [2] - return second; How to return all 1 and 2?
var el = new Element('div.preview'),
name = new Element('h3', {'html': '<a href="#!product/product?product_id='+parseInt(p.id)+'">' + p.name + '</a>'}).inject(el);
el.inject(container);
});
工作代码如下:
for (var i=0;i<data.product.length;i++) {
data.product[i].config.each(function(p){
var el = new Element('div.preview'),
name = new Element('h3', {'html': '<a href="#!product/product?product_id='+parseInt(p.id)+'">' + p.name + '</a>'}).inject(el);
el.inject(container);
});
}
谢谢。
data.product.each
将运行2次回调(因为内部有2个对象)。第一次,p将是配置对象。第二次,它将是options对象。
你正在做的事情像p.config.id
,这是没有意义的,当p是'选项'。似乎根本不需要使用each迭代器?一开始只使用var p = data.product
,去掉each
迭代
相关文章:
- jQuery匹配JSON对象的部分文本
- 在循环中分配json值时,值被覆盖
- 需要帮助设置json数组
- 如何在Javascript中将JSon对象转换为数组
- 我可以在json对象中添加一个函数吗
- 使用JS将数组转换为json对象
- Javascript-如何读取json文件中的列并将其保存在Javascript数组中
- 使用JQuery解析JSON嵌套数组
- 在使用Polymer'加载所有json文件后执行方法;s的核心ajax
- 如何使用json将对象列表从java转换为javascript
- 如何使用 node.js 比较两个 json 数组
- 如何将JSON数据导入我的ejs模板
- 区分JSON中的矩阵和列表列表
- 不显示带有本地json文件数据的谷歌地图脚本
- 如何将json数据显示为html
- 如何为json对象中的段发送array[]
- 根据id将json数组组合为一个json数组
- Json数据包含日期和时间格式
- AngularJS-在JSON选择器中使用变量名
- javascript JSON advanced