【问题标题】:javascript JSON advancedjavascript JSON 高级
【发布时间】:2013-02-13 07:48:53
【问题描述】:

我找不到错误。也许你可以帮助我: 我的代码如下:

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);   

});

附言

如果我将代码编辑为:

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] is undefined...

附注 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);  

             });

【问题讨论】:

  • 这不是 JSON,这是一个对象初始化器
  • 哪里出了问题?
  • 我看不到任何函数返回任何东西。
  • 您是否添加了控制台行并查看发生了什么?
  • 任何时候你发现自己在一个技术问题中写下“......不工作......”,在它上面退格并说确切你期望发生什么,确切地发生了什么,为什么你认为这是错误的,并引用任何可用的错误报告的输出(例如,所有主要浏览器现在都有一个错误控制台)。

标签: javascript json object mootools each


【解决方案1】:

工作代码如下:

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);  

                 });
          }

谢谢。

【讨论】:

【解决方案2】:

data.product.each 将运行回调 2 次(因为你里面有 2 个对象)。第一次, p 将是配置对象。第二次,它将是选项对象。

你正在做类似p.config.id 的事情,当 p 是“选项”时这没有意义。 看来您根本不需要使用 each 迭代器? 只需在开头使用var p = data.product,然后删除each 迭代即可。

【讨论】:

  • 如果我有很多产品,如何删除每个?
  • 我第一次看错了你的数据,对不起。你的每一个都很好。
  • 但是您似乎希望它返回一些东西。但是 Array.forEach(在 Javascript 中)和 Array.each(在 mootools 中)不返回任何东西,所以你看到 undefined 结果是正常的。
猜你喜欢
  • 2015-04-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-11-24
  • 2012-06-12
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多