繁体   English   中英

如何将json文件插入javascript

[英]how to insert json file into javascript

我有一个简单的问题,但我无法解决它!!! 我有一个带点的简单 json 文件。每个点都有坐标(长、纬度)和一个 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
        }
    }]
}

你必须解析你的文件。

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

确保根据您的需要进行修改。

你可以试试这个。

要加载您的 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');

});

请试试这个

  1. .json 文件
 [
     {
       "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": ""
     }
  ]
  1. .js 文件
 (async function() {
      const url = "s21.json";
      const data = await (await fetch(url)).json();
      console.log(data[1]);
   })();

暂无
暂无

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

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