作为HTTP请求的结果,我在URLLoader对象中获得了JSON数据。我需要在TextField中以格式化的方式放置JSON。
我怎样才能做到这一点:
[{"id":"barrrrrr","client":"foo"}]
看起来像这样:
[
{
"id":"barrrrrr",
"client":"foo"
}
]
在文本域内?
干杯。
答案 0 :(得分:3)
在Flash player 11 +
中使用本机JSON对象var stringJSON:String = loader.data as String;
var jsonData:Object = JSON.parse(stringJSON);
textField.text = JSON.stringify(jsonData,null,4);
答案 1 :(得分:0)
它与你提出的问题不完全相同,但我们使用这种方法记录as3对象(json也是如此),也可能对你有用:
package
{
import flash.display.Sprite;
import flash.utils.getQualifiedClassName;
public class astest extends Sprite
{
public function astest()
{
var jsonStr:String = '[{"id":"barrrrrr","client":{"id":"barrrrrr","client":"foo"}},{"id":"barrrrrr","client":"foo"}]';
var jsonObj:Object = JSON.parse(jsonStr);
trace(parse(jsonObj));
}
private static const simpleTypes:Array = ["int", "uint", "String", "Number", "Boolean", "null", "XML", "void"];
private static function parse(o:Object, tabs:String = ""):String
{
var out:String = "";
var tabStr:String = " ";
var cnt:int = 40; //number of properties on the same object to log out
var type:String;
for(var prop:* in o)
{
type = "null";
try{
type = getQualifiedClassName(o[prop]);
}catch(error:Error){};
out+= tabs + String(prop) + ":" + type + " = ";
if(simpleTypes.indexOf(type) >=0)
out+=o[prop]+"\r";
else
out+=type+"\r"+parse(o[prop], tabs+tabStr);
if(--cnt<=0)
{
out+=tabs+"...\r";
break;
}
}
return out;
}
}
}
此示例的输出将为:
0:Object = Object
client:Object = Object
client:String = foo
id:String = barrrrrr
id:String = barrrrrr
1:Object = Object
client:String = foo
id:String = barrrrrr
您可以修改解析方法(这很简单),使输出与您的输出更相似。