SyntaxError: JSON.parse: expected double-quoted property name at line 4 column 13 of the JSON data的解决办法

 1 <?php
 2     $uphone = $_POST["userPhone"];
 3     if($uphone == "139"){//手机号不可用
 4         echo '{
 5             "status":0,
 6             "message":"手机号不可用",
 7             }';
 8     } else {//手机号可用
 9         echo '{
10             "status":1,
11             "message":{
12                 "tips":"手机号可用",
13                 "phoneform":"中国电信",
14             }
15         }';
16     }
17 ?>

运行以上的代码会报这样的错误:SyntaxError: JSON.parse: expected double-quoted property name at line 4 column 13 of the JSON data。原因是数组赋值时格式出现了错误,最后一个属性不该加逗号。如:("message":"手机号不可用" )以及("phoneform":"中国电信" ),后面也不能加入任何注释。比如改成这样("message":"手机号不可用" //注释)也会报错:SyntaxError: JSON.parse: expected ',' or '}' after property value in object at line 3 column 32 of the JSON data

改成如下的代码即可:

 1 <?php
 2     $uphone = $_POST["userPhone"];
 3     if($uphone == "139"){//手机号不可用
 4         echo '{
 5             "status":0,
 6             "message":"手机号不可用" 
 7             }';
 8     } else {//手机号可用
 9         echo '{
10             "status":1,
11             "message":{
12                 "tips":"手机号可用",
13                 "phoneform":"中国电信" 
14             }
15         }';
16     }
17 ?>

猜你喜欢

转载自www.cnblogs.com/banyouxia/p/10945498.html