Copy link to clipboard
Copied
The output of my php file looks like this:
[{"id":2,"name":"NRK MP3","url":"http:\/\/lyd.nrk.no\/nrk_radio_mp3_mp3_m","country_id":37},{"id":72,"name":"Radio Loland","url":"http:\/\/stream.radiololand.no:5000\/loland128","country_id":37}]
What i want to accomplish is to get that information, I need the url, the country id, the name, and the id. But I get this error:
Error: Error #2101: The String passed to URLVariables.decode() must be a URL-encoded query string containing name/value pairs.
at Error$/throwError()
at flash.net::URLVariables/decode()
at flash.net::URLVariables()
at DatabaseTest_fla::MainTimeline/onCompleteHandler()[DatabaseTest_fla.MainTimeline::frame1:28]
at flash.events::EventDispatcher/dispatchEventFunction()
at flash.events::EventDispatcher/dispatchEvent()
at flash.net::URLLoader/onComplete()
If I change my php file to this. It works
<?php
echo "values=8";
?>
My As3 code:
import flash.events.MouseEvent;
import flash.net.URLLoader;
import flash.net.URLRequest;
import flash.net.URLVariables;
import flash.net.URLLoaderDataFormat;
import flash.net.URLRequestMethod;
import flash.events.Event;
var myLoader:URLLoader = new URLLoader();
myLoader.dataFormat = URLLoaderDataFormat.TEXT;
var myRequest:URLRequest=new URLRequest("mydomain.com/something.php");
myLoader.load(myRequest);
myLoader.addEventListener(Event.COMPLETE,onCompleteHandler);
var myValue: String;
function onCompleteHandler(e:Event):void{
var myvariable: URLVariables = new URLVariables(e.target.data);
myValue = myvariable.values;
trace(myValue);
}
add json.dataFormat = URLLoaderDataFormat.TEXT;
and put back json.data to parse;
trace(json.data);
if it's still undefined so the problem comes from your php script or security sandbox network
you should test on your same domain server
Copy link to clipboard
Copied
You must use JSON class as it's a JSON structure.
JSON - Adobe ActionScript® 3 (AS3 ) API Reference
and use JSON.parse(e.target.data)
Copy link to clipboard
Copied
Thank you for replying Robert,
I have tried a little back and forth now.
This is my code:
var json:URLLoader = new URLLoader();
var parsedJSONData:Object;
json = new URLLoader();
json.addEventListener(Event.COMPLETE, parseJSON);
json.load(new URLRequest("http://blogglista.no/TESTESTDBDBDBDBD/check_media.php"));
trace("Loading JSON file...");
function parseJSON(evt:Event):void {
trace("JSON file loaded successfully!");
trace("Parsing JSON...");
trace("RESULTS:");
parsedJSONData = JSON.parse(json.data)
// var jsonArray:Array = (json.data);
trace("id: " + parsedJSONData.Id);
}
This is what my Output looks like:
Loading JSON file...
JSON file loaded successfully!
Parsing JSON...
RESULTS:
id: undefined
Test Movie terminated.
Why is my "id" undefined?
My inital goal is to make a for loop for all my id's
Thank you
Copy link to clipboard
Copied
json.data must be replaced with evt.target.data
Copy link to clipboard
Copied
Hmm, tried both evt.target.data, event.target.data, and e.target.data.
Same result unfortunately:
id: undefined
Copy link to clipboard
Copied
add json.dataFormat = URLLoaderDataFormat.TEXT;
and put back json.data to parse;
trace(json.data);
if it's still undefined so the problem comes from your php script or security sandbox network
you should test on your same domain server
Copy link to clipboard
Copied
You are right, I changed it to:
{ "ID1000": { "ident": "00001", "navn": "Costa Del Mar - Chillout", "stream": "http://stream.cdm-chillout.com:8020/stream-mp3-Chill", "by": "Ibiza", "land": "Spain", "sjanger": "Chillout" }, "ID2000": { "ident": "00000", "navn": "Bandit Rock Stockholm", "stream": "http://fm02-icecast.mtg-r.net/fm02_mp3?platform=web", "by": "Stockholm", "land": "Sweden", "sjanger": "Rock" }, "ID3000": { "ident": "00002", "navn": "Lounge FM - Terrace", "stream": "http://cast.loungefm.com.ua/terrace128?1372841071202.mp3", "by": "Kiev", "land": "Ukraine", "sjanger": "Chillout" } }
And it worked, thank you! Lifesaver.
I have another question for you, it might be for another thread. But here it is:
I want to do a for loop that takes ID1000 and ID2000 and ID3000 and so forth, and I want their stream. I have tried a few different ways, but nothing works.
var i=1;
var noe:String=("parsedJSONData"+"."+"ID"+i+"000"+"."+"ident");
//getDefinitionByName(noe) as Class;
//var noeee:MovieClip = new MovieClip();
//noeee.name= noe;
//getDefinitionByName(noeee) as Class;
trace(noe);
trace("id:" + noe);
trace("id:" + parsedJSONData.ID1000.ident);
//trace("id:" + eval(noe));
var nnoe:String = "ID"+1000;
parsedJSONData.ID1000.ident
id:parsedJSONData.ID1000.ident
id:00001
ID1000
Copy link to clipboard
Copied
for(var each:String in parsedJSONData){
trace(parsedJSONData[each].whateverYouWant)
}
Copy link to clipboard
Copied
Thank you Robert!
Copy link to clipboard
Copied
you are welcome!
Find more inspiration, events, and resources on the new Adobe Community
Explore Now