我需要解析一个JSON文件,并在我的网页本地显示其数据
I need to Parse a JSON file and show its data on my web page locally
我试图获取我的JSON文件使用ajax请求和它不加载,所有我想要的是显示一些特定的数据包含在JSON文件通过使用一些变量将包含解析的数据,它的输出将显示在浏览器的网页上。但是当我运行时,它只显示了我创建的html元素,在调试器中,我知道点击函数后的代码没有执行它。我使用的代码如下:
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Reading Json Data from Json File</title>
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
<script src="scripts/jquery.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 () {
debugger;
//start ajax request
$.ajax({
url: "data.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.name + '<br />Author: ' + json.author.name);
}
});
});
});
</script>
<style>
.button{
margin:20px;
font-size:16px;
font-weight: bold;
padding:5px 10px;
}
</style>
</head>
<body>
<a href="Json file/data.json" target="_blank">Open Json File</a><br />
<input type="button" value="Get and Parse JSON" class="button" /><br />
<span id="results"></span>
</body>
</html>
Besides My JSON file named"Data" contains:
{
"name": "select2",
"title": "Select2",
"description": "Select2 is a jQuery based replacement for select boxes. It supports searching, remote data sets, and infinite scrolling of results.",
"keywords": [
"select",
"autocomplete",
"typeahead",
"dropdown",
"multiselect",
"tag",
"tagging"
],
"version": "3.4.2",
"author": {
"name": "Igor Vaynberg",
"url": "https://github.com/ivaynberg"
},
"licenses": [
{
"type": "Apache",
"url": "http://www.apache.org/licenses/LICENSE-2.0"
},
{
"type": "GPL v2",
"url": "http://www.gnu.org/licenses/gpl-2.0.html"
}
],
"bugs": "https://github.com/ivaynberg/select2/issues",
"homepage": "http://ivaynberg.github.com/select2",
"docs": "http://ivaynberg.github.com/select2/",
"download": "https://github.com/ivaynberg/select2/tags",
"dependencies": {
"jquery": ">=1.7.1"
}
}
,我已经将这个文件加载到项目资源管理器的文件夹中。
$.parseJSON(data)可以返回一个数组
json[0].name
然后
$('#results').html('Plugin name: ' + json[0].name +
'<br />Author: ' + json[0].author.name);
以上代码不能在chrome中工作,因为它不允许跨域请求。为了避免这种情况,您可以在web服务器内运行代码。没有web服务器的Ajax URL请求将无法在chrome中工作。用firefox检查一下是否可以工作。所以最好在web服务器内运行你的应用程序(html+data)。请检查下面的代码
//When DOM loaded we attach click event to button
$(document).ready(function () {
//after button is clicked we download the data
$('.button').click(function () {
debugger;
//start ajax request
$.ajax({
url: "data.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.name + '<br />Author: ' + json.author.name);
}
});
});
});
.button{
margin:20px;
font-size:16px;
font-weight: bold;
padding:5px 10px;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Reading Json Data from Json File</title>
</head>
<body>
<a href="data.json" target="_blank">Open Json File</a><br />
<input type="button" value="Get and Parse JSON" class="button" /><br />
<span id="results"></span>
</body>
</html>
相关文章:
- 为什么这个jQuery可以作为一个小提琴工作,但在我的网页上没有
- 如何将AutoAnchors jquery插件整合到我的网页中
- I'我试图在我的网页上动态地上下移动元素.我该怎么做
- 如何为我的网页中的资源创建特定页面
- 将音频添加到我的网页
- Html5 Canvas Javascript使我的网页链接没有响应
- 我想听听服务器上的更改,并相应地更新我的网页.我如何用Javascript做到这一点
- 数据库中的数据未正确插入我的网页
- 当用户向下滚动我的网页时,如何删除chrome地址栏周围的边框
- 我的jQuery获胜't在我的网页上工作[Repost]
- 我的jQuery获胜't在我的网页上工作
- 我可以'我无法让我的网页浏览JScript中选择列表中的trhorugh URL
- 选项卡分组和选项卡着色:如何将此功能应用于从我的网页打开的选项卡,而不考虑浏览器
- 如何在我的网页中通过按钮操作添加书签
- 有没有办法让我用另一个网站的图片自动填充我的网页
- 在我的网页中没有获得倒计时功能
- 如何将变量传递到我的网页
- 如果我滚动并刷新我的网页,我的导航标题会失去其属性.JQuery
- 在我的网页中显示一组图像
- 图像打印我的网页中的文档