【问题标题】:Flash ActionScript 2 JSON returning [object Object]Flash ActionScript 2 JSON 返回 [object Object]
【发布时间】:2012-04-17 01:49:12
【问题描述】:

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

这是我的代码:

    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!"
                }
            }
        ]
    }
}

谁能告诉我我做错了什么?自从我用 JSON 编写 AS2 代码以来已经有一段时间了。

【问题讨论】:

  • 提示:prosAndCons.pros[0] 是一个对象。
  • 看看我对你的 JSON 所做的编辑,看看你能不能发现任何东西。

标签: json actionscript


【解决方案1】:

一些注意事项:

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

  • 数组prosAndCons.pros共有5个对象,每个对象包含一个内部对象,如下表示:

    1. “可爱的动物”
    2. “可爱的玩具”
    3. “可爱的歌曲”
    4. “厚实的磁性字母”
    5. 《朗朗上口的歌》

  • 其中的每一个都具有三个属性:
    1. “链接”
    2. “摘录计数”
    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);
}

【讨论】:

  • 它的格式很奇怪,几乎不可能处理每个对象中的信息,因为每个对象都被分配了一个随机属性名称,例如“可爱的动物”。
  • 我已发送请求查看它,他们将更新他们的 api。我希望今晚能完成这项工作,但看来我得再等一天了。
  • 不应该这样工作吗? (正在尝试测试): var sTestStr = oProduct.prosAndCons.pros["cute animals"].link;仍然返回 undefined。
  • 是的,这应该可以,假设您已经使用 JSON 库对 JSON 数据进行反序列化并将结果分配给 oProduct
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-08-18
  • 1970-01-01
  • 2016-11-19
  • 1970-01-01
  • 2013-09-13
  • 2015-01-24
相关资源
最近更新 更多