jQuery JSON 解析文件路径

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16991341/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 18:13:40  来源:igfitidea点击:

JSON Parse File Path

javascriptjqueryjsonparsing

提问by jagger

I'm stuck trying to get the correct path to the local file. I have the following directories:

我一直在尝试获取本地文件的正确路径。我有以下目录:

Resources ->
   data ->
       file.json
   js ->
     folder ->
        script.js
   html ->
      folder ->
         file1.html

I'm executing script.jsfrom file1.html, with js code:

script.jsfile1.html,执行js 代码:

var answers = JSON.parse('../../data/file.json');
alert(answers);

But it doesn't work, even alert is not starting. What is wrong?

但它不起作用,甚至警报也没有启动。怎么了?

Also I've tried this:

我也试过这个:

function readJSON(file) {
    var request = new XMLHttpRequest();
    request.open('GET', file, false);
    request.send(null);
    if (request.status == 200)
        return request.responseText;
};

var temp = readJSON('../../data/file.json');
alert(temp);

Alert undefined in this case.

在这种情况下警报未定义。

回答by karthikr

Since it is in the directory data/, You need to do:

由于它在目录中data/,您需要执行以下操作:

file path is '../../data/file.json'

文件路径是 '../../data/file.json'

$.getJSON('../../data/file.json', function(data) {         
    alert(data);
});

Pure JS:

纯JS:

   var request = new XMLHttpRequest();
   request.open("GET", "../../data/file.json", false);
   request.send(null)
   var my_JSON_object = JSON.parse(request.responseText);
   alert (my_JSON_object.result[0]);

回答by josephnvu

This solution uses an Asynchronous call. It will likely work better than a synchronous solution.

此解决方案使用异步调用。它可能比同步解决方案更有效。

var request = new XMLHttpRequest();
request.open("GET", "../../data/file.json", false);
request.send(null);
request.onreadystatechange = function() {
  if ( request.readyState === 4 && request.status === 200 ) {
    var my_JSON_object = JSON.parse(request.responseText);
    console.log(my_JSON_object);
  }
}

回答by maazza

Loading local JSON file

加载本地 JSON 文件

Use something like this

使用这样的东西

$.getJSON("../../data/file.json", function(json) {
    console.log(json); // this will show the info in firebug console 
    alert(json);
});

回答by Sumukh Bhandarkar

var request = new XMLHttpRequest();
request.open("GET","<path_to_file>", false);
request.send(null);
var jsonData = JSON.parse(request.responseText);

This code worked for me.

这段代码对我有用。

回答by Mircea

My case of working code is:

我的工作代码案例是:

var request = new XMLHttpRequest();
request.open("GET", "<path_to_file>", false);
request.overrideMimeType("application/json");
request.send(null);
var jsonData = JSON.parse(request.responseText);
console.log(jsonData);

回答by Kristaps Karlsons

If Resourcesis the root path, best way to access file.jsonwould be via /data/file.json

如果Resources是根路径,最好的访问file.json方式是通过/data/file.json