[]JSON文件加载成功后,怎么解析成Object对象
res/json/config.json文件内容:
---------------------------------
{"id": 1, "name": "Otto"}
AS测试代码(文档类):
--------------------------------
输出信息:
-------------------------
0
Chen
加载: res/json/config.json
readJsonFinish - 加载成功: res/json/config.json
...运行到 JSON.parse(jsonConf) 就报错了
---------------------------------
{"id": 1, "name": "Otto"}
AS测试代码(文档类):
--------------------------------
package
{
import laya.display.Text;
import laya.net.Loader;
import laya.utils.Handler;
/**
* ...
* @author OttoChen
*/
public class TestMain
{
private var txt:Text = null;
public function TestMain()
{
Laya.init(800, 800);
txt = new Text();
txt.mouseEnabled = false;
txt.overflow = Text.SCROLL;
txt.size(Laya.stage.width, Laya.stage.height);
txt.pos(10, 10);
txt.fontSize = 30;
txt.wordWrap = true;
txt.color = "#FF9632";
Laya.stage.addChild(txt);
var test:Object = JSON.parse('{"id": 0, "name": "Chen"}');
traceMsg(test['id']);
traceMsg(test['name']);
var jsonURL:String = "res/json/config.json";
traceMsg("加载: " + jsonURL);
Laya.loader.load(jsonURL, Handler.create(this, readJsonFinish, [jsonURL]), null, Loader.JSON);
}
private function readJsonFinish(url:String = null):void
{
traceMsg("readJsonFinish - 加载成功: " + url);
// 返回的是 * 类型,要不要先解析成字符串类型,那应该怎么转呢??
var jsonConf:* = Laya.loader.getRes(url);
// 这里直接传入jsonConf对象后,打开浏览器调试发现报错了!!
var config:Object = JSON.parse(jsonConf);
traceMsg(config['id']);
traceMsg(config['name']);
traceMsg("==================");
}
private function traceMsg(msg:String = ""):void
{
if ( msg != null )
{
txt.text += msg + "\n";
txt.scrollY = txt.maxScrollY;
}
}
}
}
输出信息:
-------------------------
0
Chen
加载: res/json/config.json
readJsonFinish - 加载成功: res/json/config.json
...运行到 JSON.parse(jsonConf) 就报错了
没有找到相关结果
已邀请:
2 个回复
OttoHX
赞同来自:
Aar0n
赞同来自: