Displaying JSON in flash
I'm trying to pull data into flash using JSON but i keep getting this error
JSONParseError: Unexpected < encountered
at com.adobe.serialization.json::JSONTokenizer/parseError()
at com.adobe.serialization.json::JSONTokenizer/getNextToken()
at com.adobe.serialization.json::JSONDecoder/nextToken()
at com.adobe.serialization.json::JSONDecoder()
at com.adobe.serialization.json::JSON$/decode()
at jsonairtest_fla::MainTimeline/decodeJSON()
at flash.events::EventDispatcher/dispatchEventFunction()
at flash.events::EventDispatcher/dispatchEvent()
at flash.net::URLLoader/onComplete()
even though i can runt the pp file in the browser and the ouput looks fine to me, i even tried calling up a txt file and that worked but i don't know what am i doing wrong here.
Here is AS3 code
import com.adobe.serialization.json.JSON
var loader:URLLoader = new URLLoader();
var request:URLRequest = new URLRequest();
request.url = "pull.php";
loader.load(request);
loader.addEventListener(Event.COMPLETE, decodeJSON);
function decodeJSON(event:Event):void{
var loader2:URLLoader = URLLoader(event.target);
//trace(event.target.data);
var People:Array = JSON.decode(loader2.data);
trace(People[0].NETWORKNAME) ;
trace(People[1].NETWORKNAME) ;
}
PHP code:
<?php
$host="localhost";
$user="";
$password="";
$database="db name";
$tablename="table name";
header('Content-type: application/json');
if(!$connection = mysql_connect($host,$user,$password))
{
//if connection not eastablished then display message and die
$message = mysql_error();
//echo "$message<br>";
die();
}else
// in case the connection is eastablished
$message = "Connection eastablished.....";
//echo"$message<br>";
mysql_select_db($database,$connection)
or die("database not found");
$query = mysql_query("SELECT NETWORKNAME from $tablename);
$returnArray = array();
while($row=mysql_fetch_assoc($query))
array_push($returnArray, $r开发者_如何转开发ow);
mysql_close();
echo json_encode($returnArray);
?>
To convert the response into an array of undecoded JSON objects, try this:
var jsonArray:Array = loader2.data.match(/\{.*?\}/gi);
Then simply loop through calling JSON.decode on each member of the array. :)
In your php output, are you setting your header type to json?
header('Content-type: application/json');
Your decoder may need the type to be correct.
The error message says there's a <
in an unexpected position.
My guess is that you are inadvertedly reading your php from the file system, instead of running it on a server. Your file is being not executed as php code and that <
is probably the opening tag of your php script (<?
or <?php
).
If you are running this code from the IDE, your url should be something like:
htp://localhost/my_project/pull.php
If you run this on a browser, you don't need an absolute path, but make sure your swf runs on an http environment (which is able to execute php, of course). That is, test it like this:
http://localhost/my_project/index.php
(where index.php is the hmtl file that embeds your swf)
精彩评论