我有以下Json文件:
{
"type": "FeatureCollection",
"crs": { "type": "name", "properties": { "name": "urn:ogc:def:crs:OGC:1.3:CRS84" } },
"features": [
{ "type": "Feature", "properties": { "PARK_ID": 393, "FACILITYID": 26249, "coordinates": [ -75.73, 45.34 ] } },
{ "type": "Feature", "properties": { "PARK_ID": 161, "FACILITYID": 3510, "coordinates": [ -75.73, 45.37 ] } },
我能够读取第一行“ type”:“ FeatureCollection”
但我不确定如何阅读“ crs”和“功能”。我正在尝试使用“功能”中的坐标制作一棵树。
到目前为止,我的代码:
import javax.json.*;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.InputStream;
public class Preprocess {
public static void main (String [] args){
InputStream fis = null;
try {
fis = new FileInputStream("wadepools.json");
} catch (FileNotFoundException e) {
e.printStackTrace();
}
JsonReader reader = Json.createReader(fis);
JsonObject wadepool = reader.readObject();
reader.
System.out.println (wadepool.getString("features"));//if i put "type" here i get the output "FeatureCollection"
}
}
最好是将其保存在本地json库中,因为我对Maven或Gradle没有任何经验。
谢谢。
最佳答案
您可以从数组中获取JSON obj。
JSON:
{
"type": "FeatureCollection",
"crs": {
"type": "name",
"properties": {
"name": "urn:ogc:def:crs:OGC:1.3:CRS84"
}
},
"features": [{
"type": "Feature",
"properties": {
"PARK_ID": 393,
"FACILITYID": 26249,
"coordinates": [-75.73, 45.34]
}
},
{
"type": "Feature",
"properties": {
"PARK_ID": 161,
"FACILITYID": 3510,
"coordinates": [-75.73, 45.37]
}
}
]
}
Java:
JSONArray jarray = (JSONArray) parser.parse(new FileReader("wadepools.json"));
for (Object o : jarray )
{
JSONObject value= (JSONObject) o;
String type= (String) value.get("type");
System.out.println(type);
JSONArray features= (JSONArray) value.get("features");
for (Object features: features)
{
System.out.println(features+"");
}
}
希望这可以帮助 ..!