2015-11-06 68 views
-5

工作我是一个初学者在使用JSON工作,所以需要一些帮助! 我有附着在一个HTML文件中使用这些代码的外部JSON文件:与外部JSON

{ 
"reports": [{ 
     "id": "1", 
     "name": "week", 
     "type": "bar", 
     "size": 12288 
    }, { 
     "id": "2", 
     "name": "month", 
     "type": "line", 
     "size": 242688 
    }] 
} 

而且附着过多另一个外部JS main.js。例如,我想读取数组中main.js中的json文件中的数据!我该怎么办?我没有任何访问权限来更改json文件。

  • :我必须使用纯粹的js!
+1

你是什么意思, “附”?在客户端阅读它的唯一方法是通过AJAX。 – Amadan

+0

什么意思是“读取数据” - 在Web应用程序中?网站? html文件?什么? – Alex

+0

@Amadan我的意思是联系! –

回答

1

解决方案采用纯JavaScript。

var listreports = new Array(); 
function init() { 
loadJSON(function(response) { 
    // Parse JSON string into object 
    var actual_JSON = JSON.parse(response); 

    for (var prop in actual_JSON){ 
     tepObj = actual_JSON[prop]; // this is loop into reports, if you have reports2 will be inside 
     for (var rep in tepObj){ 
      temprep = tepObj[rep]; 
      console.log(temprep) 
      var report = new Object(); 
      report.id = temprep.id 
      report.name= temprep.name 
      report.type= temprep.type 
      report.size= temprep.size 
      listreports.push(report); 
      console.log(report) 
     } 

    } 
    console.log(listreports); 
}); 
} 


function loadJSON(callback) { 

    var xobj = new XMLHttpRequest(); 
     xobj.overrideMimeType("application/json"); 
    xobj.open('GET', 'test.json', true); // Replace 'test' with the path to your file 
    xobj.onreadystatechange = function() { 
      if (xobj.readyState == 4 && xobj.status == "200") { 
      // Required use of an anonymous callback as .open will NOT return a value but simply returns undefined in asynchronous mode 
      callback(xobj.responseText); 
      } 
    }; 
    xobj.send(null); 
} 


init(); //this is only to call the function. 

使用jQuery

首先解决试试这个

var reports = new array() 
$.getJSON("folder/file.json", function(data) { 
}).success(function(data){ 
    for (var prop in data){ 
    temprep= data[prop] 
    report.id = temprep.id 
    report.name= temprep.name 
    report.type= temprep.type 
    report.size= temprep.size 
    reports.push(report) 
    }); 

那么你将有对象的数组与JSON

+0

哦!对不起,我忘了!我必须使用纯js! –

+0

我用pureJS解决方案进行编辑,请检查 – TiGreX

+0

这意味着我有listreports [0] .id吗?或者是什么 ? tnx –

0

如果我理解正确的话你有一个文件在JSON和你的主要的Javascript另一个文件,如果这是正确的,那么你必须在该main.js文件上面的JSON文件HTML。

然后,你将能够从main.js到达json,因为当main.js ks加载的json文件已经加载时,如果main.js在html中的json文件之前,那么你赢了'吨有什么办法让从main.js

编辑JSON:你的HTML头应该是这样的:

<script src="json.js"></script> 
<script src="main.js"></script> 

因此,JSON文件将是已知的main.js文件。其他人是正确的,你应该有你把它里面的JSON一个变量,所以你将能够获得从main.js文件。

你可以通过这个代码做这个(假设你要拨打的变量“j”

var j = your_json; 
+0

正确!我做到了!我不知道这样做的语法! –

0

如果要加载,你已经证明,然后不被存储在JSON对象格式的JSON文件。给一个变量

更改您的JSON文件:

var reports = { 
"reports": [{ 
     "id": "1", 
     "name": "week", 
     "type": "bar", 
     "size": 12288 
    }, { 
     "id": "2", 
     "name": "month", 
     "type": "line", 
     "size": 242688 
    }] 
}; 
+0

我说我不能改变json文件! –

1

可以存储JSON文件中与Ajax请求main.js一个变量:

var xhReq = new XMLHttpRequest(); 
xhReq.open("GET", ***here the url to your JSON file***, false); 
xhReq.send(null); 

然后,您可以将该JSON字符串解析为一个JavaScript对象,以便像数组那样操作。

var jsonObject = JSON.parse(xhReq.responseText);