Copy link to clipboard
Copied
Hey
I want to parse this information. it is php.
[{"KategoriID":1,"KategoriNavn":"4. Dagligvare"},{"KategoriID":2,"KategoriNavn":"1. Bygg og jernvare"},{"KategoriID":3,"KategoriNavn":"3. Sport"},{"KategoriID":4,"KategoriNavn":"5. Hjem og interi\u00f8r"},{"KategoriID":6,"KategoriNavn":"2. Elektronikk"},{"KategoriID":7,"KategoriNavn":"6. Kl\u00e6r og mote"},{"KategoriID":9,"KategoriNavn":"8. Lek & morro"},{"KategoriID":10,"KategoriNavn":"7. Helse"}]
This is my AS3 Code:
var json:URLLoader = new URLLoader();
var parsedJSONData:Object;
json = new URLLoader();
json.addEventListener(Event.COMPLETE, parseJSON);
json.load(new URLRequest("http://blogglista.no/Tilbudsappen/StoreCategories.php"));
trace("Loading JSON file...");
var bgmList=new Vector.<String>();
function parseJSON(evt:Event):void {
parsedJSONData = JSON.parse(json.data)
//trace(parsedJSONData.streams[0])
trace("parsed: " + parsedJSONData);
}
What I get when i trace:
parsed: [object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object].
I have tried several ways to get what the objects really are. But I want to be able to get the KategoriID and KategoriNavn. I have several php files that looks like this. How do I write it?
Thank you!
for(var each:String in parsedJSONData){
for(var each2:String in parsedJSONData[each]){
trace(each2+" = "+parsedJSONData[each][each2]);
}
}
Copy link to clipboard
Copied
your trace should be:
trace("parsed: "+parsedJSONData.toString());
or
for(var each:String in parsedJSONData){
trace(each+" = "+parsedJSONData[each]);
}
Copy link to clipboard
Copied
Hmm
This is what Im getting with thoose traces.
parsed: [object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
0 = [object Object]
1 = [object Object]
2 = [object Object]
3 = [object Object]
4 = [object Object]
5 = [object Object]
Copy link to clipboard
Copied
for(var each:String in parsedJSONData){
for(var each2:String in parsedJSONData[each]){
trace(each2+" = "+parsedJSONData[each][each2]);
}
}
Copy link to clipboard
Copied
Thank you Robert!!! Works perfect. Now I am going to import 10 URLs at once with a for loop. But what happens is that the foor loop is done before the Event.COMPLETE is fired. Could you point me in the right direction of how I could make the for loop "wait"? Thanks!
for(var i=1;i<antallKategorier;i++){
var json1:URLLoader = new URLLoader();
var parsedJSONData1:Object;
json1 = new URLLoader();
json1.addEventListener(Event.COMPLETE, parseJSON);
var jsonNumber=("http://blogglista.no/Tilbudsappen/Category"+i+".php");
json1.load(new URLRequest(jsonNumber));
trace(jsonNumber);
function parseJSON(evt:Event):void {
//trace("JSON file loaded successfully!");
//trace("Parsing JSON...");
//trace("RESULTS:");
i++;
parsedJSONData1 = JSON.parse(json.data);
for(var each:String in parsedJSONData1){
for(var each2:String in parsedJSONData1[each]){
//trace(each2+" = "+parsedJSONData[each][each2]);
trace(each2);
if(each2=="ButikkURL"){trace(parsedJSONData1[each][each2]);
}
}
}
}
}
Copy link to clipboard
Copied
How it can be fired before if you run parseJSON() function especially at Event.COMPLETE???
also it's safe to urlencode your URL in JSON to avoid any strange result
Copy link to clipboard
Copied
Well, my intention is to run the for loop, parse the data from the 10 URLs, put it into an array.
What happens now is that it finishes the loop (i=1 to i=10) before it parses for i=1. Which means I miss 9/10 of the parsing. and basically 9/10ths of my array aswell.
If I managed to explain my intention.
Copy link to clipboard
Copied
another way:
var urlLoader:URLLoader = new URLLoader();
urlLoader.load(new URLRequest(apiURL));
urlLoader.addEventListener(Event.COMPLETE, function(e:Event) {
var json:Object = JSON.decode(e.target.data);
//etc...
});
Copy link to clipboard
Copied
Yeah, the parsing works very good.
I can load the 10 URLs writing the code 10 times. Which I will do if I dont find another way. What I want to do is run this for loop, which parses data from the 10 URLs, and pushes them into my array. Instead of having to write urlLoader1-10, json1-10 and so forth.
for (var i = 1; i < antallKategorier; i++) //FYI: antallKategorier==10
{
var json1: URLLoader = new URLLoader();
var parsedJSONData1: Object;
json1 = new URLLoader();
json1.addEventListener(Event.COMPLETE, parseJSON); // The problem is that this line runs after my for loop is done, while // I want it to run 10 times.
var jsonNumber = ("http://blogglista.no/Tilbudsappen/Category" + i + ".php");
json1.load(new URLRequest(jsonNumber));
function parseJSON(evt: Event): void {
parsedJSONData1 = JSON.parse(json.data);
for (var each: String in parsedJSONData1) {
for (var each2: String in parsedJSONData1[each]) {
//trace(each2+" = "+parsedJSONData[each][each2]);
trace(each2);
if (each2 == "ButikkURL") {
trace(parsedJSONData1[each][each2]);
}
}
}
}
}
Copy link to clipboard
Copied
ha ok you have 10 url to grab the json data right?
var nbURL:uint = 1;
jsonGrabAll(urlArray[nbURL]);
private function jsonGrabAll(url):void{
try{
// your urlloader + json logic
}catch(event:*){
trace("something wrong");
}
}
private function parseJSON(evt:Event):void{
// parse logic
if(everything == "ok"){
nbURL += 1;
if(nbURL < 11){
jsonGrabAll(urlArray[nbURL]);
}else{
nbURL = 1;
}
}
}