我有一个简单的问题,但我无法解决它!我有一个带点的简单json文件..每个点都有坐标(long,lat)和一个id。这个文件我想用JsFiddle插入到javascript中的表中。 (之后,我想使用每一对坐标来创建另一个特征并导出一个带坐标和此功能的json文件,但这不是我现在的问题:P)。我附上了这个json文件..谢谢你有美好的一天! :)
我的 json 文件:
{
"displayFieldName": "",
"fieldAliases": {
"FID": "FID",
"Id": "Id",
"Longtitude": "Longtitude",
"Latitude": "Latitude"
},
"geometryType": "esriGeometryPoint",
"spatialReference": {
"wkid": 4326,
"latestWkid": 4326
},
"fields": [{
"name": "FID",
"type": "esriFieldTypeOID",
"alias": "FID"
}, {
"name": "Id",
"type": "esriFieldTypeInteger",
"alias": "Id"
}, {
"name": "Longtitude",
"type": "esriFieldTypeDouble",
"alias": "Longtitude"
}, {
"name": "Latitude",
"type": "esriFieldTypeDouble",
"alias": "Latitude"
}],
"features": [{
"attributes": {
"FID": 0,
"Id": 1,
"Longtitude": 23.739000000000001,
"Latitude": 37.972000000000001
},
"geometry": {
"x": 23.739000000000001,
"y": 37.972000000000001
}
}, {
"attributes": {
"FID": 1,
"Id": 2,
"Longtitude": 23.760100000000001,
"Latitude": 37.984999999999999
},
"geometry": {
"x": 23.760100000000001,
"y": 37.984999999999999
}
}, {
"attributes": {
"FID": 2,
"Id": 3,
"Longtitude": 23.749199999999998,
"Latitude": 37.975999999999999
},
"geometry": {
"x": 23.749199999999998,
"y": 37.975999999999999
}
}, {
"attributes": {
"FID": 3,
"Id": 4,
"Longtitude": 23.735700000000001,
"Latitude": 37.975999999999999
},
"geometry": {
"x": 23.735700000000001,
"y": 37.975999999999999
}
}]
}
答案 0 :(得分:0)
您必须解析文件。
var fileContent = readTextFile("file:///C:/your/path/to/file.txt");
fielContent = JSON.parse(fileContent);
在此之后,您可以使用您想要的json,例如取每个坐标
fileContent.features.each(function(point) {
console.log(point.geometry.x + " " + point.geometry.y);
});
确保根据需要进行修改。
答案 1 :(得分:0)
你可以试试这个。
加载你的JSON:
function loadJSON(file,callback) {
var xobj = new XMLHttpRequest();
xobj.overrideMimeType('application/json');
xobj.open('GET', file, true);
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);
}
搜索它并得到你想要的任何东西:
//return an array of objects according to key, value, or key and value matching
function getObjects(obj, key, val) {
var objects = [];
for (var i in obj) {
if (!obj.hasOwnProperty(i)) continue;
if (typeof obj[i] == 'object') {
objects = objects.concat(getObjects(obj[i], key, val));
} else
//if key matches and value matches or if key matches and value is not passed (eliminating the case where key matches but passed value does not)
if (i == key && obj[i] == val || i == key && val == '') { //
objects.push(obj);
} else if (obj[i] == val && key == ''){
//only add if the object is not already in the array
if (objects.lastIndexOf(obj) == -1){
objects.push(obj);
}
}
}
return objects;
}
//return an array of values that match on a certain key
function getValues(obj, key) {
var objects = [];
for (var i in obj) {
if (!obj.hasOwnProperty(i)) continue;
if (typeof obj[i] == 'object') {
objects = objects.concat(getValues(obj[i], key));
} else if (i == key) {
objects.push(obj[i]);
}
}
return objects;
}
//return an array of keys that match on a certain value
function getKeys(obj, val) {
var objects = [];
for (var i in obj) {
if (!obj.hasOwnProperty(i)) continue;
if (typeof obj[i] == 'object') {
objects = objects.concat(getKeys(obj[i], val));
} else if (obj[i] == val) {
objects.push(i);
}
}
return objects;
}
示例:
loadJSON('index.json', function(text){
var data = JSON.parse(text);
console.log(data);
var files = getObjects(data, 'type', 'file');
var fileNames = getValues(files, 'name');
var directories = getObjects(data, 'type','directory');
var directoryNames = getValues(directories, 'name');
});
答案 2 :(得分:0)
请试试这个
[
{
"Dzień": 1,
"Dyżur nocny": "Błotna 3",
"Dyżur świąteczny": "Wojska Polskiego "
},
{
"Dzień": 2,
"Dyżur nocny": "Zachlapana 9",
"Dyżur świąteczny": ""
}
]
(async function() {
const url = "s21.json";
const data = await (await fetch(url)).json();
console.log(data[1]);
})();