【发布时间】:2017-07-26 00:13:24
【问题描述】:
我有一个 csv 文件
input.csv
1,[103.85,1.28992],[103.89,1.294],[103.83,1.216]
2,[103.5,1.292],[103.9,1.4],[103.3,1.21]
3,[103.6,1.291],[103.6,1.39],[103.3,1.29]
从这里我需要把它转换成
{
"type": "LineString",
"coordinates": [[103.85,1.28992],[103.89,1.294],[103.83,1.216]]
"properties": {
"id": "1"
}
},
{
"type": "LineString",
"properties": {
"id": "2"
},
"coordinates": [[103.5,1.292],[103.9,1.4],[103.3,1.21]]
},{
"type": "LineString",
"properties": {
"id": "3"
},
"coordinates": [[103.6,1.291],[103.6,1.39],[103.3,1.29]]
}
我现在正在尝试在 java 中执行此操作。所以我用打开的 csv 读取了 csv 文件
try (CSVReader reader = new CSVReader(new FileReader(fileName))) {
String[] nextLine;
while ((nextLine = reader.readNext()) != null) {
for (String e: nextLine) {
// System.out.format("%s ", e);
System.out.println( e.split(",",1));
}
}
但是我在拆分线路时遇到了问题。如果您查看第一行,那么我想要
1 作为一部分,其余 [103.85,1.28992],[103.89,1.294],[103.83,1.216] 作为另一部分。这样我就可以构建字符串了
String s="{\"type\": \"LineString\", \"coordinates\": "+s[1]+"
\"properties\": { \"id\":"+s[0]+"} }";
感谢任何帮助
【问题讨论】:
-
@Jens 我不读取 json 文件我读取 csv 并写入 JSON 文件
标签: java regex csv text-processing opencsv