Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/flash/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Actionscript 3 将JSON转换为数组的好方法_Actionscript 3_Flash_Actionscript - Fatal编程技术网

Actionscript 3 将JSON转换为数组的好方法

Actionscript 3 将JSON转换为数组的好方法,actionscript-3,flash,actionscript,Actionscript 3,Flash,Actionscript,我正在尝试将json响应转换为动作脚本3中的数组 这是我的JSON响应: [{ "id": 1, "klistid": 8017, "name": "List item name", "description": "List item description", "type": 1, "offset": 300, "req": "gfi" }, { "id": 2, "klistid": 8018, "name":

我正在尝试将json响应转换为动作脚本3中的数组

这是我的JSON响应:

[{
    "id": 1,
    "klistid": 8017,
    "name": "List item name",
    "description": "List item description",
    "type": 1,
    "offset": 300,
    "req": "gfi"
},
{
    "id": 2,
    "klistid": 8018,
    "name": "List item name",
    "description": "List item description",
    "type": 1,
    "offset": 600,
    "req": "gfi"
},
{
    "id": 3,
    "klistid": 8019,
    "name": "List item name",
    "description": "List item description",
    "type": 1,
    "offset": 900,
    "req": "gfi"
}]
这是我的ac3代码:

    function GetLists()
    {
    var req:URLRequest = new URLRequest("http://localhost:51318/api/List/1");
    var ret:URLLoader = new URLLoader();
    ret.addEventListener(Event.COMPLETE,function(e:Event)
    {
        var jsonData:Object = JSON.decode(ret.data);
        for (var i:String in jsonData)
        {
        trace(i + ": " + jsonData[i]);
        }
    });
    ret.load(req);
    }
我将此功能分配给一个按钮,但每当我单击该按钮时,什么都不会发生。当ac3代码进入for循环代码时,我将跟踪放入其中,代码只是停止,什么也不做。我只想将这个json字符串转换为数组或列表,以便在其中进行迭代。我已经看过一些关于将JSON字符串转换为数组的帖子,但我都试过了,仍然没有任何效果


谢谢

您的json数据和尝试对其进行迭代的方式之间存在差异

您的数据是一个包含一个元素的数组,但您尝试在该元素上迭代,就像它应该包含您的对象id:1、2、3等等一样。。。当然不是

相反,请从json数据中删除包装器数组,或者如果不想,请执行以下操作:

var data:Object = jsonData[0];
for (var i:String in data)
{
    trace(i)
}

您只需将解码后的数据转换为
数组

function GetLists()
{
    var req:URLRequest = new URLRequest("http://localhost:51318/api/List/1");
    var ret:URLLoader = new URLLoader();
    ret.addEventListener(Event.COMPLETE,function(e:Event)
    {
        var jsonData:Array = JSON.decode(ret.data) as Array;
        for each(var obj:Object in jsonData)
        {
            trace(obj["id"] + ": " + obj["description"]);
        }
    }
    ret.load(req);
}

解码时,您已经拥有该数组
var jsonData:Object=JSON.decode(ret.data)
因此,如果您执行了
trace(jsonData是数组)
您将获得true。因此,您可以通过执行
jsonData[0].id等操作来访问对象。此外,您还可以使用内置的顶级JSON函数,即
JSON.parse(ret.data)
@LDMS奇怪的是,当我用
跟踪检查我的jsonData(jsonData是数组)
我的json字符串错了吗?不,我只是试着将其作为POST并得到true(作为数组)。试着输入
ret.data
,看看你得到了什么,也许问题就在那里。另外,您是否切换到使用
JSON.parse
(去掉对as3corelib的JSON的所有引用)as3corelib JSON类的性能不如本机类,您绝对应该切换。