Comunidad de diseño web y desarrollo en internet online

consulta por comunicacion AS3 - JSON

Citar            
MensajeEscrito el 29 Sep 2010 07:22 pm
hola, buenas tardes;
estoy investigando la comunicacion entre AS3 y JSON;
vi algunos tutoriales, y creo q entendi bastante bien, si bien me falta muchisimo camino por recorrer;
mi problema surgio cuando quise salir del ejemplo del tutorial, y levantar datos de otro lado;
aqui desarrollo mi ejemplo, y luego mi consulta:

he seguido este tutorial: [url=http://www.kirupa.com/forum/showthread.php?t=326732][/url]

en mi .fla, primer frame, tengo el siguiente codigo:

Código ActionScript :

//import Jason.as class
import com.adobe.serialization.json.JSON

var loader:URLLoader = new URLLoader();
var request:URLRequest = new URLRequest();

//reading data from txt file

//Json can be called from any file like .txt .php etc. i Used here .txt
request.url = "_demo.txt";
loader.load(request);

//On load i'm calling this event which call decoding json class file
loader.addEventListener(Event.COMPLETE, decodeJSON);

//decoding funtion

function decodeJSON(event:Event):void{
var loader:URLLoader = URLLoader(event.target);
var facebook:Array = JSON.decode(loader.data);
/*Trace the JSON data, remember it used to look like this ([ {"name":"Jaylo", "number":"3243251"}, {"name":"Jenny", "phone":"8675309"}])*/
trace(facebook[0].name) ; //Jaylo
trace(facebook[0].number) ; //3243251
trace(facebook[1].name) ; //Jenny
trace(facebook[1].number) ; //8675309
//text box
my_txt.text = facebook[0].name +" "+ facebook[0].number;
}


en _demo.txt tengo la siguiente linea de codigo:

Código :

[ {"name":"Jaylo", "number":"3243251"}, {"name":"Jenny", "number":"8675309"}]


hasta aqui, los pasos del tutorial; no tuve problemas;

mi problema surge cuando intento tomar datos, como si tomara datos de facebook, de la siguiente estructura:

Código :

{
   "data": [
      {
         "id": "119421374429_xxxxxxxxxxxx",
         "from": {
            "name": "fulanito",
            "category": "Musicians",
            "id": "11942137xxxx"
         },
{
         "id": "119421374234_xxxxxxxxxxxx",
         "from": {
            "name": "menganito",
            "category": "Musicians",
            "id": "11944234xxxx"
         }
]
}


modifico mi AS de la siguiente forma:

Código ActionScript :

//import Jason.as class
import com.adobe.serialization.json.JSON

var loader:URLLoader = new URLLoader();
var request:URLRequest = new URLRequest();

//reading data from txt file

//Json can be called from any file like .txt .php etc. i Used here .txt
request.url = "_demo.txt";
loader.load(request);

//On load i'm calling this event which call decoding json class file
loader.addEventListener(Event.COMPLETE, decodeJSON);

//decoding funtion

function decodeJSON(event:Event):void{
var loader:URLLoader = URLLoader(event.target);
var facebook:Array = JSON.decode(loader.data);
/*Trace the JSON data*/
trace(facebook[0].id) ; //119421374429_xxxxxxxxxxxx
trace(facebook[0].from.name) ; //fulanito
trace(facebook[1].id) ; //119421374234_xxxxxxxxxxxx
trace(facebook[1].from.name) ; //menganito
//text box
my_txt.text = facebook[0].id+" "+ facebook[0].from.name;
}


cuando pruebo mi ejemplo modificado, obtengo el siguiente error:

Código :

TypeError: Error #1034: Error de conversión forzada: no se puede convertir Object@2340e9e9 en Array.
   at json_fla::MainTimeline/decodeJSON()
   at flash.events::EventDispatcher/dispatchEventFunction()
   at flash.events::EventDispatcher/dispatchEvent()
   at flash.net::URLLoader/onComplete()


cabe aclarar, que si al archivo .txt lo dejo asi:

Código :

 [
      {
         "id": "119421374429_xxxxxxxxxxxx",
         "from": {
            "name": "fulanito",
            "category": "Musicians",
            "id": "11942137xxxx"
         },
{
         "id": "119421374234_xxxxxxxxxxxx",
         "from": {
            "name": "menganito",
            "category": "Musicians",
            "id": "11944234xxxx"
         }
]


mi ejemplo, funciona;
alguien puede guiarme en esta consulta, por favor;
muchas gracias, desde ya!
saludos,
sebas

Por flashodelico

80 de clabLevel



 

firefox
Citar            
MensajeEscrito el 24 Nov 2010 04:49 pm
Es importante mencionar que lo que regresa el JSON es un arreglo multidimensional. De tal forma que si tienen varios datos, la forma de accesarlos es:

json = JSON.decode(rpta.data);
trace(json[0]["nombre"]);
trace(json[1]["nombre"]);

trace(json[0]["recetas"]);
trace(json[0]["recetas"].length);
trace(json[0]["recetas"][0]["id"]);
trace(json[0]["recetas"][0]["titulo"]);

y asi sucesivamente dependiendo de su estructura.

Por ishtarlaure

28 de clabLevel



 

chrome

 

Cristalab BabyBlue v4 + V4 © 2011 Cristalab
Powered by ClabEngines v4, HTML5, love and ponies.