This file contains a JSON object that also contains a JSON array. To keep things simple in this example, we are only going to print it out.

It is now time to create our Java project. If you keep up with my other tutorials, you’ll know we aren’t going to be using an IDE such as Eclipse. Instead we are going to be using a text editor and a command prompt. Our project structure is going to look like the following:

In the above code you can see that we’ve gotten our JSON string, converted the string into a JSONObject and then extracted the keywords array. Finally the data we obtain is printed out. There are many other great JSON functions to make use of, all of which can be found in the Javadocs for the library.

If you’re not sure how to make an Apache Ant build file, just use the following for our project:

To test everything out, just run ant buildandrun from the command line or terminal. It will build a Java Archive (JAR) file and run it.

Conclusion

This is just one of many ways to parse JSON data using Java. I chose to use this library set because it closely resembles how you would parse JSON in native Android. Another common way which I might explain in a later tutorial is with JSON Simple.

Nic Raboy

Nic Raboy is an advocate of modern web and mobile development technologies. He has experience in Java, JavaScript, Golang and a variety of frameworks such as Angular, NativeScript, and Apache Cordova. Nic writes about his development experiences related to making web and mobile development easier to understand.