Untitled

mail@pastecode.io avatar
unknown
plain_text
3 years ago
1.8 kB
2
Indexable
Never
import java.io.*;
import org.json.simple.*;
import org.json.simple.parser.*;

public class ControlDataDictionary {

	public static void main(String[] args) {
		JSONParser parser = new JSONParser(); //creating an object of type JSONparser
	      try {
	         Object obj = parser.parse(new FileReader("C:\\Users\\LENOVO\\Desktop\\DataDictionary.json"));
	         JSONObject jsonObject = (JSONObject)obj; //typecasting from object class to json object
	         int i=1;
	            
				for(Object s:  (JSONArray)jsonObject.get("person")) 
				{			 
				// Split up to print individual values 
					 System.out.println("Details of Person: "+i);
					 JSONObject eachPerson = (JSONObject) s; //typecasting from object class to json object
					 Person person = new Person();  //creating a person object
					 person.setFirstname(eachPerson.get("FirstName").toString()); //setting the firstname of person object as method takes the string we are converting it to string and passing as a parameter
					 person.setLastname(eachPerson.get("LasName").toString()); //setting the LastName of person object
					 person.setAge(Integer.parseInt(eachPerson.get("Age").toString())); //setting the Age of person object,as the method takes the integer value we are using parseInt
					 person.setContactNumber(eachPerson.get("ContactNumber").toString()); //setting the ContactNumber of person object
					 person.setGender(eachPerson.get("Gender").toString()); //setting the Gender of person object
					 System.out.println(person.toString()); //displaying the output using toString method of Person class
					 i++;
					}
	 
			} catch (FileNotFoundException e) {
				e.printStackTrace();
			} catch (IOException e) {
				e.printStackTrace();
			} catch (ParseException e) {
				e.printStackTrace();
			}
	}
}