我知道这个问题已经在这里提到了:
Start reading the file after a specific word
但是,在我的情况下,我想从特定单词(包括该单词)中读取.geojson文件并将其保存在String中。
{"type": "FeatureCollection","features": [{"type": "Feature","properties": {},"geometry": {"type": "LineString","coordinates": [[4.354282,52.032195],[4.354087,52.032462],[4.353783,52.032962],[4.353579,52.033437],[4.353333,52.034151],[4.352991,52.03545],[4.352517,52.037002],[4.352442,52.037352],[4.352368,52.0378],[4.352336,52.038238],[4.352331,52.039962],[4.352346,52.040706]
]
}
}
]
}
我的字符串应该以 {" type":" Linestring" 开头,然后是文件的其余部分。它必须适用于任何Linestring geojson。
到目前为止我的代码:
BufferedReader br = new BufferedReader(new FileReader(
"C:\\Users\\****\\Desktop\\test2.geojson"));
String line;
while ((line = br.readLine()) != null) {
if (line.contains("LineString")) {
break; // breaks the while loop
}
}
while ((line = br.readLine()) != null) {
System.out.println(line);
}
br.close();
有人能把我推向正确的方向吗?
干杯!
答案 0 :(得分:0)
以下代码对我有用!
为了获得所需的JSON,我首先必须遍历树层次结构。这意味着打开“特征”,“几何”,最后打开“坐标”以到达坐标。在最后添加Linestring和括号以再次转换为适当的JSON文件。
// look for features
if (jsoninput.contains("features")) {
jsonArray1 = obj.getJSONArray("features");
System.out.println(jsonArray1+ "\n");
}
// look for geometry
if (jsoninput.contains("geometry")) {
jsonArray2 = obj.getJSONObject("geometry");
System.out.println(jsonArray2+ "\n");
jsonArray3 = jsonArray2.getJSONArray("coordinates");
output = ""+jsonArray3;
System.out.println(output);
}
// no features of geometry, just check "coordinates"
if (!jsoninput.contains("features") & !jsoninput.contains("geometry")) {
jsonArray4 = obj.getJSONArray("coordinates");
output = ""+jsonArray4;
System.out.println(output);
}
// output is just a String of coordinates, add LineString prefix and
// brackets to make it a proper JSON again
String finaloutput = "{\"type\": \"LineString\",\"coordinates\":"
+ output + "}";
System.out.println(finaloutput);