kevinstueber
kevinstueber

Reputation: 2956

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.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!

Upvotes: 0

Views: 1956

Answers (1)

taskinoor
taskinoor

Reputation: 46037

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.

Upvotes: 1

Related Questions