Unable to parse JSON using AS3 and Flash
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.开发者_如何学Pythonload(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!
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.
精彩评论