views:

62

answers:

1

I have made an Ajax request to a PHP script and that PHP script returned this JSON object and when i tested with link text it says it's valid

[
{
    "id_annotation": "1",
    "annot_doc_id": "page_de_test.html",
    "annot_id_auteur": "2",
    "anno_startOffset": "47",
    "annot_startContainer": "id-aaa",
    "annot_endOffset": "88",
    "annot_forme": "1",
    "annot_value": "dispositif physique fait pour \n  stocker ",
    "annot_obj": "1",
    "annot_area": "",
    "annot_date": "vendredi 25 juin 2010 10:38:07"
},
{
    "id_annotation": "2",
    "annot_doc_id": "page_de_test.html",
    "annot_id_auteur": "2",
    "anno_startOffset": "122",
    "annot_startContainer": "id-aaa",
    "annot_endOffset": "200",
    "annot_forme": "1",
    "annot_value": " le plus simple est \n  d\u2019utiliser un syst\u00e8me de repr\u00e9sentation \u00e0 deux chiffres",
    "annot_obj": "1",
    "annot_area": "",
    "annot_date": "vendredi 25 juin 2010 10:38:19"
},
{
    "id_annotation": "3",
    "annot_doc_id": "page_de_test.html",
    "annot_id_auteur": "2",
    "anno_startOffset": "1",
    "annot_startContainer": "ident-77",
    "annot_endOffset": "146",
    "annot_forme": "1",
    "annot_value": "Dans un ordinateur, le dispositif qui permet de stocker de l\u2019information est donc \n  rudimentaire, bien plus rudimentaire que les mains humaines.",
    "annot_obj": "1",
    "annot_area": "",
    "annot_date": "vendredi 25 juin 2010 10:38:25"
},
{
    "id_annotation": "4",
    "annot_doc_id": "page_de_test.html",
    "annot_id_auteur": "2",
    "anno_startOffset": "107",
    "annot_startContainer": "ident-77",
    "annot_endOffset": "194",
    "annot_forme": "1",
    "annot_value": "crobaties avec ses doigts, mais \u00e9cartons ce cas). Avec un \n  emplacement d\u2019information ",
    "annot_obj": "1",
    "annot_area": "",
    "annot_date": "vendredi 25 juin 2010 10:38:33"
}

]

But the problem was when i tried ti loop over this json response by using this function, the fonction get excuted just one time, I want to know how to loop over this returned JSON

success: function(data) {
                    var mon_selection_2;
                    var mon_range;
                    resultText = eval(data);
                    var resultText_length = resultText.length;
                    for (var i=0; i<resultText_length; i++)
                    {
                    mon_selection_2 = window.getSelection();
                    mon_selection_2.removeAllRanges();
                    mon_range = document.createRange();
                    mon_selection_2.addRange(mon_range);
                    id_annotation_js = resultText[i].id_annotation;
                    anno_startOffset_js = resultText[i].anno_startOffset;
                    annot_startContainer_js = resultText[i].annot_startContainer;
                    annot_endOffset_js = resultText[i].annot_endOffset;
                    annot_value_js = resultText[i].annot_value;
                    annot_forme_js = resultText[i].annot_forme;
                    var start_Node = document.getElementById(annot_startContainer_js);
                    var textNode = start_Node.firstChild;
                    mon_range.setStart(textNode,anno_startOffset_js);
                    mon_range.setEnd(textNode,annot_endOffset_js);
                    var Gras_ele_js = document.createElement("span");
                    Gras_ele_js.setAttribute("style","color:black;font-size:14px;font-weight:bolder");
                    Gras_ele_js.setAttribute("value",mon_range);    
                    Gras_ele_js.setAttribute("id",id_annotation_js);    
                    mon_range.surroundContents(Gras_ele_js);    
                }
+1  A: 

well, with this line

resultText = eval(data);

you convert the string into a javascript object. You shouldn't do it that way (using eval), even if it's the fastest of all ways, because if massive security reasons.

You should use jQuerys very own implementation $.parseJSON() or, if available, browsers integrated JSON parser like window.JSON.parse();

To your question, you need to loop over an object here.

$.each(resultText, function(i,v)){
   $.each(v, function(index,value){
      anno_startOffset_js = value.anno_startOffset;
      // or anno_startOffset_js = value['anno_startOffset'];
   });
};
jAndy
Sorry and thank you But for example how to get How to get "anno_startOffset" value in every loop of resulText and save it in variable?
access2008
@access2008: see my update
jAndy
@jAndy, @access2008, and remember to always `var` your variables, especially when dealing with closures :)
Sean Kinsey