private static void readJsonFile() {
   try (JsonReader jsonReader =
       Json.createReader(
           new FileReader(
               Paths.get(System.getProperty("user.dir"), "target/myData.json").toString()))) {
     JsonStructure jsonStructure = jsonReader.read();
     JsonValue.ValueType valueType = jsonStructure.getValueType();
     if (valueType == JsonValue.ValueType.OBJECT) {
       JsonObject jsonObject = (JsonObject) jsonStructure;
       JsonValue firstName = jsonObject.get("firstName");
       LOGGER.info("firstName=" + firstName);
       JsonValue emailAddresses = jsonObject.get("phoneNumbers");
       if (emailAddresses.getValueType() == JsonValue.ValueType.ARRAY) {
         JsonArray jsonArray = (JsonArray) emailAddresses;
         for (int i = 0; i < jsonArray.size(); i++) {
           JsonValue jsonValue = jsonArray.get(i);
           LOGGER.info("emailAddress(" + i + "): " + jsonValue);
         }
       }
     } else {
       LOGGER.warning("First object is not of type " + JsonValue.ValueType.OBJECT);
     }
   } catch (FileNotFoundException e) {
     LOGGER.severe("Failed to open file: " + e.getMessage());
   }
 }
Esempio n. 2
0
  public static void main(String[] args) throws IOException {
    String workingDir = System.getProperty("user.dir");
    Path filePath = FileSystems.getDefault().getPath(workingDir + "/resources/Day12");

    JsonReader reader = Json.createReader(Files.newInputStream(filePath));
    JsonStructure jsonst = reader.read();

    System.out.printf("Value is %d\n", navigateTree(jsonst, null));
    System.out.printf("Value is %d\n", navigateTree(jsonst, "red"));
  }
 private static void createJsonFile() {
   JsonObject model =
       Json.createObjectBuilder()
           .add("firstName", "Martin")
           .add(
               "phoneNumbers",
               Json.createArrayBuilder()
                   .add(Json.createObjectBuilder().add("mobile", "1234 56789"))
                   .add(Json.createObjectBuilder().add("home", "2345 67890")))
           .build();
   try (JsonWriter jsonWriter =
       Json.createWriter(
           new FileWriter(
               Paths.get(System.getProperty("user.dir"), "target/myData.json").toString()))) {
     jsonWriter.write(model);
   } catch (IOException e) {
     LOGGER.severe("Failed to create file: " + e.getMessage());
   }
 }