[英]Unable to parse JSON string error in my Phonegap project
從PHP獲取JSON時遇到問題。 下面的代碼是有效的:
[{
"vehicleId": "1",
"status": "Running",
"position": "x",
"battery": "25",
"distanceTravelled": "123",
"destination": "y",
"freeSeats": "2",
"speed": "32"
}
]
但是,我的新版本的代碼就是這樣(數字值中沒有引號),它給出了一個解析Json錯誤,它沒有顯示任何內容。
[{
"vehicleId": 1,
"status": "Running",
"position": "x",
"battery": 25,
"distanceTravelled": 123,
"destination": "y",
"freeSeats": 2,
"speed": 32
}
]
我在jsonlint.com檢查了格式,它是有效的。 Eclipse中的日志錯誤是:
05-09 21:37:25.536: E/Web Console(336): SyntaxError: Unable to parse JSON string at file:///android_asset/www/MobileMan.js:37
34 xmlhttp.open("GET",url,false);
35 xmlhttp.send();
36 var json = xmlhttp.responseText;
37 obj = JSON.parse(json);
我還在Chrome上的計算機上嘗試了Javascript代碼,一切正常。 我無法解決問題。 謝謝你的回答。
好吧,如果解析器拋出錯誤,那么它意味着提供給parse的值不是純JSON對象。
另一方面,關於JSON.stringify
因此,在您的情況下,您首先需要將獲得的結果轉換為JSON對象,然后嘗試解析它。
a = [{
"vehicleId": 1,
"status": "Running",
"position": "x",
"battery": 25,
"distanceTravelled": 123,
"destination": "y",
"freeSeats": 2,
"speed": 32
}
];
var b = JSON.stringify(a);
var c = JSON.parse(b);
console.log(c)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.