I can't seem to print JSON Objects with FOR Loop from "Personal Info" object? please see my code and json data below. | Selenium Forum
A
Ashik Das Posted on 27/09/2020

Here is My code below : 

import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Set;

import org.json.simple.JSONArray;
import org.json.simple.JSONObject;
import org.json.simple.parser.JSONParser;
import org.json.simple.parser.ParseException;

public class ReadingJsonFile {

 

@SuppressWarnings("unused")
public static void main(String[] args) throws IOException, ParseException {

String path = "D:\\Selenium Training\\Java Lessons\\JSON_Practice\\ClientData.json";
FileReader reader = new FileReader(path);
JSONParser parser = new JSONParser();
JSONObject json = (JSONObject)parser.parse(reader);
//System.out.println(json.toJSONString());

//now I will try to read the array info from 'FIrst Client info' object

/*
JSONObject personalInfo = (JSONObject)json.get("First Client Info");

JSONArray ClientOne= (JSONArray)personalInfo.get("ClientOne");
System.out.println("First Client Info : " + ClientOne);



//now we need to get client name/dob/location from the object which is inside a parent array
JSONObject ClientInfo = (JSONObject)ClientOne.get(0);
System.out.println("personal data" + ClientInfo);

JSONObject data = (JSONObject)ClientInfo.get("Personal Info");
System.out.println(data);
String name = (String)data.get("Name");
String DOB = (String)data.get("DOB");
String location = (String)data.get("Location");
System.out.println("here is client data: " + name+ "--"+"--"+DOB+"--"+location);

System.out.println("**********************2nd client info*****************");
JSONObject personalInfo2 = (JSONObject)json.get("Second Client Info");
JSONArray Clienttwo= (JSONArray)personalInfo2.get("ClientTwo");
JSONObject clienttwo = (JSONObject)Clienttwo.get(0);
System.out.println("personal data" + clienttwo);
JSONObject data1 = (JSONObject)clienttwo.get("Personal Info");
System.out.println(data1);
String name1 = (String)data1.get("Name");
String DOB1 = (String)data1.get("DOB");
String location1 = (String)data1.get("Location");
System.out.println("here is client data: " + name1+ "--"+"--"+DOB1+"--"+location1);
//JSONArray data = (JSONArray)personalInfo.get("First Client Info");
*/

//we can also use forloop to print the string from the inside object and read client name/dob/location..etc

System.out.println("SAME ANSWER BELOW AS ABOVE BUT THIS TIME WE'RE USING JAVA FORLOOP");

//JSONObject customerinformation = (JSONObject)data.get("Personal Info");
JSONObject newObject= (JSONObject)json.get("First Client Info");
JSONArray clientone=(JSONArray)newObject.get("ClientOne");


JSONObject personalinfo = (JSONObject)clientone.get(0);
for(int i=0; i<personalinfo.size(); i++) {
JSONArray data3 = (JSONArray)personalinfo.get(i);

String name =(String)data3.get("Name");
System.out.println(data3);



}


//String m = (String)data3.get("Name");
//String n = (String)data3.get("DOB");
//String o = (String)data3.get("Location");
//System.out.println("..."+m+"...."+n+"...."+o);





//System.out.println("here is the test name : " + testName);

// now we can print the array if we want

//JSONObject details = (JSONObject)information.get("Personal Info");
//System.out.println(json.size());
//for(int j=0; j<details.size(); j++) {
//JSONObject currentTestData = (JSONObject)json.get(j);
//System.out.println(currentTestData);

}



}

 

..............................................................................

Here is MY JSON data/input info :

{
"First Client Info" :

{
"ClientOne" :[

{
"Personal Info":
{
"Name" : "Mark" ,
"DOB" : "12/2/1995",
"Location" : "Boston"
},

"Carrer Info" :
{
"Job" : "Software Tester " ,
"Position" : " Selenium Automation" ,
"Tools" : "Selenum "
},

"EducationInfo" :
{
"Degree" : "B.S in software",
"Institute" : "Boston University",
"GPS" : "3.5"
}

}
]
},

 


"Second Client Info" :

{
"ClientTwo" :[

{
"Personal Info" :
{
"Name" : "Patel" ,
"DOB" : "11/2/1995",
"Location" : "New York"
},


"Carrer Info" :
{
"Job" : "Electrical Engineer " ,
"Position" : " Circuit Designer" ,
"Tools" : "Multisum "
},


"Education" :
{
"Major" :"EE",
"Institute" : "Umass",
"GPS" : "3.2"
}


}
]
}


}

 

 


A
Ashish Thakur Replied on 30/09/2020

What is your error?

Any exception coming?


A
Ashik Das Replied on 26/10/2020

Hi Ashish,

Thank you for your reply. this problem have been resovled and found the solution. Thanks