SitePoint Sponsor

User Tag List

Results 1 to 13 of 13
  1. #1
    SitePoint Addict
    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!

  2. #2
    SitePoint Evangelist
    Join Date
    Jul 2007
    Posts
    345
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    To declare an array, use
    this.smartlinks_info = new Array();

  3. #3
    SitePoint Evangelist
    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.

  4. #4
    SitePoint Addict
    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!

  5. #5
    SitePoint Addict
    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!

  6. #6
    SitePoint Member
    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!.?!?

  7. #7
    SitePoint Evangelist
    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.

  8. #8
    SitePoint Addict
    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!

  9. #9
    SitePoint Evangelist
    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: \\"

  10. #10
    SitePoint Addict
    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!

  11. #11
    SitePoint Addict
    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!

  12. #12
    SitePoint Addict
    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!

  13. #13
    SitePoint Addict
    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

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •