Java JSON Object and Array

May 22, 2018 214 0 1

Set the Environment

Before start coding we have to set a proper environment for the compiler, in order to recognize the JSON’s classes
Please download the files on the link below and add them to the project.
Java Json Jar
Java Json Simple

JSON Object

We will use these files for json operations. Now will create a json file using java.

JSONObject obj=new JSONObject();
System.out.print("JSON OBJECT : "+ obj+"\n");

Screen Output :
JSON OBJECT : {"name":"RIDVAN","salary":6000,"age":24}

JSON Array

Now create a json array

JSONArray arry = new JSONArray();
System.out.print("JSON ARRAY : "+arry+"\n");

Screen Output :
JSON ARRAY : ["22","22","600"]

JSON Read String

let’s read all the data from the String

String s="{\"name\":\"RIDVAN\",\"salary\":6000,\"age\":24}";
JSONObject k=new JSONObject(s);
String name = k.get("name").toString();
String salary = k.get("salary").toString();
String age = k.get("age").toString();

Screen Output :
JSON String Result : RIDVAN 6000 24

we will do the json writing process on the next article : Java JSON Write File

Github Link

Categories: Java
share TWEET PIN IT SHARE share share
Leave a reply

Leave a Reply

Your email address will not be published. Required fields are marked *