2012-04-17 120 views
0

我想在ActionScript 2中获取JSON对象的值,但它一直返回undefined或[object Object]。Flash ActionScript 2 JSON返回[对象对象]

这里是我的代码:

for (var i:Number = 0; i < oProduct.prosAndCons.pros.length; i++) { 
     if (i == oProduct.prosAndCons.pros.length) { 
      break; 
     }; 
     // 
     mcProsCons.txtPros.htmlText += oProduct.prosAndCons.pros[i]+ "<br /><br />"; 
    }; 

这里是返回的JSON:

{ 
    "prosAndCons": { 
     "pros": [ 
      { 
       "cute animals": { 
        "link": "http://searchreviews.com/best/q-1661072-cute-animals", 
        "excerptCount": 1, 
        "excerpt": "Cute songs and cute animals." 
       } 
      }, 
      { 
       "cute toy": { 
        "link": "http://searchreviews.com/best/q-3584162-cute-toy", 
        "excerptCount": 6, 
        "excerpt": "All in all it's a very cute toy that holds up to a lot of use." 
       } 
      }, 
      { 
       "cute songs": { 
        "link": "http://searchreviews.com/best/q-1769522-cute-songs", 
        "excerptCount" :2, 
        "excerpt": "Cute songs and cute animals." 
       } 
      }, 
      { 
       "chunky magnetic letters": { 
        "link": "http://searchreviews.com/best/q-662-chunky-magnetic-letters", 
        "excerptCount": 1, 
        "excerpt": "The chunky magnetic letters are perfect for little hands and the magnets that hold them to the fridge are enclosed so there is no worry of a swallow hazard." 
       } 
      }, 
      { 
       "catchy song": { 
        "link": "http://searchreviews.com/best/q-672-catchy-song", 
        "excerptCount": 4, 
        "excerpt": "\" You made a match, look what you have done ,\" It's a very catchy song!" 
       } 
      } 
     ] 
    } 
} 

谁能告诉我什么,我做错了什么?这是一段时间,因为我用AS2编码W/JSON。

+0

提示:'prosAndCons.pros [0]'是一个对象。 – Marty 2012-04-17 03:04:51

+0

任何其他提示? – BrownFreelance 2012-04-17 04:45:42

+0

看看我对你的JSON所作的编辑,看看你能否发现任何东西。 – Marty 2012-04-17 04:50:59

回答

0

一些注意事项:

  • prosAndCons.pros[0]是一个对象。

    1. “可爱动物”
    2. “可爱玩具”
    3. “可爱的歌曲:

    4. 共有阵列prosAndCons.pros,每个包含由下式表示的内对象5个的对象是“

    5. ”矮胖磁性字母“
    6. ”琅琅上口的歌曲“

  • 每个市场都要有三个属性:
    1. “链接”
    2. “excerptCount”
    3. “摘录”

尝试使用此为您的JSON(如果你可以编辑它):

{"prosAndCons":{"pros":[{"name":"cute animals","link":"http://searchreviews.com/best/q-1661072-cute-animals","excerptCount":1,"excerpt":"Cute songs and cute animals."},{"name":"cute toy","link":"http://searchreviews.com/best/q-3584162-cute-toy","excerptCount":6,"excerpt":"All in all it's a very cute toy that holds up to a lot of use."},{"name":"cute songs","link":"http://searchreviews.com/best/q-1769522-cute-songs","excerptCount":2,"excerpt":"Cute songs and cute animals."},{"name":"cute magnetic letters","link":"http://searchreviews.com/best/q-662-chunky-magnetic-letters","excerptCount":1,"excerpt":"The chunky magnetic letters are perfect for little hands and the magnets that hold them to the fridge are enclosed so there is no worry of a swallow hazard."},{"name":"cute song","link":"http://searchreviews.com/best/q-672-catchy-song","excerptCount":4,"excerpt":"\" You made a match, look what you have done ,\" It's a very catchy song!"}]}} 

有了这个,你就可以做这样的事情:

for(var i:int = 0; i < oProduct.prosAndCons.pros.length; i++) 
{ 
    var pro:Object = oProduct.prosAndCons.pros[i]; 

    trace(pro.name); 
    trace(pro.link); 
} 
+0

不幸的是我无法编辑json。 – BrownFreelance 2012-04-17 05:27:15

+0

这是一种奇怪的格式,它会使每个对象中的信息几乎不可能处理,因为每个对象都被分配了一个随机属性名称,如“可爱的动物”。 – Marty 2012-04-17 05:34:36

+0

我发送了一个请求,看看它会更新它们的api。我希望今晚能够完成这个任务,但看起来我需要等一天。 – BrownFreelance 2012-04-17 05:46:53