バグが見つかりません。あなたが私を助けることができるかもしれません:私のコードは以下の通りです:
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)}
});
このコードが上記のコードとして機能しない理由(未定義を返す):
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);
});
PS
コードを次のように編集した場合:
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);
});
1つの製品とコンソールエラーのみが返されます:p.config[i]は未定義です...
PS 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);
});