So this line is okay to get the reader.
JsonReader reader = Json.createReader(new FileReader("Elements.JSON"));
Your problem is that JsonStructure is an interface that doesn't have the method getJsonArray, but JsonObject does.
Therefore, you need to be doing
JsonObject jsonst = reader.readObject();
And then you can use the methods of JsonObject like so
JsonArray elements = jsonst.getJsonArray("Elements");
for (int i = 0; i < elements.size(); i++) {
JsonObject element = elements.getJsonObject(i);
String name = element.getString("name");
}
Finally, when you are done with the reader, you need to close it.
reader.close();
Answer from OneCricketeer on Stack Overflowjava 8 - javax.json - How to get a specific value from pre-made JSON - Stack Overflow
Isn't JSON support included in java 8?
It's included in Java EE 7 (not SE 7/8)
More on reddit.comJava 8 - working with JsonObject (javax.json) - how to determine if a node has child nodes? - Stack Overflow
java - How can I import javax.json in eclipse - Stack Overflow
Videos
So this line is okay to get the reader.
JsonReader reader = Json.createReader(new FileReader("Elements.JSON"));
Your problem is that JsonStructure is an interface that doesn't have the method getJsonArray, but JsonObject does.
Therefore, you need to be doing
JsonObject jsonst = reader.readObject();
And then you can use the methods of JsonObject like so
JsonArray elements = jsonst.getJsonArray("Elements");
for (int i = 0; i < elements.size(); i++) {
JsonObject element = elements.getJsonObject(i);
String name = element.getString("name");
}
Finally, when you are done with the reader, you need to close it.
reader.close();
try:
jsonst.getJsonArray("Elements")[0].getJsonString("name");
and also modify the line which says read instead of readObject:
JsonStructure jsonst = reader.readObject();
I've installed java 8, but when I do either or these: import javax.json.*; import javax.json.JsonObject;
I get "error: package javax.json does not exist import javax.json.JsonObject;"
I thought json is now included in java. What am I missing?
If using Maven, add this dependency to your pom.xml
<dependency>
<groupId>javax.json</groupId>
<artifactId>javax.json-api</artifactId>
<version>1.0</version>
</dependency>
For Gradle, add this to your build.gradle
compile 'javax.json:javax.json-api:1.0'
Going to assume that you are not using Java 7 and thus need to add the JAR file directly to your project path:
Here's the link to the JAR
And where it came from: http://jsonp.java.net/
Download and add to your project build path.
include the following dependency in your "pom.xml" file.
<dependency>
<groupId>com.googlecode.json-simple</groupId>
<artifactId>json-simple</artifactId>
<version>1.1.1</version>
</dependency>
The simple answer is, you don't have those classes in scope.
This package is not one of the standard java libraries included in your java environment, all you need to do is to find the required jar and ensure it is in scope.