我下载了一个json文件,并希望将其导入到mongodb中,但是在将其导入数据库之前,我想在node.js中编写代码以重组json对象,并向每个对象添加一些其他信息。 每个对象的结构如下:
import java.util.Arrays;
import java.util.Scanner;
public class String2intArray {
public static void main(String[] args) {
System.out.println(Arrays.toString(str2ia("6,8 ,9 , 10")));
System.out.println(Arrays.toString(str2ia("1002")));
System.out.println(Arrays.toString(str2ia("")));
System.out.println(Arrays.toString(str2ia(", 2")));
System.out.println(Arrays.toString(str2ia("Ett,Två")));
System.out.println(Arrays.toString((str2ia("1,,2"))));
System.out.println(Arrays.toString(str2ia("1 , 2 ,")));
}
public static int[] str2ia(String s) {
int arraylengt = 0;
int arrayindex = 0;
Scanner scan = new Scanner(s).useDelimiter(" *, *");
if(s.length() == 0)
{
int[] emptyArray = {};
System.out.print("empty array ");
return emptyArray;
}
while (scan.hasNext()) {
if (scan.hasNextInt()) {
int nextInt = scan.nextInt();
arraylengt ++;
} else {
// discard next token
scan.next();
}
}
int[] intArray = new int[arraylengt];
Scanner scan2 = new Scanner(s).useDelimiter(" *, *");
try {
while (scan2.hasNext()) {
if (scan2.hasNextInt()) {
intArray[arrayindex] = scan2.nextInt();
arrayindex++;
} else {
// discard next token
throw new IllegalArgumentException("Bad input");
}
}}
catch (IllegalArgumentException e) {
System.out.print("Bad input");
int[] emptyArray = {};
return emptyArray;
}
{
return intArray;
}
}
}
我为json属性创建模型或架构的目的是,如果稍后更改某些NAMES,我可以轻松地直接在模型中应用更改,而无需在我的javascript代码中。 如果有人给我提示会很棒。 谢谢
我的代码的一部分:
{
"fid": "52955",
"geometry": {
"type": "LineString",
"coordinates": [
[
-76.9812226935329,
38.9032437788008
],
[
-76.9811595039813,
38.9033432081856
]
]
},
"signs": [
{
"MANUAL_SEGID": null,
"OBJECTID": "125463",
"GLOBALID": "{0F7DB59B-948D-42DB-90EF-3F17D391B8BB}",
"SIGNARROWDIRECTION": "2",
"MUTCD": "R-DC-Loading_Zone_With_ParkMobile_Zone",
"intervalInfo": [
{
"RESTRICTIONSTATUS": "3",
"OBJECTID": "41460"
}
]
},
{
"MANUAL_SEGID": null,
"OBJECTID": "125465",
"GLOBALID": "{079902E8-D9B0-462B-9323-1550B4FE7197}",
"SIGNARROWDIRECTION": "2",
"MUTCD": "R-DC-2HR",
"intervalInfo": [
{
"RESTRICTIONSTATUS": "1",
"OBJECTID": "41462"
}
]
}
]
}
嘿,我可以用以下类编写一个objectModel.js,但是我仍然很困惑如何为数组使用构造函数。在我的示例中this.signs?因为val.signs是一个符号数组。
const filesArray = [ "permitZone.geojson"]; //Array of files in manual folder
const dataInfo = {
description: 'Zone',
data: [
{
url: 'https://opendata.arcgis.com/datasets/fcfbf29074e549d8aff9b9c708179291_1.geojson',
type: 'permitZone.geojson'
}
]
};
function dataReader() {
var allResult = [];
var arrayFilePromises = filesArray.map(function (fileName, index) {
return downloadFile(dataInfo.data[index].url, dataDownloadPath, fileName)
.then((fileRes) => {
fileRes.features.forEach(function (val) {
// Here i need to read each object and apply some changes
});
})
})
return Promise.all([Promise.all(arrayFilePromises)])
.then(() => {
return allResult
})
}
答案 0 :(得分:1)
我终于解决了,通过signs数组的迭代应该像这样:
class Sign {
constructor(signData) {
this.objectId = signData.OBJECTID;
this.global = signData.GLOBALID;
this.manualSegId = signData.MANUAL_SEGID;
this.signarrowDirection = signData.SIGNARROWDIRECTION;
}
}
class MyCity {
constructor(val) {
this.fid = val.fid;
this.lineId = val.lineId;
this.segId = val.segId;
this.geometry = val.geometry;
this.signs = val.signs.map(signData => new Sign(signData))
}
}
module.exports = {
MyCity: MyCity
}