Aller au contenu

Json to JS problem avec Eval?


kreel-ex

Sujets conseillés

Quel est le problem dans ce code qui est censé récupéré le json, en faire un objet javascript et en faire un affichage ?

// Example Objet en json
var TestlinkJson = {"id_source":"7315f47dba65515fca60c851d7065bc6","url_source":"http:\/\/news.yahoo.com\/s\/afp\/20060822\/od_afp\/chinacateringcultureoffbeat_060822074830","title_source":"Jail-theme teahouse becomes prisoner of own success in China - Yahoo! News","smartlinks_infos":[{"id_dest":"75effc69e8c68b389c65c67119463312","score":"126","deleted":"0","date":"1164279208","zone_cible":"151","manually_added":"0","url_dest":"http:\/\/news.yahoo.com\/s\/afp\/20060825\/od_afp\/malaysiahealthtoilets_060825160532","title_dest":"Malaysia calls for \"toilet revolution\" - Yahoo! News"}]}

//fonction de construction de SmartLinks
function TestLink(url_source,title_source,id_source)
{
this.url_source = url_source; //l'url du document source du SmartLinks
this.title_source = title_source; // titre du doc source du SmartLinks
this.id_source = id_source; //l'id de la source du SmartLinks
this.smartlinks_info = new array; //tableau de liens cible avec info sur la zone, la date etc...


}

var TestLink = eval('(' + TestlinkJson + ')');

alert(TestLink);

Lien vers le commentaire
Partager sur d’autres sites

//Example Objet en json
var TestlinkJson = {"id_source":"7315f47dba65515fca60c851d7065bc6","url_source":"http:\/\/news.yahoo.com\/s\/afp\/20060822\/od_afp\/chinacateringcultureoffbeat_060822074830","title_source":"Jail-theme teahouse becomes prisoner of own success in China - Yahoo! News","smartlinks_infos":[{"id_dest":"75effc69e8c68b389c65c67119463312","score":"126","deleted":"0","date":"1164279208","zone_cible":"151","manually_added":"0","url_dest":"http:\/\/news.yahoo.com\/s\/afp\/20060825\/od_afp\/malaysiahealthtoilets_060825160532","title_dest":"Malaysia calls for \"toilet revolution\" - Yahoo! News"}]}

//fonction de construction de SmartLinks
// notice we're now using JSON as the init data
function TestLink(json_init)
{
// init data is used from the init object, e.g. json_init.property
this.url_source = json_init.url_source; //l'url du document source du SmartLinks
this.title_source = json_init.title_source; // titre du doc source du SmartLinks
this.id_source = json_init.id_source; //l'id de la source du SmartLinks
// notice Array() -- you need to use the proper constructor
// to actually create an array
this.smartlinks_info = new Array(); //tableau de liens cible avec info sur la zone, la date etc...


}

// note I changed the name of the instance var to avoid confusion
var testLinkInstance = new TestLink(TestlinkJson);

// this will alert saying '[Object]' or something similar,
// since you are attempting to alert with an entire object
alert(testLinkInstance.smartlinks_info[title_dest]);

Lien vers le commentaire
Partager sur d’autres sites

Veuillez vous connecter pour commenter

Vous pourrez laisser un commentaire après vous êtes connecté.



Connectez-vous maintenant
×
×
  • Créer...