我有一个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中进行操作。因此,我读了带有open 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]+"} }";
任何帮助都将受到赞赏
您可以尝试:
(d+),(.*)
您不需要拆分...如果执行它,您会得到两个组。第1组是数字,第2组是后面的内容说明
尝试此样本:
final String regex = "(\d+),(.*)";
final String string = "1,[103.85,1.28992],[103.89,1.294],[103.83,1.216]n"
+ "2,[103.5,1.292],[103.9,1.4],[103.3,1.21]n"
+ "3,[103.6,1.291],[103.6,1.39],[103.3,1.29]";
final Pattern pattern = Pattern.compile(regex, Pattern.MULTILINE);
final Matcher matcher = pattern.matcher(string);
while (matcher.find()) {
System.out.println(matcher.group(1));
System.out.println(matcher.group(2));
}
使用jsonsimple创建所需的JSON。我认为最简单的JSON LIB。请参阅此使用示例。
您可以自己解析行:
try (BufferedReader reader = new BufferedReader(new FileReader(fileName))) {
String nextLine;
while ((nextLine = reader.readLine()) != null) {
int ix = nextLine.indexOf(',');
if (ix >= 0) {
String head = nextLine.substring(0, ix);
String tail = nextLine.substring(ix+1);
doSomethingWith(head, tail);
}
}
}
问题是要以所需的方式获取数据,以生成input.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]"
或
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]"
因为它是在一条线和线的末端之间的六个逗号,任何CSV解析器都会解释该行有七个列而不是两列。