Advertisement
Guest User

readFileAsJson

a guest
Dec 13th, 2018
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.09 KB | None | 0 0
  1. package demo;
  2.  
  3.  
  4. import org.json.simple.JSONArray;
  5. import org.json.simple.JSONObject;
  6. import org.json.simple.parser.ParseException;
  7.  
  8. import java.io.File;
  9. import java.io.FileNotFoundException;
  10. import java.io.FileReader;
  11. import java.io.IOException;
  12.  
  13. public class Driver {
  14.  
  15. public static void main(String[] args) throws org.json.simple.parser.ParseException, FileNotFoundException, IOException, ParseException {
  16.  
  17. org.json.simple.parser.JSONParser parser = new org.json.simple.parser.JSONParser();
  18.  
  19. /*try {
  20. File jsonFile = new File("/Users/juniastchambakuiteul/Desktop/DesignPatterns/src/main/resources/json2-data.json");
  21. Object object = parser.parse(new FileReader(jsonFile));
  22.  
  23. JSONObject jsonObject = (JSONObject) object;
  24.  
  25. String name = (String) jsonObject.get("name");
  26. long age = (Long) jsonObject.get("age");
  27.  
  28. JSONArray message = (JSONArray) jsonObject.get("messages");
  29.  
  30. Iterator<String> iterator = message.iterator();
  31.  
  32. while(iterator.hasNext()){
  33. System.out.println(iterator.next());
  34. }
  35.  
  36.  
  37.  
  38. System.out.println("Name: " + name + " Age: " + age);
  39.  
  40. } catch (FileNotFoundException e) {
  41. e.printStackTrace();
  42. }*/
  43.  
  44. File jsonFile = new File("/Users/juniastchambakuiteul/Desktop/DesignPatterns/src/main/resources/json-data.json");
  45. JSONArray array = (JSONArray) parser.parse(new FileReader(jsonFile));
  46.  
  47. for(Object obj : array){
  48. JSONObject person = (JSONObject) obj;
  49. String name = (String) person.get("name");
  50. System.out.println("Name: " + name);
  51. String city = (String) person.get ("city");
  52. System.out.println("City: " + city);
  53. String job = (String) person.get("job");
  54. System.out.println("Job: " + job);
  55. JSONArray cars = (JSONArray) person.get("cars");
  56. for(Object car : cars){
  57. System.out.println(car);
  58. }
  59. }
  60. // for (Object o : a)
  61. // {
  62. // JSONObject person = (JSONObject) o;
  63. //
  64. // String name = (String) person.get("name");
  65. // System.out.println(name);
  66. //
  67. // String city = (String) person.get("city");
  68. // System.out.println(city);
  69. //
  70. // String job = (String) person.get("job");
  71. // System.out.println(job);
  72. //
  73. // JSONArray cars = (JSONArray) person.get("cars");
  74. //
  75. // for (Object c : cars)
  76. // {
  77. // System.out.println(c+"");
  78. // }
  79. // }
  80.  
  81. // ObjectMapper mapper = new ObjectMapper();
  82. // InputStream is = Test.class.getResourceAsStream("/json-data.json");
  83. // try {
  84. // Test testObj = mapper.readValue(is, Test.class);
  85. // System.out.println(testObj.getCar() + testObj.getCity() + testObj.getJob() + testObj.getName());
  86. //
  87. // is.close();
  88. // } catch (IOException e) {
  89. // e.printStackTrace();
  90. // }
  91.  
  92.  
  93. }
  94. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement