我想解析此JSON代碼示例:如何分析一些JSON代碼
{
"licenses": [
{
"id": "TN",
"value": "ar"
},
{"id": "FR", "value": "fr"} ,
{"id": "GB", "value": "en"} ,
{"id": "US", "value": "en"}
]
}
我有這樣的HTML網頁使用jQuery插件來做到這一點:
<!DOCTYPE html><!--HTML5 doctype-->
<html>
<head>
<title>Your New Application</title>
<meta http-equiv="Content-type" content="text/html; charset=utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum- scale=1.0, minimum-scale=1.0, user-scalable=0" />
<style type="text/css">
/* Prevent copy paste for all elements except text fields */
* { -webkit-user-select:none; -webkit-tap-highlight-color:rgba(255, 255, 255, 0); }
input, textarea { -webkit-user-select:text; }
body { background-color:white; color:black }
</style>
<script src='intelxdk.js'></script>
<script type="text/javascript">
/* This code is used to run as soon as Intel activates */
var onDeviceReady=function(){
//hide splash screen
intel.xdk.device.hideSplashScreen();
};
document.addEventListener("intel.xdk.device.ready",onDeviceReady,false);
</script>
</head>
<body>
<a href="pays.json" target="_blank">Open JSON file</a><br />
<input type="button" value="Get and parse JSON" class="button" />
<br />
<span id="results"></span>
<script src="libs/jquery-1.10.1.js"></script>
<script>
//When DOM loaded we attach click event to button
$(document).ready(function() {
//after button is clicked we download the data
$('.button').click(function(){
//start ajax request
$.ajax({
url: "pays.json",
//force to handle it as text
dataType: "text",
success: function(data) {
//data downloaded so we call parseJSON function
//and pass downloaded data
var json = $.parseJSON(data);
//now json variable contains data in json format
//let's display a few items
$('#results').html('Plugin name: ' + json.licenses[0].id + '<br />Author: ' + json.licenses[0].value);
}
});
});
});
</script>
</body>
</html>
PS:本JSON文件與HTML網頁位於相同的文件夾下。 問題是當我點擊「獲取並解析JSON」按鈕時,沒有任何顯示!
嘗試使用螢火蟲或類似的,看看你的Ajax請求被執行,如果有某種沿途錯誤的。 – Cyclonecode
'強制將其作爲文本處理。爲什麼? – dfsq
什麼是JS錯誤?此外,代碼中還存在很多問題:1.混合使用jQuery和傳統JavaScript; 2.改用''; 3.不要在CSS中使用'*'; 4.混合使用單引號和雙引號 – Raptor