SitePoint Sponsor |
|
User Tag List
Results 1 to 13 of 13
Thread: JSOn to JS Eval Problem
-
Jul 30, 2007, 02:22 #1
- Join Date
- Jan 2005
- Location
- Paris
- Posts
- 370
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
JSOn to JS Eval Problem
What is wrong with this code? it should to parse JSOn the string it into javascript object then display it on screen... i get nothing but a white screen!
Code JavaScript:// 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);
On a PHP/Java/XML/Javascript/MySQL internship right now!
-
Jul 30, 2007, 02:53 #2
- Join Date
- Jul 2007
- Posts
- 345
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
To declare an array, use
this.smartlinks_info = new Array();
-
Jul 30, 2007, 02:59 #3
- Join Date
- Jul 2007
- Posts
- 345
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
Probably more important:
Because you are using your JSON object as a string to pass to the eval method, the { and } object brackets need to be part of that string.
So, your first line should be
var TestlinkJson = '{"id_source":"hjgjhgj", ...}';
i.e. the object should be enclosed in quotes.
-
Jul 30, 2007, 03:14 #4
- Join Date
- Jan 2005
- Location
- Paris
- Posts
- 370
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
thanks for the synthaxe error, let me see if it works!
On a PHP/Java/XML/Javascript/MySQL internship right now!
-
Jul 30, 2007, 03:16 #5
- Join Date
- Jan 2005
- Location
- Paris
- Posts
- 370
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
is the eval part written the right way?
On a PHP/Java/XML/Javascript/MySQL internship right now!
-
Jul 30, 2007, 05:23 #6
- Join Date
- Jul 2007
- Posts
- 4
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
i am not sure you cal call the lart method on an entire object!.?!?
-
Jul 30, 2007, 05:34 #7
- Join Date
- Jul 2007
- Posts
- 345
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
The alert method will just show [object Object] or such-like. However, as it stands, the script doesn't even get that far.
-
Jul 30, 2007, 05:45 #8
- Join Date
- Jan 2005
- Location
- Paris
- Posts
- 370
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
i did make the correction you suggested and nothing still works... if you know how i could make this script work please let me know what part of the code need to edited to go as far as displaying the content of the object...
thanks in advance
Code JavaScript:// 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; this.title_source = title_source; this.id_source = id_source; this.smartlinks_info = new array(); } var TestLink = eval('(' + TestlinkJson + ')'); alert(TestLink.url_source);
On a PHP/Java/XML/Javascript/MySQL internship right now!
-
Jul 30, 2007, 06:01 #9
- Join Date
- Jul 2007
- Posts
- 345
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
I think I've found the problem.
The last property of the JSON object has escaped quotes: \"
Try double escaping them: \\"
-
Jul 30, 2007, 06:53 #10
- Join Date
- Jan 2005
- Location
- Paris
- Posts
- 370
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
thank you again for this one again... actually the alert function works on the object before the eval
On a PHP/Java/XML/Javascript/MySQL internship right now!
-
Jul 31, 2007, 02:56 #11
- Join Date
- Jan 2005
- Location
- Paris
- Posts
- 370
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
i tried a work around, let me know what u think:
Code JavaScript:// 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);
On a PHP/Java/XML/Javascript/MySQL internship right now!
-
Jul 31, 2007, 19:41 #12
- Join Date
- Jan 2005
- Location
- Paris
- Posts
- 370
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
I am trying to understand DOM scripting, so how would you do to display the testlink object content in a two column table? one column for the first three atributes ine one cell and the last one in a second cell?
On a PHP/Java/XML/Javascript/MySQL internship right now!
-
Aug 3, 2007, 02:52 #13
- Join Date
- Jan 2005
- Location
- Paris
- Posts
- 370
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
I am triying to display the content innerHTML in this code... but it's not happening... what's wrong?
Code:<html> <head> <title>Code de démonstration - Explorer un tableau HTML avec des interfaces DOM et JavaScript</title> <script> // Example ObjetSmartLink en json var SmartlinkJson = {"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"}]}; //var SmartlinkJson = <?= $jsonstring ?> //fonction de construction de SmartLinks var SmartLink = eval(SmartlinkJson); alert(SmartLink.id_source); //fonction d'affichage de SmartLinks function DisplaySmartLinks (SmartLink) { //récupere une référence vers l'élément SmartLinks var zone_affichage = document.getElementById("SmartLink") //var TableCible=new Array('Titre_Cible"', 'Date_cible'); var Props=new Array('id_source', 'url_source', 'title_source', 'smartlinks_info'); // récupère une référence vers l'élément body // var mybody = document.getElementsByTagName("body")[0]; // création des éléments <table> et <tbody> smartlinksTable = document.createElement("table"); smartlinksTbody = document.createElement("tbody"); //for (p=0;p<Props.length;p++){ // création des cellules for(var j = 0; j < 2; j++) { // création d'un élément <tr> smartlinksLine = document.createElement("tr"); // création du premier élément <td> de la ligne SmartLinks smartlinksCell1 = document.createElement("td"); for (k=0; k<2; k++) { smartlinksCell1.innerHTML += 'cellule source<br />'; //smartlinksCell1.innerHTML +=SmartLink[Props[p]] } // ajoute la premiere cellule <td> à la ligne <tr> smartlinksLine.appendChild(smartlinksCell1); // création du second élément <td> de la ligne smartlinks smartlinksCell2 = document.createElement("td"); for (l=0; l<6; l++) { smartlinksCell2.innerHTML += 'liens cibles<br />'; } // ajoute la seconde cellule <td> à la ligne <tr> smartlinksLine.appendChild(smartlinksCell2); // ajoute la ligne <tr> à l'élément <tbody> smartlinksTbody.appendChild(smartlinksLine); // } } // ajoute <tbody> à l'élément <table> smartlinksTable.appendChild(smartlinksTbody); // ajoute <table> à l'élément <body> zone_affichage.appendChild(smartlinksTable); // définit l'attribut border de mytable à 2 smartlinksTable.setAttribute("border", "1"); } </script> </head> <body onload="DisplaySmartLinks()"> <div id="Smartlink"> le test d'affichage </div> </body> </html>
On a PHP/Java/XML/Javascript/MySQL internship right now!
Bookmarks