簡體   English   中英

用於使用JSON.parse生成JSON對象的JSON格式不正確

[英]Incorrect format of a JSON for generate an JSON object with JSON.parse

我有一個畫樹的網站。 inicio_pru.php創建一個表示的JSON,該JSON傳遞給JavaScript文件以創建樹。

inicio_pru.php在兩個不同的時刻被調用,首先,當頁面被充電時,它將創建JSON並將其傳遞給example_pru.js以便繪制它。 2,當樹已經創建,並且用戶單擊樹的節點時,這example_pru.js調用inicio_pru.php並帶有對象XMLHttpRequest,並且inicio_pru.php會以與第一次相同的方式生成JSON,並將其發送到帶有“ echo”命令的XMLHttpRequest

它在第一種情況下有效,但在第二種情況下無效,它會產生以下錯誤:

Unexpected token '

inicio_pru.php

function main_p ($ID,$tipo_var,$desc_var,$idnodo,$t_ancestros) {
  ......

  //Here, $arbol is saved in the correct format 


  if ($tipo_var=='BIFURCADORES'){
    $file = fopen("archivo.txt", "w");
    //$file = fopen($desc_var.".txt", "w");
    fwrite($file, $arbol . PHP_EOL);
    fclose($file);
    } 

    return $arbol;

 }



 //main program



 if (!is_null($idnodo)) {


    // main_p , has saved  $arbol with each field of the JSON in double quotes

    $arbol=main_p($ID,$tipo_var,$desc_var,$idnodo,$t_ancestros);

  //this sentence has saved  $arbol at the discwith each field of the JSON without double quotes
    exec('echo '.$arbol. ' >>/tmp/pista11', $output, $error);


  //This is sent to example1_json_pru.js throght an objet    XMLHttpRequest 
    echo $arbol;

  }
  else 

    $arbol=main_p($ID,$tipo_var,$desc_var,$idnodo,$t_ancestros);

如您所見,$ arbol保存在兩個文件中:

archivo.txt,正確,它將每個字段都用雙引號引起來,但是在pista11中,fileds顯示時沒有這些雙引號:

archivo.txt , (correct):

{"id":"53530","name":"Bifurcadores <ul ....

pista11 , (incorrect):

{id:53530,name:Bifurcadores <ul  ......

inicio_pru.php中“ else”句子中, $ arbol傳遞給另一個.php,然后將其發送到.js example_pru.js ,並且可以運行:

grafos_template_otro_json_fr_pru.php:

   <!--  Files -->

   <script language="javascript" type="text/javascript"      src="../mapas/assets/js/example1_json_pru.js"></script>

   <script type="text/javascript">
     var datos=<?php echo $arbol ; ?>;
     var ID=<?php echo $ID ; ?>;
     var tipo_var=<?php echo $tipo_var ; ?>;

   </script>

    </head>

           <body onload="init(datos,1,ID,tipo_var);">

(init是example1_json_pru.js的功能)

然而,當init_pru.phpXMLHttpRequest的調用,它pases $ ARBOL到example1_json_pru.js這樣,它不工作,並生成之前mencioned錯誤:

XMLHttpRequest:

    onCreateLabel: function(label, node){
        label.id = node.id;            
        label.innerHTML = node.name;
        label.onclick = function(){

        var http = new XMLHttpRequest();
        var url = "inicio_pru.php";
        var params = "idnodo="+ node.id + "&ID=" + ID + "&id_arbol=" + tipo_var;
         http.open("POST", url, true);

        //Send the proper header information along with the request
        http.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
        http.send(params);
        http.onreadystatechange = function() {//Call a function when the state changes.
        if(http.readyState == 4 && http.status == 200) {

        //astonishingly,  alert shows the fields of the JSON in quotes despite pista11 (file saved just before sending $arbol XMLHttpRequest object), showed them wihthout double quotes




               alert (http.responseText);
               //This is the sentence that generates error:
               json=JSON.parse(http.responseText);

               st.loadJSON(json);
               //compute node positions and layout
               st.compute();
               //optional: make a translation of the tree
               st.geom.translate(new $jit.Complex(-200, 0), "current");
               st.onClick(node.id);    

            }
           }

         };

請你幫助我好嗎? 真的非常感謝你

問題是您的json格式無效。

 archivo.txt , (correct):
{"id":"53530","name":"Bifurcadores

pista11 , (incorrect):

{id:53530,name:Bifurcadores

在上面的示例中,您可以看到變量和值中缺少“”。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM