public static void dumpJSON(JsonValue tree, String key, String depthPrefix) {
   if (key != null) logger.info(depthPrefix + "Key " + key + ": ");
   switch (tree.getValueType()) {
     case OBJECT:
       logger.info(depthPrefix + "OBJECT");
       JsonObject object = (JsonObject) tree;
       for (String name : object.keySet()) dumpJSON(object.get(name), name, depthPrefix + "  ");
       break;
     case ARRAY:
       logger.info(depthPrefix + "ARRAY");
       JsonArray array = (JsonArray) tree;
       for (JsonValue val : array) dumpJSON(val, null, depthPrefix + "  ");
       break;
     case STRING:
       JsonString st = (JsonString) tree;
       logger.info(depthPrefix + "STRING " + st.getString());
       break;
     case NUMBER:
       JsonNumber num = (JsonNumber) tree;
       logger.info(depthPrefix + "NUMBER " + num.toString());
       break;
     case TRUE:
     case FALSE:
     case NULL:
       logger.info(depthPrefix + tree.getValueType().toString());
       break;
   }
 }
 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());
   }
 }
  /**
   * http://docs.oracle.com/middleware/1213/wls/WLPRG/java-api-for-json-proc.htm#WLPRG1061
   *
   * @param tree
   * @param key
   */
  public static Object navigateTree(JsonValue tree, String key) {

    Object ret = null;

    if (key != null) System.out.print("Key " + key + ": ");

    switch (tree.getValueType()) {
      case OBJECT:
        System.out.println("OBJECT");
        JsonObject object = (JsonObject) tree;

        for (String name : object.keySet()) navigateTree(object.get(name), name);
        break;
      case ARRAY:
        System.out.println("ARRAY");
        JsonArray array = (JsonArray) tree;
        for (JsonValue val : array) navigateTree(val, null);
        break;
      case STRING:
        JsonString st = (JsonString) tree;
        System.out.println("STRING " + st.getString());
        break;
      case NUMBER:
        JsonNumber num = (JsonNumber) tree;
        System.out.println("NUMBER " + num.toString());
        break;
      case TRUE:
        System.out.println("TRUE");
      case FALSE:
        System.out.println("FALSE");
      case NULL:
        System.out.println(tree.getValueType().toString());
        break;
    }

    return ret;
  }
Beispiel #4
0
 public static int navigateTree(JsonValue tree, String skip) {
   int ret = 0;
   switch (tree.getValueType()) {
     case OBJECT:
       // System.out.println("OBJECT");
       JsonArrayBuilder dirtyResult = Json.createArrayBuilder();
       JsonObject object = (JsonObject) tree;
       for (String name : object.keySet()) {
         if (object.get(name).getValueType() == JsonValue.ValueType.STRING
             && object.getString(name).equals(skip)) {
           return 0;
         }
         dirtyResult.add(object.get(name));
       }
       ret = ret + navigateTree(dirtyResult.build(), skip);
       break;
     case ARRAY:
       // System.out.println("ARRAY");
       JsonArray array = (JsonArray) tree;
       for (JsonValue val : array) ret = ret + navigateTree(val, skip);
       break;
     case STRING:
       JsonString st = (JsonString) tree;
       // System.out.println("STRING " + st.getString());
       break;
     case NUMBER:
       JsonNumber num = (JsonNumber) tree;
       return num.intValue();
     case TRUE:
     case FALSE:
     case NULL:
       // System.out.println(tree.getValueType().toString());
       break;
   }
   return ret;
 }