Example usage for org.json.simple JSONObject toJSONString

List of usage examples for org.json.simple JSONObject toJSONString

Introduction

In this page you can find the example usage for org.json.simple JSONObject toJSONString.

Prototype

public String toJSONString() 

Source Link

Usage

From source file:bizlogic.Records.java

public static void list(Connection DBcon) throws IOException, ParseException, SQLException {

    Statement st;/*from  w  w  w  .  j  ava2  s  . co  m*/
    ResultSet rs = null;

    try {
        st = DBcon.createStatement();
        rs = st.executeQuery("SELECT userconf.log_list.sensor_id, " + "userconf.log_list.smpl_interval, "
                + "userconf.log_list.running, " + "userconf.log_list.name AS log_name, "
                + "userconf.log_list.log_id, " + "userconf.sensorlist.name AS sensor_name "
                + "FROM USERCONF.LOG_LIST " + "JOIN userconf.sensorlist "
                + "ON userconf.log_list.sensor_id=userconf.sensorlist.sensor_id");

    } catch (SQLException ex) {
        Logger lgr = Logger.getLogger(Records.class.getName());
        lgr.log(Level.SEVERE, ex.getMessage(), ex);
    }

    try {
        FileWriter recordsFile = new FileWriter("/var/lib/tomcat8/webapps/ROOT/Records/records.json");
        //BufferedWriter recordsFile = new BufferedWriter(_file);
        //recordsFile.write("");
        //recordsFile.flush(); 

        FileReader fr = new FileReader("/var/lib/tomcat8/webapps/ROOT/Records/records.json");
        BufferedReader br = new BufferedReader(fr);

        JSONObject Records = new JSONObject();

        int _total = 0;

        JSONArray recordList = new JSONArray();

        while (rs.next()) {

            String isRunningStr;

            JSONObject sensor_Obj = new JSONObject();

            int sensor_id = rs.getInt("sensor_id");
            sensor_Obj.put("sensor_id", sensor_id);

            String smpl_interval = rs.getString("smpl_interval");
            sensor_Obj.put("smpl_interval", smpl_interval);

            Boolean running = rs.getBoolean("running");
            if (running) {
                //System.out.print("1");
                isRunningStr = "ON";
            } else {
                //System.out.print("0");
                isRunningStr = "OFF";
            }
            sensor_Obj.put("running", isRunningStr);

            String log_name = rs.getString("log_name");
            sensor_Obj.put("log_name", log_name);

            String sensor_name = rs.getString("sensor_name");
            sensor_Obj.put("sensor_name", sensor_name);

            int log_id = rs.getInt("log_id");
            sensor_Obj.put("recid", log_id);

            recordList.add(sensor_Obj);
            _total++;

        }

        rs.close();

        Records.put("total", _total);
        Records.put("records", recordList);

        recordsFile.write(Records.toJSONString());
        recordsFile.flush();

        recordsFile.close();

        System.out.print(Records.toJSONString());
        System.out.print(br.readLine());

    }

    catch (IOException ex) {
        Logger.getLogger(Records.class.getName()).log(Level.SEVERE, null, ex);
    }
}

From source file:ch.simas.jtoggl.JToggl.java

/**
 * All users in the workspace with the given id.
 * //from  ww  w.j a  va2 s  .c  o m
 * @param workspaceId
 *            id of the workspace
 * @return all users
 */
public List<User> getWorkspaceUsers(long workspaceId) {
    Client client = prepareClient();
    String url = WORKSPACES_USERS.replace(PLACEHOLDER, String.valueOf(workspaceId));
    WebResource webResource = client.resource(url);

    String response = webResource.get(String.class);
    JSONArray data = (JSONArray) JSONValue.parse(response);

    List<User> users = new ArrayList<User>();
    if (data != null) {
        for (Object obj : data) {
            JSONObject entryObject = (JSONObject) obj;
            users.add(new User(entryObject.toJSONString()));
        }
    }
    return users;
}

From source file:ch.simas.jtoggl.JToggl.java

/**
 * All active tasks in the workspace with the given id.
 * /*from  w w  w  .j  ava 2 s .  c  o  m*/
 * @param workspaceId
 *            id of the workspace
 * @return all tasks
 */
public List<Task> getActiveWorkspaceTasks(long workspaceId) {
    Client client = prepareClient();
    String url = WORKSPACE_TASKS.replace(PLACEHOLDER, String.valueOf(workspaceId));
    WebResource webResource = client.resource(url);

    String response = webResource.get(String.class);
    JSONArray data = (JSONArray) JSONValue.parse(response);

    List<Task> tasks = new ArrayList<Task>();
    if (data != null) {
        for (Object obj : data) {
            JSONObject entryObject = (JSONObject) obj;
            tasks.add(new Task(entryObject.toJSONString()));
        }
    }
    return tasks;
}

From source file:io.github.collaboratory.LauncherCWL.java

private void writeJob(String jobOutputPath, JSONObject newJson) {
    try {// w  w w .ja va2 s  .c om
        //TODO: investigate, why is this replacement occurring?
        final String replace = newJson.toJSONString().replace("\\", "");
        FileUtils.writeStringToFile(new File(jobOutputPath), replace, StandardCharsets.UTF_8);
    } catch (IOException e) {
        throw new RuntimeException("Could not write job ", e);
    }
}

From source file:ch.simas.jtoggl.JToggl.java

/**
 * All projects in the workspace with the given id.
 * /*ww  w. j  av  a2 s  .c  o  m*/
 * @param workspaceId
 *            id of the workspace
 * @return all projects
 */
public List<Project> getWorkspaceProjects(long workspaceId) {
    Client client = prepareClient();
    String url = WORKSPACE_PROJECTS.replace(PLACEHOLDER, String.valueOf(workspaceId));
    WebResource webResource = client.resource(url);

    String response = webResource.get(String.class);
    JSONArray data = (JSONArray) JSONValue.parse(response);

    List<Project> projects = new ArrayList<Project>();
    if (data != null) {
        for (Object obj : data) {
            JSONObject entryObject = (JSONObject) obj;
            projects.add(new Project(entryObject.toJSONString()));
        }
    }
    return projects;
}

From source file:ch.simas.jtoggl.JToggl.java

/**
 * Get workspaces./*w w  w  . j  a va2 s .com*/
 * 
 * @return list of {@link Workspace}
 */
public List<Workspace> getWorkspaces() {
    Client client = prepareClient();
    WebResource webResource = client.resource(WORKSPACES);

    String response = webResource.get(String.class);
    JSONArray data = (JSONArray) JSONValue.parse(response);

    List<Workspace> workspaces = new ArrayList<Workspace>();
    if (data != null) {
        for (Object obj : data) {
            JSONObject entryObject = (JSONObject) obj;
            workspaces.add(new Workspace(entryObject.toJSONString()));
        }
    }
    return workspaces;
}

From source file:ApplicationIntegrationTest.java

@Test
public void test4CanUpdateBook() {
    writeDummyData();/*w ww  .j  av a 2s  .c om*/

    BookDTO book2 = TestDataGenerator.createBook();
    book2.setUser(_userDTO);

    JSONObject userJson = new JSONObject();
    userJson.put("id", _userDTO.getId());
    userJson.put("username", _userDTO.getUsername());

    JSONObject bookJson = new JSONObject();
    bookJson.put("id", _bookDTO.getId());

    bookJson.put("title", book2.getTitle());
    bookJson.put("author", book2.getAuthor());
    bookJson.put("publisher", book2.getPublisher());
    bookJson.put("isbn", book2.getIsbn());
    bookJson.put("year", book2.getYear());

    bookJson.put("user", userJson);

    given().contentType(JSON).body(bookJson.toJSONString()).when().post("/updateBook").then()
            .statusCode(HttpStatus.SC_OK).log().ifError();

    when().get("/readBook?id=".concat(_bookDTO.getId().toString())).then().contentType(JSON)
            .statusCode(HttpStatus.SC_OK).body("title", equalTo(book2.getTitle()))
            .body("author", equalTo(book2.getAuthor())).body("publisher", equalTo(book2.getPublisher()))
            .body("isbn", equalTo(book2.getIsbn())).body("year", equalTo(book2.getYear())).log().ifError();
}

From source file:ch.simas.jtoggl.JToggl.java

/**
 * Get time entries started in a specific time range. 
 * By default, the number of days from the field "How long are time entries 
 * visible in the timer" under "My settings" in Toggl is used to determine 
 * which time entries to return but you can specify another date range using 
 * start_date and end_date parameters.// w  ww . j a v a 2 s. c om
 * 
 * @param startDate
 * @param endDate
 * @return list of {@link TimeEntry}
 */
public List<TimeEntry> getTimeEntries(Date startDate, Date endDate) {
    Client client = prepareClient();
    WebResource webResource = client.resource(TIME_ENTRIES);

    if (startDate != null && endDate != null) {
        MultivaluedMap queryParams = new MultivaluedMapImpl();
        queryParams.add("start_date", DateUtil.convertDateToString(startDate));
        queryParams.add("end_date", DateUtil.convertDateToString(endDate));
        webResource = webResource.queryParams(queryParams);
    }
    String response = webResource.get(String.class);
    JSONArray data = (JSONArray) JSONValue.parse(response);

    List<TimeEntry> entries = new ArrayList<TimeEntry>();
    if (data != null) {
        for (Object obj : data) {
            JSONObject entryObject = (JSONObject) obj;
            entries.add(new TimeEntry(entryObject.toJSONString()));
        }
    }
    return entries;
}

From source file:fi.aalto.drumbeat.apicalls.bimserver.BIMServerJSONApi.java

@SuppressWarnings("unchecked")
public void getProjects(ComboBox selection, Map<String, Long> projects) {

    if (token == null) {
        System.err.println("No login");
        return;/*w w  w . j  av a2 s  . c o m*/
    }
    try {
        if (selection.isVisible())
            selection.removeAllItems();
    } catch (Exception e) {
        //update before table initalization?
        return;
    }
    projects.clear();
    JSONObject getAllProjects = new JSONObject();

    JSONObject parameters = new JSONObject();
    // parameters.put( "onlyTopLevel","true");

    JSONObject request = new JSONObject();
    request.put("interface", "ServiceInterface");
    request.put("method", "getAllReadableProjects");
    request.put("parameters", parameters);

    getAllProjects.put("token", token);
    getAllProjects.put("request", request);

    System.out.println(getAllProjects);
    System.out.println("Result:");
    JSONObject result = http(bimserver_api_url, getAllProjects.toJSONString());
    JSONObject response = (JSONObject) result.get("response");
    if (response != null) {
        JSONArray get_result = (JSONArray) response.get("result");
        if (get_result != null) {
            for (Object obj : get_result) {

                // Add a row the hard way

                JSONObject project = (JSONObject) obj;
                String name = (String) project.get("name");
                if (name != null)
                    selection.addItem(name);

                Long project_id = (Long) project.get("oid");
                if (project_id != null) {
                    projects.put(name, project_id);
                } else
                    System.err.println("ERROR no project ID!");

            }
        } else {
            JSONObject bs_exception = (JSONObject) response.get("exception");
            if (bs_exception != null) {
                String bs_message = (String) bs_exception.get("message");
                System.err.println("ERROR " + bs_message);
            }
        }
    }

}

From source file:ch.simas.jtoggl.JToggl.java

/** 
 * Create a new task./*from   w w w.  j  a  v a  2s .  com*/
 * 
 * @param task
 * @return created {@link Task}
 */
public Task createTask(Task task) {
    Client client = prepareClient();
    WebResource webResource = client.resource(TASKS);

    JSONObject object = createTaskRequestParameter(task);
    String response = webResource.entity(object.toJSONString(), MediaType.APPLICATION_JSON_TYPE)
            .post(String.class);

    object = (JSONObject) JSONValue.parse(response);
    JSONObject data = (JSONObject) object.get(DATA);
    return new Task(data.toJSONString());
}