使用javascript读取Json文件数据?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16649829/
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
Read Json file data using javascript?
提问by Rachel
function uploadJsonFunction(){
var jsonURL = "C:\Users\My Documents\new\WebContent\JsonFiles\TreeJson\countries.json";
}
- countries.json is valid json and the above path has countries.json file.
- I want to read the countries.json file whole data/contents using javascript.
- country.json 是有效的 json 并且上面的路径有 country.json 文件。
- 我想使用 javascript 读取 countries.json 文件的整个数据/内容。
Here is my Json:
这是我的 Json:
{
identifier: 'id',
label: 'name',
items: [
{
id: 'AF',
name: 'Africa',
type:'continent'
}
]
}
回答by Arkadiusz Cie?liński
$.ajax({
url: "\countries.json",
success: function (data) {
var obj = JSON.parse(data);
}
});
For safety reasons, The C:\Users\My Documents\new\WebContent\JsonFiles\TreeJson\countries.jsonurl won't work in a browser.
出于安全原因,该C:\Users\My Documents\new\WebContent\JsonFiles\TreeJson\countries.jsonurl 在浏览器中不起作用。
回答by 456qwe123asd
Please try to see this example.
请尝试查看此示例。
-or-
-或者-
How to use a JSON file in javascript
Hope may help you.
希望可以帮到你。
回答by EmiX
Why not to try Google: http://www.json.org/js.html
为什么不试试谷歌:http: //www.json.org/js.html
I used this for Android app that worked with PhoneGap(HTML+Javascript+JQuery)
我将此用于与 PhoneGap(HTML+Javascript+JQuery) 一起使用的 Android 应用程序
Worked fine.
工作得很好。
My Method:
我的方法:
var link_name = pathtojson.json
$.getJSON(link_name,
function(data){
var ttext = (data["SearchResponse"]["Translation"]["Results"][0]["TranslatedTerm"]);
document.getElementById('ttexti').innerHTML = ttext;
}
);

