Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package demo;
- import org.json.simple.JSONArray;
- import org.json.simple.JSONObject;
- import org.json.simple.parser.ParseException;
- import java.io.File;
- import java.io.FileNotFoundException;
- import java.io.FileReader;
- import java.io.IOException;
- public class Driver {
- public static void main(String[] args) throws org.json.simple.parser.ParseException, FileNotFoundException, IOException, ParseException {
- org.json.simple.parser.JSONParser parser = new org.json.simple.parser.JSONParser();
- /*try {
- File jsonFile = new File("/Users/juniastchambakuiteul/Desktop/DesignPatterns/src/main/resources/json2-data.json");
- Object object = parser.parse(new FileReader(jsonFile));
- JSONObject jsonObject = (JSONObject) object;
- String name = (String) jsonObject.get("name");
- long age = (Long) jsonObject.get("age");
- JSONArray message = (JSONArray) jsonObject.get("messages");
- Iterator<String> iterator = message.iterator();
- while(iterator.hasNext()){
- System.out.println(iterator.next());
- }
- System.out.println("Name: " + name + " Age: " + age);
- } catch (FileNotFoundException e) {
- e.printStackTrace();
- }*/
- File jsonFile = new File("/Users/juniastchambakuiteul/Desktop/DesignPatterns/src/main/resources/json-data.json");
- JSONArray array = (JSONArray) parser.parse(new FileReader(jsonFile));
- for(Object obj : array){
- JSONObject person = (JSONObject) obj;
- String name = (String) person.get("name");
- System.out.println("Name: " + name);
- String city = (String) person.get ("city");
- System.out.println("City: " + city);
- String job = (String) person.get("job");
- System.out.println("Job: " + job);
- JSONArray cars = (JSONArray) person.get("cars");
- for(Object car : cars){
- System.out.println(car);
- }
- }
- // for (Object o : a)
- // {
- // JSONObject person = (JSONObject) o;
- //
- // String name = (String) person.get("name");
- // System.out.println(name);
- //
- // String city = (String) person.get("city");
- // System.out.println(city);
- //
- // String job = (String) person.get("job");
- // System.out.println(job);
- //
- // JSONArray cars = (JSONArray) person.get("cars");
- //
- // for (Object c : cars)
- // {
- // System.out.println(c+"");
- // }
- // }
- // ObjectMapper mapper = new ObjectMapper();
- // InputStream is = Test.class.getResourceAsStream("/json-data.json");
- // try {
- // Test testObj = mapper.readValue(is, Test.class);
- // System.out.println(testObj.getCar() + testObj.getCity() + testObj.getJob() + testObj.getName());
- //
- // is.close();
- // } catch (IOException e) {
- // e.printStackTrace();
- // }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement