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:org.kitodo.data.index.elasticsearch.type.DocketType.java

@SuppressWarnings("unchecked")
@Override/*from   w  w  w .  j  a v  a 2 s  .  c  o  m*/
public HttpEntity createDocument(Docket docket) {

    LinkedHashMap<String, String> orderedDocketMap = new LinkedHashMap<>();
    orderedDocketMap.put("name", docket.getName());
    orderedDocketMap.put("file", docket.getFile());
    JSONObject docketObject = new JSONObject(orderedDocketMap);

    return new NStringEntity(docketObject.toJSONString(), ContentType.APPLICATION_JSON);
}

From source file:com.iitb.cse.Utils.java

/**
 * genetated and returns the String in Json format. String contains
 * information about the action for sending AP settings file This string is
 * later sent to filtered devices.//from  w w  w  .  j  a  v a 2 s. c o  m
 */
@SuppressWarnings("unchecked")
static String getControlFileJson(String message, String timeout, String logBgTraffic) {
    JSONObject obj = new JSONObject();
    obj.put(Constants.action, Constants.sendControlFile);
    obj.put(Constants.textFileFollow, Boolean.toString(true));
    obj.put(Constants.serverTime, Long.toString(Calendar.getInstance().getTimeInMillis()));
    obj.put(Constants.message, message);
    obj.put(Constants.timeout, timeout);
    obj.put("selectiveLog", logBgTraffic);
    String jsonString = obj.toJSONString();

    Date obj1 = new Date(Long.parseLong((String) obj.get(Constants.serverTime)));

    System.out.println(jsonString + obj1);
    return jsonString;
}

From source file:org.kitodo.data.index.elasticsearch.type.RulesetType.java

@SuppressWarnings("unchecked")
@Override//from w  ww.j a v a  2 s .c  o m
public HttpEntity createDocument(Ruleset ruleset) {

    LinkedHashMap<String, String> orderedRulesetMap = new LinkedHashMap<>();
    orderedRulesetMap.put("title", ruleset.getTitle());
    orderedRulesetMap.put("file", ruleset.getFile());
    orderedRulesetMap.put("fileContent", "");
    JSONObject rulesetObject = new JSONObject(orderedRulesetMap);

    return new NStringEntity(rulesetObject.toJSONString(), ContentType.APPLICATION_JSON);
}

From source file:com.avinashbehera.sabera.network.HttpClient.java

public static JSONObject SendHttpPostUsingUrlConnection(String url, JSONObject jsonObjSend) {

    URL sendUrl;//from  ww w  .  ja v  a 2s.c om
    try {
        sendUrl = new URL(url);
    } catch (MalformedURLException e) {
        Log.d(TAG, "SendHttpPostUsingUrlConnection malformed URL");
        return null;
    }

    HttpURLConnection conn = null;

    try {
        conn = (HttpURLConnection) sendUrl.openConnection();
        conn.setReadTimeout(15000 /* milliseconds */);
        conn.setConnectTimeout(15000 /* milliseconds */);
        conn.setRequestMethod("POST");
        conn.setChunkedStreamingMode(1024);
        conn.setRequestProperty("Content-Type", "application/json");
        conn.setRequestProperty("Accept", "application/json");
        conn.setRequestProperty("Connection", "Keep-Alive");
        conn.addRequestProperty("Content-length", jsonObjSend.toJSONString().length() + "");
        conn.setDoInput(true);
        conn.setDoOutput(true);

        OutputStream os = conn.getOutputStream();
        BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(os, "UTF-8"));
        //writer.write(getPostDataStringfromJsonObject(jsonObjSend));
        Log.d(TAG, "jsonobjectSend = " + jsonObjSend.toString());
        //writer.write(jsonObjSend.toString());
        writer.write(String.valueOf(jsonObjSend));

        writer.flush();
        writer.close();
        os.close();

        int responseCode = conn.getResponseCode();
        Log.d(TAG, "responseCode = " + responseCode);

        if (responseCode == HttpsURLConnection.HTTP_OK) {

            Log.d(TAG, "responseCode = HTTP OK");

            InputStream instream = conn.getInputStream();

            String resultString = convertStreamToString(instream);
            instream.close();
            Log.d(TAG, "resultString = " + resultString);
            //resultString = resultString.substring(1,resultString.length()-1); // remove wrapping "[" and "]"

            // Transform the String into a JSONObject
            JSONParser parser = new JSONParser();
            JSONObject jsonObjRecv = (JSONObject) parser.parse(resultString);
            // Raw DEBUG output of our received JSON object:
            Log.i(TAG, "<JSONObject>\n" + jsonObjRecv.toString() + "\n</JSONObject>");

            return jsonObjRecv;

        }

    } catch (Exception e) {
        // More about HTTP exception handling in another tutorial.
        // For now we just print the stack trace.
        e.printStackTrace();
    } finally {
        if (conn != null) {
            conn.disconnect();
        }
    }
    return null;

}

From source file:org.kitodo.data.index.elasticsearch.type.HistoryType.java

@SuppressWarnings("unchecked")
@Override/* www  . j a  va2 s .  co m*/
public HttpEntity createDocument(History history) {

    LinkedHashMap<String, String> orderedHistoryMap = new LinkedHashMap<>();
    orderedHistoryMap.put("numericValue", history.getNumericValue().toString());
    orderedHistoryMap.put("stringValue", history.getStringValue());
    orderedHistoryMap.put("type", history.getHistoryType().toString());
    DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
    String date = history.getDate() != null ? dateFormat.format(history.getDate()) : null;
    orderedHistoryMap.put("date", date);
    orderedHistoryMap.put("process", history.getProcess().getId().toString());

    JSONObject historyObject = new JSONObject(orderedHistoryMap);

    return new NStringEntity(historyObject.toJSONString(), ContentType.APPLICATION_JSON);
}

From source file:cv_vacature_bank.Jsonhandler.java

public void createFile(String type) {
    Writer writer = null;/*from   w w  w  .  ja va2  s.  co  m*/
    id++;

    try {
        writer = new BufferedWriter(
                new OutputStreamWriter(new FileOutputStream(type + Integer.toString(id) + ".txt"), "utf-8"));

        for (JSONObject jo : objs) {
            writer.write(jo.toJSONString() + "\n\n");
        }
    } catch (IOException ex) {
        // report
    } finally {
        try {
            writer.close();
        } catch (Exception ex) {
        }
    }
}

From source file:com.twitter.hdfsdu.DataTransformerTest.java

/**
 * Rigourous Test :-)/*from   ww  w  .ja va2  s .  c  o m*/
 */
public void testDataTransformer() throws Exception {
    //load file
    InputStream inputStream = DataTransformerTest.class.getClassLoader()
            .getResourceAsStream("com/twitter/hdfsdu/data/example.txt");
    InputStreamReader input = new InputStreamReader(inputStream);
    LineReader l = new LineReader(input);
    String line = l.readLine();
    List<NodeData> nlist = new ArrayList<NodeData>();
    while (line != null) {
        String[] entries = line.split("\t");
        NodeData n = new NodeData();
        n.path = entries[0];
        n.fileSize = entries[1];
        nlist.add(n);
        line = l.readLine();
    }
    JSONObject ans = DataTransformer.getJSONTree("/", 2, nlist);
    System.out.println(ans.toJSONString());
    //        assertTrue( true );
}

From source file:ClubImpl.java

public String getNews() throws IOException {
    JSONParser parser = new JSONParser();
    Object obj = null;//www .  j  av  a2s  . c o  m
    try {
        File f = new File(".");
        System.out.println(f.getAbsolutePath());
        File relative = new File("News.json");
        obj = parser.parse(new FileReader(relative));
    } catch (Exception ex) {
        Logger.getLogger(ClubImpl.class.getName()).log(Level.SEVERE, null, ex);
    }

    JSONObject jsonObject = (JSONObject) obj;
    return jsonObject.toJSONString();
}

From source file:ClubImpl.java

public String getEvents() throws IOException {
    JSONParser parser = new JSONParser();

    Object obj = null;/*from   w  w w .  j av a2  s  .  co  m*/
    try {
        File f = new File(".");
        System.out.println(f.getAbsolutePath());
        File relative = new File("Events.json");
        obj = parser.parse(new FileReader(relative));
    } catch (Exception ex) {
        Logger.getLogger(ClubImpl.class.getName()).log(Level.SEVERE, null, ex);
    }

    JSONObject jsonObject = (JSONObject) obj;
    return jsonObject.toJSONString();
}

From source file:ClubImpl.java

public String getAboutUs() {
    JSONParser parser = new JSONParser();

    Object obj = null;// w w w.  java2s  . co m
    try {
        File f = new File(".");
        System.out.println(f.getAbsolutePath());
        File relative = new File("AboutUs.json");
        obj = parser.parse(new FileReader(relative));
    } catch (Exception ex) {
        Logger.getLogger(ClubImpl.class.getName()).log(Level.SEVERE, null, ex);
    }

    JSONObject jsonObject = (JSONObject) obj;
    return jsonObject.toJSONString();

}