我有以下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”和“features”。我试图使用“功能”中的坐标来制作一棵树。
我的代码因此:
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的任何经验。
感谢。
答案 0 :(得分:0)
您可以从数组中获取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+"");
}
}
希望这会有所帮助..!
答案 1 :(得分:0)
如果您不了解任何构建工具,则可以忽略此答案。 如果你学习任何构建工具maven,gradle等会更好。 https://maven.apache.org/ https://gradle.org/ 以下代码用于maven构建。 将以下依赖项添加到构建文件pom.xml。
的pom.xml
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.9.0</version>
</dependency>
<!-- https://mvnrepository.com/artifact/com.fasterxml.jackson.core/jackson-core -->
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-core</artifactId>
<version>2.9.0</version>
</dependency>
代码:
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
public class Preprocess {
private static ObjectMapper MAPPER = new ObjectMapper().configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
public static void main (String [] args){
File fis = null;
try {
// provide a proper path to wadepools.json file
fis = new File("./src/wadepools.json");
JsonNode jsonNode = MAPPER.readTree(fis);
System.out.println(jsonNode.get("features"));
System.out.println(jsonNode.get("features").get(0).get("properties"));
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (JsonProcessingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}