json 文档拆分工具_将JSON文件对象拆分为多个文件

本文介绍了一种方法,通过JavaScript将包含多个Feature的JSON文件拆分成单独的文件,每个文件包含一个Feature。代码示例展示了如何遍历JSON对象并创建包含单个Feature的新JSON文件,供用户下载。
摘要由CSDN通过智能技术生成

我没有正确测试这个代码。但是应该给你一些关于如何解决上述问题的想法

var json = {

"type": "FeatureCollection",

"features": [

{

"type": "Feature",

"properties": {},

"geometry": {

"type": "Polygon",

"coordinates": [

[

[

-37.880859375,

78.81903553711727

],

[

-42.01171875,

78.31385955743478

],

[

-37.6171875,

78.06198918665974

],

[

-37.880859375,

78.81903553711727

]

]

]

}

},

{

"type": "Feature",

"properties": {},

"geometry": {

"type": "Polygon",

"coordinates": [

[

[

-37.6171875,

78.07107600956168

],

[

-35.48583984375,

78.42019327591201

],

[

-37.880859375,

78.81903553711727

],

[

-37.6171875,

78.07107600956168

]

]

]

}

}

]

}

$(document).ready(function(){

var counter = 1;

json.features.forEach(function(feature){

var data = {type: json.type, features: [feature]}

var newJson = JSON.stringify(data);

var blob = new Blob([newJson], {type: "application/json"});

var url = URL.createObjectURL(blob);

var a = document.createElement('a');

a.download = "feature_" + counter + ".json";

a.href = url;

a.textContent = "Download feature_" + counter + ".json";

counter++;

document.getElementById('feature').appendChild(a);

document.getElementById('feature').appendChild(document.createElement('br'));

});

});;

^{pr2}$;和13;

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值