我有一个外部JSON文件,并且在Web应用程序中供内部使用时,我必须使用外部服务器的主JSON中的所有选定对象创建自定义和separte JSON字符串。
输出字符串应如下所示,但有更多对象:
{"geometry": {"type": "Point", "coordinates": [78.454, 22.643]}, "type": "Feature", "properties": {}}
创建字符串的jquery代码如下所示:
$.getJSON(getshowObjectReportExtern, function(data) {
$.each(data, function(key, value) {
// loop html table
}
var llrealTimeObj = {
geometry: {
type: 'FeatureCollection',
features: [
{ // loop from here
type: 'Feature',
geometry: {
type: 'Point',
coordinates: [ value.latitude_mdeg, value.longitude_mdeg]
},
properties: {
Number: value.objectno,
Plate: value.objectname,
Location: value.postext_short
}
}, // till there
]
}
}
var llrealTimeJSONString = JSON.stringify(llrealTimeObj);
console.log(llrealTimeJSONString);
});
为每个单个对象创建字符串工作正常,但目前问题是,我不知道如何创建包含外部JSON中所有选定对象的单个字符串。
对我有什么建议吗?提前谢谢!
编辑: 最终输出应该看起来像这样,但是使用我的主JSON文件中的对象。
{
"type": "FeatureCollection",
"features": [
{
"type": "Feature",
"geometry": {
"type": "Point",
"coordinates": [
-22.054688930511475,
29.53522956294847
]
},
"properties": {}
},
{
"type": "Feature",
"geometry": {
"type": "Point",
"coordinates": [
-22.933595180511475,
19.642587534013032
]
},
"properties": {}
},
{
"type": "Feature",
"geometry": {
"type": "Point",
"coordinates": [
-25.394532680511475,
28.76765910569123
]
},
"properties": {}
}
]
}
链接到在线编辑器:https://google-developers.appspot.com/maps/documentation/utils/geojson/
答案 0 :(得分:0)
假设数据是从外部源带来的JSON数组。
//jsonData is the external data I get from server
var jsonData =
{
"type": "FeatureCollection",
"features": [
{
"type": "Feature",
"geometry": {
"type": "Point",
"coordinates": [
-54.9904990196228,
-7.88514728342433
]
},
"properties": {}
},
{
"type": "Feature",
"geometry": {
"type": "Point",
"coordinates": [
-54.9026083946228,
-13.752724664396975
]
},
"properties": {}
},
{
"type": "Feature",
"geometry": {
"type": "Point",
"coordinates": [
-49.7170615196228,
-16.214674588248542
]
},
"properties": {}
}
]
};
//My custome object
var llrealTimeObj = {
geometry: {
type: 'FeatureCollection',
features: []
}
};
//Loop through external data
$.each(jsonData.features, function (i, node) {
llrealTimeObj.geometry.features.push({
type: node.type,
geometry: node.geometry //Add any more properties that you like here.
});
});
var llrealTimeJSONString = JSON.stringify(llrealTimeObj);
console.log(llrealTimeJSONString);

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.0.0/jquery.min.js"></script>
&#13;
答案 1 :(得分:0)
我从同事那里得到了一个创建字符串的提示。 请参阅以下解决方案:
$.getJSON(getshowObjectReportExtern, function(data) {
var vehicleListData = '';
var tmpLlrtArr = [];
$.each(data, function(key, value) {
// loop html table
var tmpLlrtObj = {};
tmpLlrtObj.type = 'Feature';
tmpLlrtObj.geometry = {};
tmpLlrtObj.geometry = {};
tmpLlrtObj.geometry.type = 'Point';
tmpLlrtObj.geometry.coordinates = [(value.longitude_mdeg * 0.000001).toFixed(6), (value.latitude_mdeg * 0.000001).toFixed(6)];
tmpLlrtObj.properties = {};
tmpLlrtObj.properties.VNumber = value.objectno;
tmpLlrtObj.properties.Plate = value.objectname;
tmpLlrtObj.properties.Position = value.postext_short;
tmpLlrtArr.push(tmpLlrtObj);
});
var llrealTimeObj = {
"type": "FeatureCollection",
"features": tmpLlrtArr
}
var llrealTimeJSONString = JSON.stringify(llrealTimeObj);
console.log(llrealTimeObj);
$('#vehicleList').html(vehicleListData);
});