Using javax.json library
- Add the following libraries as dependencies or add them to the build path
// https://mvnrepository.com/artifact/javax.json/javax.json-api
implementation("javax.json:javax.json-api:1.1.4") // javax.json-api only contains the API (interfaces) and no implementation
// https://mvnrepository.com/artifact/org.glassfish/javax.json
implementation("org.glassfish:javax.json:1.1.4")// to get an implementation(but if your server comes with a pre-bundled implementation, you can skip this, but need for local development)
Ref
javax.json-api
is deprecated and it is recommended to use jakarta.json-api
// https://mvnrepository.com/artifact/jakarta.json/jakarta.json-api
compile("jakarta.json:jakarta.json-api:2.0.0")
- Proper JSON file, the question had incomplete ] and }.
{
"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
]
}
}
]
}
- source code
File fis = null;
try {
// provide a proper path to wadepools.json file
fis = new File("./src/wadepools.json");
JsonObject jsonObject;
try (JsonReader reader = Json.createReader(new FileInputStream(fis))) {
jsonObject = reader.readObject();
}
// since you know that features is an array of object, we will read it as JsonArray
final JsonArray features = jsonObject.getJsonArray("features");
System.out.println(features);
// From json we know that features is array of objects and it has only 1 object, to read objects from array, we first locate it using index 0, we can iterate array and read objects too, but for simplicity we are targeting only 0th index
final JsonObject object = features.getJsonObject(0);
System.out.println(object.get("properties"));
} catch (IOException e) {
e.printStackTrace();
}
/*output
[{"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]}}]
{"PARK_ID":393,"FACILITYID":26249,"coordinates":[-75.73,45.34]}
*/
Using Jackson library
You can ignore this answer if you don't know any build tools.
It would be better if you learn any build tools either Maven, Gradle, etc.
https://maven.apache.org/
https://gradle.org/
The following code is for maven build.
Add the following dependencies to your build file 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>
Code:
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();
}
}
}