Javascript-如何读取json文件中的列并将其保存在Javascript数组中
Javascript - How can I read column in json file and save it in javascript array
我制作了一个json文件,其内容如下:
[{"pdis": "pdistance", "time": "time", "lon": "longitude", "tdis": "totaldistance", "secsp": "sectionspray", "lat": "latitude"}, {"pdis": "0.000555", "time": "10:01:43", "lon": "126.952741667", "tdis": "0.000555", "secsp": "3343.0", "lat": "37.4805016667"}, {"pdis": "0.027396", "time": "10:01:57", "lon": "126.952753333", "tdis": "0.027951", "secsp": "3320.0", "lat": "37.4807483333"},~~~,{"~~~~~"}]
我想读取它们并将其保存在javascript数组中为:
pdis time lon tds secsp lat
0.000555 10:01:43 126.95274 0.000555 3343.0 37.4805
0.027396 10:01:57 126.95275 0.02795 3320.0 37.4807
我该怎么做
请给我建议或推荐一些网站或页面。
完全匹配的代码样本可以在这里找到
https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/JSON/parse
JSON.parse('{"p": 5}', function(k, v) {
if (k === '') { return v; } // if topmost value, return it,
return v * 2; // else return v * 2.
}); // { p: 10 }
试试这个。
var jsonString = '[{ "pdis": "pdistance", "time": "time", "lon": "longitude", "tdis": "totaldistance", "secsp": "sectionspray", "lat": "latitude" }, { "pdis": "0.000555", "time": "10:01:43", "lon": "126.952741667", "tdis": "0.000555", "secsp": "3343.0", "lat": "37.4805016667" }, { "pdis": "0.027396", "time": "10:01:57", "lon": "126.952753333", "tdis": "0.027951", "secsp": "3320.0", "lat": "37.4807483333" }]';
var result = JSON.parse(jsonString).map(function(obj, index) {
var arr = Object.keys(obj);
if (index > 0) {
arr = arr.map(function(key) {
return obj[key];
});
}
return arr;
});
document.body.innerHTML = JSON.stringify(result);
检查这个jsfiddle,它下载了带有内容的文件
pdis time lon tdis secsp lat
pdistance time longitude totaldistance sectionspray latitude
0.000555 10:01:43 126.952741667 0.000555 3343.0 37.4805016667
0.027396 10:01:57 126.952753333 0.027951 3320.0 37.4807483333
它通常采用json数组并转换为表格式然后将其下载为文件。
解析JSON字符串后,可以使用以下方法构建包含所需部分的数组。
var array = [{ "pdis": "pdistance", "time": "time", "lon": "longitude", "tdis": "totaldistance", "secsp": "sectionspray", "lat": "latitude" }, { "pdis": "0.000555", "time": "10:01:43", "lon": "126.952741667", "tdis": "0.000555", "secsp": "3343.0", "lat": "37.4805016667" }, { "pdis": "0.027396", "time": "10:01:57", "lon": "126.952753333", "tdis": "0.027951", "secsp": "3320.0", "lat": "37.4807483333" }],
keys = Object.keys(array[0]),
result = array.map(function (a) {
return keys.map(function (k) {
return a[k];
});
});
document.write('<pre>' + JSON.stringify(result, 0, 4) + '</pre>');
相关文章:
- 保存数组javascript
- 保存表中的行's在数组中的tbody,稍后使用它们
- 保存id数组's而不是对象
- JavaScript-保存、存储和更新数组
- Javascript-将类型化数组保存为blob,并作为二进制数据读回
- 使用javascript/jquery从现有数组中创建一个新数组,该数组保存项目存在的次数
- AJAX将JSON数组保存到多个JS变量中
- 我的函数不会返回要保存在数组中的对象
- Javascript数组只保存最近的对象
- 将对象属性保存到数组时出现奇怪的重复
- 将地理编码器的位置保存到数组中
- 如何在排序前保存数组的副本
- 如何使用保存数组名称的变量将对象文本添加到数组
- 使用猫鼬保存数组数据的更干净的代码
- Javascript:保存数组中重复项的索引
- 在重新加载时保存数组值并将其取回
- 如何在parse.com+AngularJS中使用for循环保存数组中的每个对象
- 使用postgresql保存数组进行Sequelize
- 对象,用于保存数组JQuery
- 如何在文件上快速保存数组