2

我收到 SyntaxError: JSON.parse: expected double-quoted property name。

我对 PHP 关联进行编码并转义了这样的非法字符:

var TweetsAnalized = '<?php echo addslashes($Row_Words_Repeated_Relation_to_json) ?>';

然后,当我使用 JSON 时,我会这样解析它:var tweets = JSON.parse(TweetsAnalized);

我得到了错误。

这是数组的样子:

'{\"\":[[\"trial\",\"Cairo\",\"#RamletBoulaq\",\"march\",\"heading\",\"prosecutor\",\"office\",\"#nilecity\",\"#niletowers\",\"Muslim\",\"Brotherhood\",\"chants\",\"chanting\",\"building\",\"9\",\"#AdminCourt\",\"#ConstituentAssembly\",\"dissolution\",\"will\",\"draft\",\"hold\",\"dissolve\",\"today\",\"#Maspero\",\"March\",\"6\",\"#Dokki\",\"#Tahrir\",\"held\",\"protesters\",\"Chants\",\"Bread\",\"freedom\",\"issues\",\"#Morsy\",\"Anti\",\"Mohamed\",\"Mahmoud\",\"people\",\"regime\",\"Al\",\"President\",\"Supreme\",\"Constitutional\",\"Court\",\"postponed\",\"#Egypt\",\"civil\",\"revolution\",\"#Oct19\",\"tomorrow\",\"\\n\",\"writes\",\"greater\",\"good\",\"downfall\",\"constitutional\",\"declaration\",\"Morsy\",null,\"game\",\"wins\",\"Egypt\",\"worst\",\"option\",\"Syrian\",\"refugees\",\"vote\",\"funeral\",\"press\",\"conference\",\"Tahrir\",\"support\",\"decree\",\"crisis\",\"seized\",\"2012\",\"law\",\"clashes\",\"#FJP\",\"offices\",\"injuries\",\"#DNE\",\"latest\",\"bid\",\"#BREAKING\",\"presidential\",\"#Jika\",\"journalist\",\"detained\",\"forces\",\"rebels\",\"#Abdeen\",\"creating\",\"Monday\",\"protests\",\"urges\",\"chief\",\"ahead\",\"attempting\",\"#MB\",\"sit\",\"meeting\",\"Tuesday\",\"injured\",\"temporary\",\"Clashes\",\"Qasr\",\"Shia\",\"#Muslims\",\"claim\",\"entering\",\"#Ashura\",\"#Palestinian\",\"November\",\"#Sinai\",\"#Alexandria\",\"team\",\"Gadalla\",\"decisions\"],[2,2,4,6,4,2,2,4,4,5,6,3,2,2,2,2,4,3,3,2,2,2,4,2,2,2,3,20,3,3,3,2,3,2,16,2,4,4,4,3,6,5,2,2,2,3,34,2,2,2,2,2,5,2,2,2,4,5,7,3,2,2,3,2,2,2,2,2,2,2,3,3,3,3,3,2,2,2,2,2,2,2,33,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,4,2,3,3,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2]],\"#Tahrir\":[[\"#Morsy\",\"supporters\",\"Mohamed\",\"Mahmoud\",\"anti\",\"Morsy\",\"protesters\",\"square\",\"Kasr\",\"Nil\",\"empty\",\"pro\",\"throwing\",\"Pro\",\"charge\",\"rock\",\"St\",\"injuries\",\"drove\",\"injured\",\"man\",\"carried\",\"face\",\"march\",\"#Dokki\",\"\",\"held\",\"Chants\",\"Bread\",\"freedom\",\"dissolution\",\"#ConstituentAssembly\",\"heading\",\"demanding\",\"ensued\",\"#FJP\",\"leader\",\"Muslim\",\"Brotherhood\",\"calls\",\"head\",\"building\",\"Clashes\",\"clashes\",\"High\",\"Court\",\"authority\",\"chants\",\"fights\",\"continue\",\"Anti\",\"people\",\"demand\",\"fall\",\"president\",\"groups\",\"parties\",\"holding\",\"#Egypt\",\"civil\",\"Protesters\",\"#Oct19\",\"March\",\"Al\",\"Sayeda\",\"Zeinab\",\"moving\",\"revolution\",\"Badie\",\"today\'s\",\"protests\",\"will\",\"growing\",\"number\",\"street\",\"Cairo\",\"\\n\",\"constitutional\",\"declaration\",\"#DNE\",\"Qasr\",\"National\",\"Rescue\",\"Front\",\"sit\",\"low\",\"Follow\",\"updates\",\"Police\",\"front\",\"gas\",\"Ainy\",\"call\",\"Tuesday\",\"Pictures\",\"infront\",\"demonstration\",\"CSF\",\"#Nov23\",\"Tahrir\",\"thousands\",\"#nov23\",\"path\",\"motorcycles\"],[18,4,8,7,7,6,11,4,2,2,2,5,2,2,3,2,2,2,2,3,2,2,2,10,5,20,4,4,2,2,3,2,4,2,2,2,2,5,6,2,3,2,4,5,3,3,2,4,2,5,2,3,2,2,2,2,2,2,16,2,6,13,3,10,4,4,2,2,2,3,3,3,2,2,2,2,2,2,3,3,6,2,2,2,2,2,2,2,2,3,3,5,2,3,2,2,2,2,2,2,2,10,2,2]],\"#Egypt\":[[\"today\",\"Egyptian\",\"protest\",\"current\",\"conditions\",\"return\",\"Rafah\",\"amid\",\"security\",\"#Copts\",\"arrested\",\"#Syria\",\"International\",\"Mohamed\",\"will\",\"year\",\"charges\",\"years\",\"lawsuit\",\"religion\",\"protesters\",\"Al\",\"presidential\",\"demanding\",\"building\",\"office\",\"#Morsy\",\"gas\",\"pile\",\"National\",\"Front\",\"Supreme\",\"Constitutional\",\"court\",\"constitutional\",\"continue\",\"strike\",\"demands\",\"#Sinai\",\"Friday\",\"#Oct19\",\"#Tahrir\",\"groups\",\"\",\"representative\",\"#FJP\",\"head\",\"party\",\"#BREAKING\",\"#DNE\",\"clashes\",\"front\",\"offices\",\"injuries\",\"regime\",\"held\",\"#Damanhour\",\"President\",\"Council\",\"Judges\",\"declaration\",\"attempting\",\"#MB\",\"changes\",\"location\",\"Tuesday\",\"#Abdeen\",\"Brotherhood\",\"High\",\"Court\",\"Clashes\",\"Rescue\",\"sit\",\"Muslim\",\"march\",\"crisis\",\"BREAKING\",\"call\",\"Morsy\",\"demonstration\",\"#Alexandria\",\"#Nov23\"],[4,2,3,3,2,2,2,2,2,3,2,2,2,2,2,2,2,2,2,2,2,4,2,2,2,2,16,2,2,4,4,2,2,2,4,2,2,2,2,2,3,16,2,34,2,3,2,2,4,27,4,2,2,2,2,3,2,4,2,2,4,2,6,2,2,5,2,3,2,2,2,3,2,2,3,2,2,2,2,2,2,2]],\"#Morsy\":[[\"judges\",\"supporters\",\"prosecutor\",\"general\",\"#Tahrir\",\"Mohamed\",\"Mahmoud\",\"anti\",\"Morsy\",\"protesters\",\"square\",\"Kasr\",\"Nil\",\"pro\",\"throwing\",\"Pro\",\"charge\",\"rock\",\"injuries\",\"Brotherhood\",\"High\",\"Court\",\"#FJP\",\"Muslim\",\"authority\",\"chants\",\"issues\",\"\",\"Anti\",\"people\",\"demand\",\"fall\",\"president\",\"office\",\"#Egypt\",\"President\",\"hours\",\"finished\",\"clashes\",\"#DNE\",\"#BREAKING\",\"decree\",\"constitutional\",\"declaration\",\"temporary\",\"will\",\"held\",\"Tuesday\",\"#MB\"],[3,4,2,2,18,7,5,6,5,6,3,2,2,5,2,2,2,2,3,4,2,2,2,3,2,2,2,16,2,3,2,2,2,2,16,5,2,2,2,7,2,2,2,3,2,2,2,2,2]],\"#DNE\":[[\"#BREAKING\",\"#Egypt\",\"#Tahrir\",\"clashes\",\"#FJP\",\"offices\",...al\",\"hold\",\"meeting\",\"discuss\",\"move\",\"#nov23\",\"#tahrir\",\"#egypt\"],[2,2,2,2,2,2,2,2,2]],\"team\":[[\"meeting\",\"#nov23\",\"#tahrir\",\"#egypt\",\"decides\",\"resign\",\"presidential\",\"\"],[2,2,2,2,2,2,2,2]],\"support\":[[\"\"],[3]],\"revolution\":[[\"will\",\"\",\"#Tahrir\"],[2,2,2]],\"protest\":[[\"governorates\",\"#Egypt\",\"#Oct19\"],[2,3,2]],\"prosecutor\":[[\"#RamletBoulaq\",\"office\",\"#nilecity\",\"#niletowers\",\"general\",\"#Morsy\"],[2,2,2,2,3,2]],\"president\":[[\"#Morsy\",\"people\",\"#Tahrir\"],[2,2,2]],\"postponed\":[[\"\"],[3]],\"office\":[[\"#RamletBoulaq\",\"march\",\"General\",\"prosecutor\",\"#nilecity\",\"#niletowers\",\"general\",\"#Egypt\",\"#Morsy\"],[2,2,2,2,2,2,2,2,2]],\"leader\":[[\"#FJP\",\"Brotherhood\",\"calls\",\"#Tahrir\",\"head\",\"building\"],[2,2,2,2,2,2]],\"held\":[[\"march\",\"#Tahrir\",\"\",\"#Egypt\",\"Tuesday\",\"#Morsy\"],[2,4,3,3,2,2]],\"front\":[[\"#Egypt\",\"gas\",\"#Tahrir\"],[2,2,3]],\"draft\":[[\"\",\"Supreme\",\"Constitutional\",\"rejects\"],[2,2,2,2]],\"current\":[[\"Egyptian\",\"#Egypt\"],[2,3]],\"crisis\":[[\"\",\"#Egypt\"],[3,2]],\"court\":[[\"high\",\"#Egypt\"],[2,2]],\"conference\":[[\"\"],[3]],\"calls\":[[\"#FJP\",\"leader\",\"Brotherhood\",\"#Tahrir\",\"head\",\"building\"],[2,2,2,2,2,2]],\"Zeinab\":[[\"Sayeda\",\"march\",\"#Tahrir\",\"#Oct19\",\"moving\"],[3,3,4,4,2]],\"US\":[],\"Sayeda\":[[\"Zeinab\",\"march\",\"#Tahrir\",\"#Oct19\",\"moving\"],[3,3,4,4,2]],\"Presidential\":[[\"#nov23\",\"team\",\"hold\",\"meeting\",\"tomorrow\",\"discuss\",\"move\",\"#tahrir\",\"#egypt\"],[3,2,2,2,2,2,2,2,2]],\"National\":[[\"Front\",\"#Egypt\",\"Rescue\",\"#Tahrir\"],[3,4,3,2]],null:[[\"declaration\",\"\"],[2,3]],\"Front\":[[\"National\",\"#Egypt\",\"Rescue\",\"#Tahrir\"],[3,4,3,2]],\"Egypt\":[[\"worst\",\"option\",\"Syrian\",\"refugees\",\"\"],[2,2,2,2,3]],\"Constitutional\":[[\"#Egypt\",\"Supreme\",\"rejects\",\"draft\",\"\",\"declaration\"],[2,2,2,2,2,2]],\"Chants\":[[\"\",\"Bread\",\"freedom\",\"dissolution\",\"#ConstituentAssembly\",\"#Dokki\",\"march\",\"heading\",\"#Tahrir\"],[2,2,2,2,2,2,2,2,4]],\"Cairo\":[[\"\",\"#Tahrir\"],[2,2]],\"#tahrir\":[[\"#nov23\",\"Presidential\",\"team\",\"hold\",\"meeting\",\"tomorrow\",\"discuss\",\"move\",\"#egypt\"],[4,2,2,2,2,2,2,2,3]],\"#BREAKING\":[[\"#DNE\",\"#Egypt\",\"#Morsy\",\"\"],[4,4,2,2]],\"urges\":[[\"\"],[2]],\"trial\":[[\"contempt\",\"religion\",\"\"],[2,2,2]],\"today\'s\":[[\"protests\",\"#Oct19\",\"#Tahrir\"],[2,2,3]],\"thousands\":[[\"#Tahrir\",\"#nov23\"],[2,2]],\"strike\":[[\"#Egypt\"],[2]],\"street\":[[\"#Tahrir\"],[2]],\"sit\":[[\"#DNE\",\"\",\"#Tahrir\",\"#Egypt\"],[2,2,2,2]],\"security\":[[\"#Egypt\"],[2]],\"rights\":[[\"human\"],[2]],\"residents\":[[\"#RamletBoulaq\",\"#niletowers\",\"#nilecity\"],[2,2,2]],\"religion\":[[\"trial\",\"contempt\",\"#Copts\",\"#Egypt\"],[2,2,2,2]],\"regime\":[[\"\",\"people\",\"#DNE\",\"#Egypt\"],[3,2,2,2]],\"moving\":[[\"march\",\"Sayeda\",\"Zeinab\",\"#Oct19\",\"#Tahrir\"],[2,2,2,2,2]],\"minister\":[],\"man\":[[\"#Tahrir\"],[2]],\"leave\":[[\"#RamletBoulaq\",\"#nilecity\",\"#niletowers\"],[2,2,2]],\"law\":[[\"\"],[2]],\"justice\":[],\"judges\":[[\"#Morsy\",\"President\"],[3,2]],\"injuries\":[[\"#Morsy\",\"#Tahrir\",\"#Egypt\",\"\"],[3,2,2,2]],\"high\":[[\"court\",\"building\"],[2,2]],\"groups\":[[\"#Tahrir\",\"#Egypt\"],[2,2]],\"freedom\":[[\"\",\"Bread\",\"dissolution\",\"#Tahrir\"],[3,2,2,2]],\"face\":[[\"#Tahrir\"],[2]],\"expected\":[[\"will\"],[2]],\"entering\":[[\"\"],[2]],\"demands\":[[\"#Egypt\"],[2]],\"demanding\":[[\"#Tahrir\",\"#Egypt\"],[2,2]],\"decree\":[[\"\",\"#Morsy\"],[3,2]],\"constitution\":[[\"#ConstituentAssembly\"],[2]],\"chief\":[[\"\"],[2]],\"charge\":[[\"#Morsy\",\"rock\",\"#Tahrir\",\"protesters\",\"drove\"],[2,2,3,2,2]],\"car\":[[\"bomb\"],[2]],\"Thousands\":[],\"Supreme\":[[\"Constitutional\",\"rejects\",\"draft\",\"\"],[2,2,2,2]],\"Rescue\":[[\"National\",\"Front\",\"#Tahrir\",\"#Egypt\"],[2,3,2,3]],\"Pictures\":[[\"#Tahrir\"],[2]],\"October\":[[\"#AdminCourt\",\"postpones\",\"#ConstituentAssembly\",\"case\",\"16\",\"#EgyCourts\"],[2,2,2,2,2,2]],\"General\":[[\"office\",\"Egyptian\"],[2,2]],\"Erian\":[[\"#FJP\",\"head\",\"party\",\"Al\"],[2,3,3,2]],\"Egyptian\":[[\"current\",\"#Egypt\",\"General\"],[2,2,2]],\"Council\":[[\"#Egypt\"],[2]],\"CSF\":[[\"High\",\"Court\",\"protesters\",\"#Tahrir\"],[2,2,2,2]],\"BREAKING\":[[\"#Egypt\"],[2]],\"April\":[[\"will\"],[2]],\"#egypt\":[[\"Presidential\",\"team\",\"hold\",\"meeting\",\"tomorrow\",\"discuss\",\"move\",\"#nov23\",\"#tahrir\"],[2,2,2,2,2,2,2,3,3]],\"#Sinai\":[[\"#Egypt\",\"\"],[2,2]],\"#Nov23\":[[\"#Tahrir\",\"#Egypt\"],[2,2]],\"#FreeAlber\":[[\"lawyer\",\"prosecution\",\"#PT\",\"moved\"],[2,2,2,2]],\"#EgyCourts\":[[\"#AdminCourt\",\"postpones\",\"#ConstituentAssembly\",\"case\",\"October\",\"16\"],[2,2,3,3,2,2]],\"#Copts\":[[\"#Egypt\",\"religion\"],[3,2]],\"#Abdeen\":[[\"Palace\",\"\",\"#MB\",\"changes\",\"location\",\"Tuesday\",\"#DNE\",\"#Egypt\"],[2,2,2,2,2,2,2,2]]}';

请需要有关该问题的帮助。

编辑:这里是关于我如何获得 $Row_Words_Repeated_Relation 的整个过程。

while($Repeatitive_max > 4)
     { 
         $Row_Words_Repeated_TweetsList= array();     
             $Row_Words_Repeated_counter=0;
             $IRow=0;
             $counter2=0;
             $Icolo=0;
             $Row_Words_Repeated_Count=array();
             $Row_Words_Repeated_Count[0][0]=" ";

             for($RRow=0 ;$RRow < sizeof($Array_TweetExplode); $RRow++)
             {
                 if(in_array($BigMatrix_Filtered[0][$Repeatition_position], $Array_TweetExplode[$RRow]))
                 {      
                     for($ICol=0;$ICol<sizeof($Array_TweetExplode[$RRow]);$ICol++)
                     {
                           if(!in_array($Array_TweetExplode[$RRow][$ICol],$Row_Words_Repeated_Count[0])
                           && $BigMatrix_Filtered[0][$Repeatition_position] != $Array_TweetExplode[$RRow][$ICol])
                           {  
                             $Row_Words_Repeated_Count[0][$counter2]= $Array_TweetExplode[$RRow][$ICol];                             
                             $counter2++;
                           }
                     }
                 }
             }
             for($RRow=0 ;$RRow < sizeof($Array_TweetExplode); $RRow++)
             {
                 if(in_array($BigMatrix_Filtered[0][$Repeatition_position], $Array_TweetExplode[$RRow]))
                 {      
                     for($ICol=0;$ICol<sizeof($Array_TweetExplode[$RRow]);$ICol++)
                     {
                        $Row_Words_Repeated_TweetsList[$IRow][$ICol]= $Array_TweetExplode[$RRow][$ICol];
                     }
                     $IRow++;
                 }
             }
             $RowCounter=1;
             for($Row=0;$Row<sizeof($Array_TweetExplode);$Row++)
             {
                 if(in_array($BigMatrix_Filtered[0][$Repeatition_position], $Array_TweetExplode[$Row]))
                 {
                      for($ColCounter=0;$ColCounter<sizeof($Row_Words_Repeated_Count[0]);$ColCounter++)
                      {
                          $Row_Words_Repeated_Count[$RowCounter][$ColCounter]=0;
                      }
                      $RowCounter++;
                 }
             }
             $RowCounter=1;
             for($RRow=0;$RRow<sizeof($Row_Words_Repeated_TweetsList);$RRow++)
             {
                 for($CCol=0;$CCol<sizeof($Row_Words_Repeated_TweetsList[$RRow]);$CCol++)
                 {

                    $Position = array_search($Row_Words_Repeated_TweetsList[$RRow][$CCol],$Row_Words_Repeated_Count[0]);
                    if($Position !== false)
                    {
                        if($Position != NULL || $Position >= 0 )
                        {            
                             $Row_Words_Repeated_Count[$RRow+1][$Position] = 1;
                             $var=0;
                        }                    
                    }             
                 }
                 $var =0;                                           
             }
             $Repeatiton_count=0;
             $finalRow2=sizeof($Row_Words_Repeated_Count);
             for($CCol=0;$CCol<sizeof($Row_Words_Repeated_Count[0]);$CCol++)
             {   
                for($RRow=1;$RRow<sizeof($Row_Words_Repeated_Count)-1;$RRow++)
                {
                    if($Row_Words_Repeated_Count[$RRow][$CCol] == 1)
                    {
                        $Repeatiton_count++;
                    }                
                }


                $Row_Words_Repeated_Count[$finalRow2][$CCol] = $Repeatiton_count;
                $Repeatiton_count=0;  

             }
             $Row_Words_Repeated_Relation[$BigMatrix_Filtered[0][$Repeatition_position]]= array();
             $counter2=0;    
             for($CCol=0;$CCol<sizeof($Row_Words_Repeated_Count[$finalRow2]);$CCol++)
             {
                if($Row_Words_Repeated_Count[$finalRow2][$CCol] > 1)
                {
                    $Row_Words_Repeated_Relation[$BigMatrix_Filtered[0][$Repeatition_position]][0][$counter2]= $Row_Words_Repeated_Count[0][$CCol];
                    $Row_Words_Repeated_Relation[$BigMatrix_Filtered[0][$Repeatition_position]][1][$counter2]= $Row_Words_Repeated_Count[$finalRow2][$CCol];
                    $counter2++;

                } 
             }
             if($Maximum_Counter >= sizeof($Array_OfCounters))
             {
                 break;
             }
             $Repeatitive_max = $Array_OfCounters[$Maximum_Counter];
             $FLAG=0;
             $InterCounter=0;
             for($CCol=0;$CCol<sizeof($BigMatrix_Filtered[$finalRow]);$CCol++)
             {
                if($Repeatitive_max == $BigMatrix_Filtered[$finalRow][$CCol])
                {

                     foreach ($Row_Words_Repeated_Relation as $key_name => $key_value) 
                     {
                        if($BigMatrix_Filtered[0][$CCol] != $key_name)
                        {
                            $InterCounter++;   
                        }
                      }
                      $Zaha2 = sizeof($Row_Words_Repeated_Relation);
                      if($InterCounter==sizeof($Row_Words_Repeated_Relation))
                      {
                          $Repeatition_position = $CCol;
                          $FLAG=1;
                          $InterCounter=0;
                          break;
                      }
                      else
                      {

                          $InterCounter=0;;
                      }

                }
                if($FLAG==1)
                {
                    $FLAG=0;
                    break;                     
                } 
             }

             $WholeRepeatedWordsCounter++;
             $Maximum_Counter++;


     }
     return $Row_Words_Repeated_Relation;

我从数据库 BTW 中检索,当 While 循环设置为 > 4(如列出的代码中)时,该过程工作正常,但是当它设置为大于 2 时,它会给我错误。此外,当我尝试向数据库添加更多数据时,它给了我同样的错误。

4

2 回答 2

1

问题是你混淆了不同的世界。addslashes曾经是用于 SQL 查询的东西(尽管有些不好,因为它不是特定于数据库引擎的)。绝对没有理由将它与 JSON 混合。

你想要的是这样的

var TweetsAnalyzed = <?php echo json_encode($Row_Words_Repeated_Relation); ?>;

这会将 PHP 数组/对象转换为正确的 JavaScript 对象(因为 JSON 正是这样:具有更严格语法的 JavaScript 对象字面量)。您不必转义任何东西等 -json_encode始终输出有效的 JavaScript。

如果$Row_Words_Repeated_Relation_to_json实际包含返回值,json_encode只需像这样使用它:

var TweetsAnalyzed = <?php echo $Row_Words_Repeated_Relation_to_json; ?>;
于 2012-12-01T02:33:34.310 回答
0

你试过不带引号吗?

var TweetsAnalized = <?php echo $Row_Words_Repeated_Relation_to_json; ?>;
于 2012-12-01T02:34:42.683 回答