Java Examples to Create JSON Arrays and JSON Objects

In the spirit of creating simple, copyable Java based JSON examples, here are some JSON Array and JSON Object processing samples. To keep it super easy to read, there is no database access or any other logic to clutter up the simple use cases presented here.

These examples are using the json-simple api that is common in many Java implementations.

Get the json-simple jar:  download json-simple-1.1.1.jar

Get the source for json-simple on github: download json-simple source

Check out the Google archive for more info on json-simple here: Read about it on Google

If you would like the eclipse project that contains the code below:  download java-json-simple eclipse project

Check out json.org for a long list of JSON parsers across many different languages.

 

This example creates a simple, single record style JSON Object.

 

This example creates an outer JSON object for “root” containing a JSON Array which contains 2 record style JSON Objects.

 

This example creates an outer JSON object for “root” containing a JSON Array which contains 2 record style JSON Objects.  Each of the record style JSON Objects contains a JSON Array.

About the Author Scott Salisbury

Scott is the creator of Motivated Code Pro and the Managing Partner of Pinch Hitter Solutions, Inc. Motivated Code Pro is devoted to helping developers build better software careers. Pinch Hitter Solutions (phs4j.com) is a consultancy focused on mobile app development and enterprise web work. Scott works primarily in Java and JavaScript and focuses on Spring and Ext JS.

Leave a Comment:

1 comment
George Rietz says June 28, 2018

I’ve pored over many pages, libraries, and tutorials on (de)serialization. This post beats them all, striking the best balance between simple accessibility and useful completeness.

Reply
Add Your Reply