无法使用 AS3 和 Flash 解析 JSON
我正在尝试通过 Flash/AS3 从 URL 解析一些 JSON。 到目前为止,这是我的代码:
import com.adobe.serialization.json.JSON;
import com.adobe.serialization.json.JSONDecoder;
var loader:URLLoader = new URLLoader();
var request:URLRequest = new URLRequest();
request.contentType = "application/json";
request.url="http://shaktiwarriors.guinness.trillitech.com/json/quiz/getNotAnsweredQuestions.php";
//request.url="demo.txt";
loader.load(request);
loader.addEventListener(Event.COMPLETE,loadConfirm);
loader.addEventListener(Event.COMPLETE,decodeJSON);
function loadConfirm(e:Event):void {
trace("Load Successful" + "\n");
}
function decodeJSON(event:Event):void {
var loader:URLLoader = URLLoader(event.target);
trace(loader.data.toString() + "\n");
var ids:Array= JSON.decode(loader.data);
for (var i:int = 0; i < ids.length; ++i){
trace(ids[i].id);
my_txt.text = ids[i].id + "\n"
}
}
trace(loader.data.toString() + "\n") 输出一堆 HTML。我确信我错过了一些简单的事情。
感谢您的帮助!
I am attempting to parse some JSON from a URL via Flash/AS3.
Here's my code so far:
import com.adobe.serialization.json.JSON;
import com.adobe.serialization.json.JSONDecoder;
var loader:URLLoader = new URLLoader();
var request:URLRequest = new URLRequest();
request.contentType = "application/json";
request.url="http://shaktiwarriors.guinness.trillitech.com/json/quiz/getNotAnsweredQuestions.php";
//request.url="demo.txt";
loader.load(request);
loader.addEventListener(Event.COMPLETE,loadConfirm);
loader.addEventListener(Event.COMPLETE,decodeJSON);
function loadConfirm(e:Event):void {
trace("Load Successful" + "\n");
}
function decodeJSON(event:Event):void {
var loader:URLLoader = URLLoader(event.target);
trace(loader.data.toString() + "\n");
var ids:Array= JSON.decode(loader.data);
for (var i:int = 0; i < ids.length; ++i){
trace(ids[i].id);
my_txt.text = ids[i].id + "\n"
}
}
The trace(loader.data.toString() + "\n") outputs a bunch of HTML. I'm sure I'm missing something simple.
Thanks for the help!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
当我在浏览器中尝试 URL 时,它说我需要登录这显然是 HTML,而不是 JSON。在尝试解析 JSON 之前,请确保加载了正确的内容。看起来您需要进行身份验证才能加载 JSON。
When I tried the URL in browser it is saying that I need to login which is clearly a HTML, not a JSON. Before trying to parse JSON make sure that you loaded the correct thing. Looks like that you need to authenticate to load the JSON.