2012-04-17 212 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