我想解析此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