javascript 如何使用Javascript读取位于iPhone PhoneGap项目文件夹中的json文件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16376313/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to read json file located in project folder in iPhone PhoneGap using Javascript
提问by hharry
I have to read a JSON file from folder that is located in a project.
我必须从位于项目中的文件夹中读取 JSON 文件。
I am using the following code:
我正在使用以下代码:
var obj = "www/places.json";
How can I read a JSON file located in project folder www
in iPhone PhoneGap using Javascript?
如何www
使用 Javascript读取位于iPhone PhoneGap项目文件夹中的 JSON 文件?
回答by Gajotres
You would read it just like it is on your server.
您会像在服务器上一样阅读它。
Solution 1 - jQuery
解决方案 1 - jQuery
If jQuery usage is not a problem then use it like this:
如果 jQuery 使用不是问题,那么像这样使用它:
//Load categories object JSON
jQuery.getJSON("categories.json", function(data){
// data is yours parsed object
});
Example :
例子 :
HTML :
HTML :
<!DOCTYPE html>
<html lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<meta name="viewport" content="width=device-width,initial-scale=1,maximum-scale=1,user-scalable=no"/>
<script src="http://www.fajrunt.org/js/jquery-1.9.1.min.js"></script>
<title>Read JSON Demo</title>
<script>
jQuery.getJSON("categories.json", function(data){
alert(data.balance);
});
</script>
</head>
<body>
Read JSON Demo
</body>
</html>
JSON file :
JSON 文件:
{"balance":1000.21,"num":100,"nickname":null,"is_vip":true,"name":"foo"}
Solution 2 - Pure javascript
解决方案 2 - 纯 javascript
If you want to use only vanilla javascript then this is a solution for you
如果您只想使用 vanilla javascript,那么这是适合您的解决方案
var xmlhttp;
var jsonObject;
// code for IE7+, Firefox, Chrome, Opera, Safari
if (window.XMLHttpRequest)
{
xmlhttp=new XMLHttpRequest();
}
// code for IE6, IE5
else
{
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange=function()
{
if (xmlhttp.readyState==4 && xmlhttp.status==200)
{
jsonObject = JSON.parse(xmlhttp.responseText);
alert(jsonObject.balance);
}
}
xmlhttp.open("GET","categories.json",true);
xmlhttp.send();
Example :
例子 :
HTML :
HTML :
<!DOCTYPE html>
<html lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<meta name="viewport" content="width=device-width,initial-scale=1,maximum-scale=1,user-scalable=no"/>
<title>Read JSON Demo</title>
<script>
var xmlhttp;
var jsonObject;
// code for IE7+, Firefox, Chrome, Opera, Safari
if (window.XMLHttpRequest)
{
xmlhttp=new XMLHttpRequest();
}
// code for IE6, IE5
else
{
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange=function()
{
if (xmlhttp.readyState==4 && xmlhttp.status==200)
{
jsonObject = JSON.parse(xmlhttp.responseText);
alert(jsonObject.balance);
}
}
xmlhttp.open("GET","categories.json",true);
xmlhttp.send();
</script>
</head>
<body>
Read JSON Demo
</body>
</html>
JSON file :
JSON 文件:
{"balance":1000.21,"num":100,"nickname":null,"is_vip":true,"name":"foo"}
回答by Jayprakash Dubey
I had same problem and got through with following snippet:
我遇到了同样的问题,并通过以下代码段解决了:
dojo.ready(function(){
var xhrArgs = { url: "file:///Users/Desktop/configJSON.txt", handleAs: "json", load: function(data){ targetNode.innerHTML = data; // Your data from JSON alert("Name : "+data.fields[0].name+" Type : "+data.fields[0].type+" Alias : "+data.fields[0].alias +" Editable : "+data.fields[0].editable); }, error: function(error){ // targetNode.innerHTML = "An unexpected error occurred: " + error; alert("An unexpected error occurred: " + error); } } // Call the asynchronous xhrGet var deferred = dojo.xhrGet(xhrArgs); }); </script>
dojo.ready(function(){
var xhrArgs = { url: "file:///Users/Desktop/configJSON.txt", handleAs: "json", load: function(data){ targetNode.innerHTML = data; // Your data from JSON alert("Name : "+data.fields[0].name+" Type : "+data.fields[0].type+" Alias : "+data.fields[0].alias +" Editable : "+data.fields[0].editable); }, error: function(error){ // targetNode.innerHTML = "An unexpected error occurred: " + error; alert("An unexpected error occurred: " + error); } } // Call the asynchronous xhrGet var deferred = dojo.xhrGet(xhrArgs); }); </script>
</head>
<body>
<div id="licenseContainer"></div>
<body>