简体   繁体   English

使用javascript读取Json文件数据?

[英]Read Json file data using javascript?

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. countries.json是有效的json,上面的路径有countries.json文件。
  • I want to read the countries.json file whole data/contents using javascript. 我想使用javascript读取countries.json文件的整个数据/内容。

Here is my Json: 这是我的Json:

{
  identifier: 'id',
  label: 'name',
  items: [
    {
      id: 'AF',
      name: 'Africa',
      type:'continent'
    }
  ]
}
$.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.json url won't work in a browser. 出于安全原因, C:\\Users\\My Documents\\new\\WebContent\\JsonFiles\\TreeJson\\countries.json url将无法在浏览器中运行。

Please try to see this example. 请尝试查看此示例。

Jquery to get Json Jquery得到Json

-or- -要么-

How to use a JSON file in javascript 如何在javascript中使用JSON文件

Hope may help you. 希望可以帮到你。

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的Android应用程序(HTML + Javascript + JQuery)

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;
}
);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM