I am new in JSON with java and was trying to explore it using maven with eclipse.
This is my json file.
"name": "Jason Bourne",
"profession": "Super agent",
"bad-guy": false,
"kills": 1000,
"phoneNumbers": [
{
"type": "home",
"number": "123-456-789"
},
{
"type": "work",
"number": "123-555-555"
}
]
},
{
"name": "Jason Voorhees",
"profession": "Maniac killer",
"bad-guy": true,
"kills": 100,
"phoneNumbers": [
{
"type": "office",
"number": "666-666-666"
}
]
},
{
"name": "Jason",
"profession": "Lead of Argonauts",
"bad-guy": false,
"kills": null
}
]
My pom.xml file looks like this
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.assignment</groupId>
<artifactId>assgQuestion</artifactId>
<version>0.0.1-SNAPSHOT</version>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<jsonp.version>1.1.2</jsonp.version>
</properties>
<dependencies>
<!-- https://mvnrepository.com/artifact/javax/javaee-api -->
<dependency>
<groupId>javax</groupId>
<artifactId>javaee-api</artifactId>
<version>8.0</version>
<scope>provided</scope>
</dependency>
<!-- https://mvnrepository.com/artifact/javax.json/javax.json-api -->
<dependency>
<groupId>javax.json</groupId>
<artifactId>javax.json-api</artifactId>
<version>${jsonp.version}</version>
</dependency>
<dependency>
<groupId>org.glassfish</groupId>
<artifactId>javax.json</artifactId>
<version>${jsonp.version}</version>
</dependency>
<!-- https://mvnrepository.com/artifact/com.googlecode.json-simple/json-simple -->
<dependency>
<groupId>com.googlecode.json-simple</groupId>
<artifactId>json-simple</artifactId>
<version>1.1.1</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.7.0</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
<version>1.6.0</version>
<configuration>
<executable>java</executable>
<arguments>
<argument>-classpath</argument>
<!-- automatically creates the classpath using all project dependencies,also adding the project build directory -->
<classpath/>
<argument>packt.javaee.json.Main</argument>
</arguments>
</configuration>
</plugin>
</plugins>
</build>
</project>
I am trying to list all the jsonp events. The following is the code snippet for the same.
public class ReadJSONFile {
public static void main(String args[]) {
new ReadJSONFile().start();
}
private void start() {
while(true) {
printOptions();
Scanner sc = new Scanner(System.in);
switch(sc.nextLine()){
case "1" :
jsonParserScenario();
break;
}
}
}
private void printOptions() {
System.out.println("1. JSON Parser");
}
private void jsonParserScenario() {
//Creating a JSONParser object
JsonParser parser = Json.createParser(ReadJSONFile.class.getResourceAsStream("/person.json"));
while (parser.hasNext()) {
//Get event from parse
JsonParser.Event event = parser.next();
//Display event on screen
System.out.println(event.toString());
}
}
}
AFter running the above file I am getting the following error.
1. JSON Parser
1
Exception in thread "main" java.lang.NullPointerException: Cannot invoke "java.io.InputStream.read()" because "this.in" is null
at org.glassfish.json.UnicodeDetectingInputStream.fillBuf(UnicodeDetectingInputStream.java:89)
at org.glassfish.json.UnicodeDetectingInputStream.detectEncoding(UnicodeDetectingInputStream.java:128)
at org.glassfish.json.UnicodeDetectingInputStream.<init>(UnicodeDetectingInputStream.java:75)
at org.glassfish.json.JsonParserImpl.<init>(JsonParserImpl.java:95)
at org.glassfish.json.JsonProviderImpl.createParser(JsonProviderImpl.java:88)
at javax.json.Json.createParser(Json.java:109)
at assgQuestion.ReadJSONFile.jsonParserScenario(ReadJSONFile.java:52)
at assgQuestion.ReadJSONFile.start(ReadJSONFile.java:29)
at assgQuestion.ReadJSONFile.main(ReadJSONFile.java:19)
Can someone help me figure out whats wrong.