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:com.fujitsu.dc.test.jersey.box.odatacol.AbstractUserDataTest.java

/**
 * ?./*  ww w .j  a  va  2s.  c  om*/
 * @param cell ??
 * @param box ??
 * @param col ??
 * @param entityType ??
 * @param userDataId ID
 * @param body 
 * @return ?
 */
protected TResponse updateUserData(String cell, String box, String col, String entityType, String userDataId,
        JSONObject body) {
    return Http.request("box/odatacol/update.txt").with("cell", cell).with("box", box).with("collection", col)
            .with("entityType", entityType).with("id", userDataId).with("accept", MediaType.APPLICATION_JSON)
            .with("contentType", MediaType.APPLICATION_JSON).with("ifMatch", "*")
            .with("token", DcCoreConfig.getMasterToken()).with("body", body.toJSONString()).returns()
            .statusCode(HttpStatus.SC_NO_CONTENT).debug();

}

From source file:Activities.java

private String addData(String endpoint) {
    String data = null;/*from ww w.j  av a  2s.c om*/
    try {
        // Construct request payload
        JSONObject attrObj = new JSONObject();
        attrObj.put("name", "URL");
        attrObj.put("value", "http://www.nvidia.com/game-giveaway");

        JSONArray attrArray = new JSONArray();
        attrArray.add(attrObj);

        TimeZone tz = TimeZone.getTimeZone("UTC");
        DateFormat df = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm'Z'");
        df.setTimeZone(tz);
        String dateAsISO = df.format(new Date());

        // Required attributes
        JSONObject obj = new JSONObject();
        obj.put("leadId", "1001");
        obj.put("activityDate", dateAsISO);
        obj.put("activityTypeId", "1001");
        obj.put("primaryAttributeValue", "Game Giveaway");
        obj.put("attributes", attrArray);
        System.out.println(obj);

        // Make request
        URL url = new URL(endpoint);
        HttpsURLConnection urlConn = (HttpsURLConnection) url.openConnection();
        urlConn.setRequestMethod("POST");
        urlConn.setAllowUserInteraction(false);
        urlConn.setDoOutput(true);
        urlConn.setRequestProperty("Content-type", "application/json");
        urlConn.setRequestProperty("accept", "application/json");
        urlConn.connect();
        OutputStream os = urlConn.getOutputStream();
        os.write(obj.toJSONString().getBytes());
        os.close();

        // Inspect response
        int responseCode = urlConn.getResponseCode();
        if (responseCode == 200) {
            System.out.println("Status: 200");
            InputStream inStream = urlConn.getInputStream();
            data = convertStreamToString(inStream);
            System.out.println(data);
        } else {
            System.out.println(responseCode);
            data = "Status:" + responseCode;
        }
    } catch (MalformedURLException e) {
        System.out.println("URL not valid.");
    } catch (IOException e) {
        System.out.println("IOException: " + e.getMessage());
        e.printStackTrace();
    }

    return data;
}

From source file:dk.dma.ais.store.FileExportRest.java

@SuppressWarnings("unchecked")
private void writeMetaUpdate(long timestamp, long packageCount) {

    // System.out.println("Updating meta!");
    FileWriter file = null;//from   w  ww  .ja va2  s .  c o  m
    JSONObject obj = new JSONObject();
    obj.put("filename", fileName);
    obj.put("timestamp", timestamp);
    obj.put("packageCount", new Long(packageCount));

    try {

        file = new FileWriter(filePath + metaFileName);
        file.write(obj.toJSONString());
        file.flush();

    } catch (IOException e) {
        // e.printStackTrace();
        System.out.println("Failed to write because reasons " + e.getMessage());
    } finally {
        try {
            file.close();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
            System.out.println("Failed to close file! " + e.getMessage());
        }
    }
}

From source file:org.exfio.weave.storage.StorageContext.java

@SuppressWarnings("unchecked")
private String encodeWeaveBasicObject(WeaveBasicObject wbo) {
    JSONObject jobj = new JSONObject();

    jobj.put("id", wbo.id);
    jobj.put("payload", wbo.payload);
    if (wbo.modified != null) {
        jobj.put("modified", String.format("%.2f", wbo.modified));
    }/*  w  w  w.  j ava  2  s . c o  m*/
    if (wbo.sortindex != null) {
        jobj.put("sortindex", wbo.sortindex);
    }
    if (wbo.ttl != null) {
        jobj.put("ttl", wbo.ttl);
    }

    return jobj.toJSONString();
}

From source file:com.aerothai.database.building.BuildingService.java

/**
* Method to check whether uname and pwd combination are correct
* 
* @param uname//from w  ww .  ja v a  2  s.c o  m
* @param pwd
* @return
* @throws Exception
*/
public String updateBuilding(String query) throws Exception {

    Connection dbConn = null;
    JSONObject obj = new JSONObject();

    try {
        dbConn = DBConnection.createConnection();

        Statement stmt = dbConn.createStatement();
        System.out.println(query);
        stmt.executeUpdate(query);

        obj.put("tag", "update");
        obj.put("msg", "done");
        obj.put("status", true);

    } catch (SQLException sqle) {
        throw sqle;
    } catch (Exception e) {
        // TODO Auto-generated catch block
        if (dbConn != null) {
            dbConn.close();
        }
        throw e;
    } finally {
        if (dbConn != null) {
            dbConn.close();
        }
    }
    return obj.toJSONString();
}

From source file:com.aerothai.database.building.BuildingService.java

/**
 * Method to check whether uname and pwd combination are correct
 * //from  w w  w.  j  a v  a2s .  co  m
 * @param uname
 * @param pwd
 * @return
 * @throws Exception
 */
public String deleteBuilding(String query) throws Exception {

    Connection dbConn = null;
    JSONObject obj = new JSONObject();

    try {
        dbConn = DBConnection.createConnection();

        Statement stmt = dbConn.createStatement();
        System.out.println(query);
        stmt.executeUpdate(query);

        obj.put("tag", "delete");
        obj.put("msg", "done");
        obj.put("status", true);

    } catch (SQLException sqle) {
        throw sqle;
    } catch (Exception e) {
        // TODO Auto-generated catch block
        if (dbConn != null) {
            dbConn.close();
        }
        throw e;
    } finally {
        if (dbConn != null) {
            dbConn.close();
        }
    }
    return obj.toJSONString();
}

From source file:com.aerothai.database.os.OsService.java

/**
* Method to check whether uname and pwd combination are correct
* 
* @param uname//ww w  .  java2 s .  co  m
* @param pwd
* @return
* @throws Exception
*/
public String updateOs(String query) throws Exception {

    Connection dbConn = null;
    JSONObject obj = new JSONObject();

    try {
        dbConn = DBConnection.createConnection();

        Statement stmt = dbConn.createStatement();
        System.out.println(query);
        stmt.executeUpdate(query);

        obj.put("tag", "update");
        obj.put("msg", "done");
        obj.put("status", true);

    } catch (SQLException sqle) {
        throw sqle;
    } catch (Exception e) {
        // TODO Auto-generated catch block
        if (dbConn != null) {
            dbConn.close();
        }
        throw e;
    } finally {
        if (dbConn != null) {
            dbConn.close();
        }
    }
    return obj.toJSONString();
}

From source file:com.aerothai.database.os.OsService.java

/**
 * Method to check whether uname and pwd combination are correct
 * /* ww  w .j  a  va2 s. c om*/
 * @param uname
 * @param pwd
 * @return
 * @throws Exception
 */
public String deleteOs(String query) throws Exception {

    Connection dbConn = null;
    JSONObject obj = new JSONObject();

    try {
        dbConn = DBConnection.createConnection();

        Statement stmt = dbConn.createStatement();
        System.out.println(query);
        stmt.executeUpdate(query);

        obj.put("tag", "delete");
        obj.put("msg", "done");
        obj.put("status", true);

    } catch (SQLException sqle) {
        throw sqle;
    } catch (Exception e) {
        // TODO Auto-generated catch block
        if (dbConn != null) {
            dbConn.close();
        }
        throw e;
    } finally {
        if (dbConn != null) {
            dbConn.close();
        }
    }
    return obj.toJSONString();
}

From source file:com.p000ison.dev.simpleclans2.JSONFlags.java

@Override
public String serialize() {
    if (data.isEmpty()) {
        return null;
    }/*from w  w  w  . j  av a2  s  .  c o  m*/
    JSONObject json = new JSONObject();

    for (Map.Entry<String, Object> entry : data.entrySet()) {
        String key = entry.getKey();
        Object value = entry.getValue();

        if (key == null || value == null) {
            continue;
        }

        if (value instanceof Collection) {
            Collection collection = (Collection) value;
            if (collection.isEmpty()) {
                continue;
            }
            JSONArray list = new JSONArray();
            list.addAll(collection);
            value = list;
        }

        json.put(key, value);
    }

    return json.toJSONString();
}

From source file:com.fujitsu.dc.test.jersey.box.odatacol.AbstractUserDataTest.java

/**
 * ??.//  ww w . j  a va 2s  . c  om
 * @param body 
 * @param sc ?
 * @return ?
 */
protected TResponse createUserData(JSONObject body, int sc) {
    TResponse response = Http.request("box/odatacol/create.txt").with("cell", cellName).with("box", boxName)
            .with("collection", colName).with("entityType", entityTypeName)
            .with("accept", MediaType.APPLICATION_JSON).with("contentType", MediaType.APPLICATION_JSON)
            .with("token", "Bearer " + DcCoreConfig.getMasterToken()).with("body", body.toJSONString())
            .returns().statusCode(sc).debug();

    return response;
}