Пример #1
0
  @Test
  public void DecodingJsonText() throws ParseException {
    System.out.println("=======decode=======");

    String s = "[0,{'1':{'2':{'3':{'4':[5,{'6':7}]}}}}]";
    Object obj = JSONValue.parse(s);
    JSONArray array = (JSONArray) obj;
    System.out.println("======the 2nd element of array======");
    System.out.println(array.get(1));
    System.out.println();

    JSONObject obj2 = (JSONObject) array.get(1);
    System.out.println("======field \"1\"==========");
    System.out.println(obj2.get("1"));

    s = "{}";
    obj = JSONValue.parse(s);
    System.out.println(obj);

    s = "{\"key\":\"Value\"}";
    // JSONValue.parseStrict()
    // can be use to be sure that the input is wellformed
    obj = JSONValue.parseStrict(s);
    JSONObject obj3 = (JSONObject) obj;
    System.out.println("====== Object content ======");
    System.out.println(obj3.get("key"));
    System.out.println();
  }
Пример #2
0
 /**
  * Get all city crime reports
  *
  * @return List<CityCrime>
  */
 public List<CityCrime> getAllCityCrime() {
   List<CityCrime> cityCrimeList = new ArrayList<CityCrime>();
   for (int i = 0; i < rawData.size(); i++) {
     try {
       cityCrimeList.add(new CityCrime((JSONObject) rawData.get(i)));
     } catch (NullPointerException e) {
       e.printStackTrace();
     }
   }
   return cityCrimeList;
 }
Пример #3
0
  private Object getObject(String path) {
    this.hitCache = false;
    if (cache.containsKey(path)) {
      this.hitCache = true;
      return cache.get(path);
    }
    String[] params = path.split("\\.");
    JSONObject json = this.root;
    JSONArray jsonArray = null;
    String currentPath = "";
    for (int i = 0; i < params.length - 1; i++) {
      String param = params[i];
      if (cachePolicy.equals(CachePolicy.CACHE_ALL_LEVELS)) {
        if (!currentPath.isEmpty()) {
          currentPath += ".";
        }
        currentPath += param;
      }
      if (param.startsWith("$")) {
        if (jsonArray == null) {
          throw new JSONOperationErrorException("Not illegal syntax at this place: " + param);
        }
        int index = getIndex(param);
        json = (JSONObject) jsonArray.get(index);
        jsonArray = null;
      } else if (param.contains("[")) {
        int find = param.indexOf("[");
        String newParam = param.substring(0, find);
        String s = param.substring(find + 1, param.indexOf("]"));
        if (s.isEmpty()) {
          jsonArray = (JSONArray) json.get(newParam);
          json = null;
        } else {
          int index = Integer.parseInt(s);
          json = (JSONObject) ((JSONArray) json.get(newParam)).get(index);
          jsonArray = null;
        }
      } else {
        Object obj = json.get(param);
        if (obj instanceof JSONObject) {
          json = (JSONObject) obj;
          jsonArray = null;
        } else if (obj instanceof JSONArray) {
          jsonArray = (JSONArray) obj;
          json = null;
        } else if (obj == null) {
          throw new IllegalStateException("json object is null");
        } else {
          throw new IllegalStateException(
              "json object ('" + param + "') has wrong type: " + obj.getClass());
        }
      }
      if (cachePolicy.equals(CachePolicy.CACHE_ALL_LEVELS)) {
        saveToCache(currentPath, json);
      }
    }
    String name = params[params.length - 1];

    Object value;
    if (name.startsWith("$")) {
      if (jsonArray == null) {
        throw new JSONOperationErrorException("Not illegal syntax at this place: " + name);
      }
      int index = getIndex(name);
      value = jsonArray.get(index);
    } else {
      value = json.get(name);
    }

    saveToCache(path, value);

    return value;
  }
Пример #4
0
  private void publishFnol(List<Fnol> fnolList) {
    String amqpURI = null;
    ConnectionFactory factory = null;
    Channel channel = null;
    Connection connection = null;
    String message;
    String BOUND_SERVICES_ENV_VARIABLE_NAME = "VCAP_SERVICES";
    Map<String, String> env = System.getenv();
    String boundServicesJson = env.get(BOUND_SERVICES_ENV_VARIABLE_NAME);
    // String
    // boundServicesJson="{\"staging_env_json\":{},\"running_env_json\":{},\"system_env_json\":{\"VCAP_SERVICES\":{\"cloudamqp\":[{\"name\":\"MQRabbit\",\"label\":\"cloudamqp\",\"tags\":[\"Web-based\",\"UserProvisioning\",\"MessagingandQueuing\",\"amqp\",\"Backup\",\"SingleSign-On\",\"NewProduct\",\"rabbitmq\",\"CertifiedApplications\",\"Android\",\"DeveloperTools\",\"DevelopmentandTestTools\",\"Buyable\",\"Messaging\",\"Importable\",\"ITManagement\"],\"plan\":\"lemur\",\"credentials\":{\"uri\":\"amqp://*****:*****@moose.rmq.cloudamqp.com/kujcbqju\",\"http_api_uri\":\"https://*****:*****@moose.rmq.cloudamqp.com/api/\"}}],\"newrelic\":[{\"name\":\"NewRelic\",\"label\":\"newrelic\",\"tags\":[\"Monitoring\"],\"plan\":\"standard\",\"credentials\":{\"licenseKey\":\"a8a96a124d1b58d708a2c4c07c6cff8938e2e2f4\"}}],\"mongolab\":[{\"name\":\"MongoDB\",\"label\":\"mongolab\",\"tags\":[\"DataStore\",\"document\",\"mongodb\"],\"plan\":\"sandbox\",\"credentials\":{\"uri\":\"mongodb://*****:*****@ds047315.mongolab.com:47315/CloudFoundry_31lvrquo_j44bi0vu\"}}]}},\"application_env_json\":{\"VCAP_APPLICATION\":{\"limits\":{\"mem\":512,\"disk\":1024,\"fds\":16384},\"application_id\":\"87bdc475-83c4-4df9-92d1-40ff9bf82249\",\"application_version\":\"52891578-5906-4846-9231-afe7048f29bf\",\"application_name\":\"vinservice\",\"application_uris\":[\"vinservice.cfapps.io\"],\"version\":\"52891578-5906-4846-9231-afe7048f29bf\",\"name\":\"vinservice\",\"space_name\":\"development\",\"space_id\":\"d33d438c-860a-46d3-ab33-4c2efac841be\",\"uris\":[\"vinservice.cfapps.io\"],\"users\":null}}}";

    if (StringUtils.isNotBlank(boundServicesJson)) {
      // amqpURI =  JsonPath.read(boundServicesJson,
      // "$..cloudamqp[0].credentials.uri",String.class);
      JSONArray jsonArray = JsonPath.read(boundServicesJson, "$..cloudamqp[0].credentials.uri");
      amqpURI = jsonArray.get(0).toString();
    } else {
      amqpURI = "amqp://localhost";
    }

    System.out.println("Sending messages to " + amqpURI);
    // System.exit(0);

    try {
      factory = new ConnectionFactory();
      factory.setUri(amqpURI);
      connection = factory.newConnection();
      channel = connection.createChannel();
      channel.queueDeclare("fnol", true, false, false, null);
    } catch (IOException e1) {
      // TODO Auto-generated catch block
      e1.printStackTrace();
    } catch (TimeoutException e1) {
      // TODO Auto-generated catch block
      e1.printStackTrace();
    } catch (KeyManagementException e1) {
      e1.printStackTrace();
    } catch (NoSuchAlgorithmException e1) {
      e1.printStackTrace();
    } catch (URISyntaxException e1) {
      e1.printStackTrace();
    }

    for (Fnol fnol : fnolList) {
      message = new Gson().toJson(fnol);

      try {
        channel.basicPublish(
            "amq.direct",
            "fnolKey",
            new AMQP.BasicProperties.Builder().contentType("text/plain").deliveryMode(2).build(),
            // MessageProperties.PERSISTENT_TEXT_PLAIN,
            message.getBytes());
        System.out.println("message " + message + " was published");
      } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
      }
    }

    try {
      channel.close();
      connection.close();
    } catch (IOException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
    } catch (TimeoutException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
    }
  }