Example usage for org.json.simple JSONObject JSONObject

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

Introduction

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

Prototype

JSONObject

Source Link

Usage

From source file:jsonencodedemo.JsonEncodeDemo.java

public static void main(String[] args) {
    JSONObject obj = new JSONObject();

    obj.put("name", "Jordan");
    obj.put("school", "BYUI");
    obj.put("age", new Integer(31));
    obj.put("year", new Integer(2015));
    obj.put("is_student", new Boolean(true));

    System.out.print(obj);// www  .ja  v  a 2 s .  co m
}

From source file:com.mysql.servlet.NewClass.java

public static void main(String[] args) {

    JSONObject headerFile = new JSONObject();

    JSONArray listOfAttrs = new JSONArray();
    listOfAttrs.add("id" + "");
    listOfAttrs.add("name" + "");
    listOfAttrs.add("age" + "");
    listOfAttrs.add("salary" + "");
    listOfAttrs.add("grade" + "");

    headerFile.put("Employee", listOfAttrs);

    try {/*from  ww  w  .  ja va  2  s .c o  m*/

        // Writing to a file  
        File file = new File("D:\\Employees.json");
        file.createNewFile();
        FileWriter fileWriter = new FileWriter(file);
        System.out.println("Writing JSON object to file");
        System.out.println("-----------------------");
        System.out.print(headerFile);

        fileWriter.write(headerFile.toJSONString());
        fileWriter.flush();
        fileWriter.close();

    } catch (IOException e) {
        e.printStackTrace();
    }

}

From source file:de.root1.kad.cvbackend.Test.java

public static void main(String[] args) {
    String ga = "";
    String value = "";

    JSONObject jsonResponse = new JSONObject();
    JSONObject jsonData = new JSONObject();
    jsonData.put(ga, value);//from www .ja  v  a2  s .c  o  m
    jsonResponse.put("d", jsonData);
    jsonResponse.put("i", "1");
    System.out.println(jsonResponse.toJSONString());
}

From source file:json.WriteToFile.java

public static void main(String[] args) {
    JSONObject countryObj = new JSONObject();
    countryObj.put("Name", "Kenya");
    countryObj.put("Population", new Integer(430000000));
    JSONArray listOfCounties = new JSONArray();
    listOfCounties.add("Nairobi");
    listOfCounties.add("Kiambu");
    listOfCounties.add("Murang'a");

    countryObj.put("Counties", listOfCounties);
    try {//from www .ja v  a 2s .c  o  m
        //writing to file
        File file = new File("/home/mars/Desktop/JsonExample1.json");
        file.createNewFile();
        FileWriter fw = new FileWriter(file);
        System.out.println("Writing JSON object to file");
        System.out.println("---------------------------");
        System.out.println(countryObj);

        fw.write(countryObj + "");
        fw.flush();
        fw.close();
    } catch (IOException e) {
        e.printStackTrace();
    }
}

From source file:jsonpractice.Jsonpractice.java

public static void main(String[] args) throws IOException {
    JSONObject countryObj = new JSONObject();
    countryObj.put("Name", "India");
    countryObj.put("Population", 1000000);

    JSONArray listOfStates = new JSONArray();
    listOfStates.add("Madhya Pradesh");
    listOfStates.add("Maharastra");
    listOfStates.add("Tamil Nadu");

    countryObj.put("States", listOfStates);

    try {/*from w w w .j a v  a  2s  .  c o  m*/
        File file = new File("C:\\Users\\user\\Documents\\CountryJSONFile.json");
        file.createNewFile();
        try (FileWriter fileWriter = new FileWriter(file)) {
            System.out.println("Writing JSON OBJECT to file");
            System.out.println("-----------------------------");
            System.out.println(countryObj);

            fileWriter.write(countryObj.toJSONString());
            fileWriter.flush();
        } catch (IOException e) {
        }

    } catch (IOException e) {
    }
}

From source file:contractEditor.SPConfFileEditor.java

public static void main(String[] args) {

    JSONObject obj = new JSONObject();
    ArrayList fileList = new ArrayList();

    fileList.add("confSP" + File.separator + "HOST1.json");
    fileList.add("confSP" + File.separator + "HOST2.json");
    fileList.add("confSP" + File.separator + "HOST3.json");
    fileList.add("confSP" + File.separator + "HOST4.json");

    obj.put("contract_list_of_SP", fileList);

    try {/*w  w w  . j av  a  2 s.  co m*/

        FileWriter file = new FileWriter("confSP" + File.separator + "contractFileList.json");
        file.write(obj.toJSONString());
        file.flush();
        file.close();

    } catch (IOException e) {
        e.printStackTrace();
    }

    System.out.print(obj);

}

From source file:com.owly.clnt.StatsTopCpuTest.java

/**
 * @param args//from w  w w .  j ava 2 s.c o m
 */
public static void main(String[] args) {

    Logger log = Logger.getLogger(StatsTopCpuTest.class);

    StatsTopCpu testStats = new StatsTopCpu();
    JSONObject topCPUjson = new JSONObject();

    System.out.println("Date is: " + testStats.getActualDate());
    System.out.println("My Host  is: " + testStats.getMyhost());
    log.debug("Before StatsTopCpuTest ");
    topCPUjson = testStats.getJSONTopCpu();
    log.debug("After StatsTopCpuTest ");
    System.out.println("JSON: " + topCPUjson.toString());
    System.out.println("Header: " + testStats.getHeader());

}

From source file:com.owly.clnt.StatsVmstatTest.java

/**
 * @param args/*from   ww w.  j av  a 2 s .c o m*/
 */
public static void main(String[] args) {

    Logger log = Logger.getLogger(StatsVmstatTest.class);

    StatsVmstat testStats = new StatsVmstat();
    JSONObject vmstatjson = new JSONObject();

    System.out.println("Date is: " + testStats.getActualDate());
    System.out.println("My Host  is: " + testStats.getMyhost());
    System.out.println("Header: " + testStats.getHeader());
    log.debug("Before VMstat ");
    vmstatjson = testStats.getJSONVmStat();
    log.debug("After VMstat ");
    System.out.println("JSON: " + vmstatjson.toString());

}

From source file:com.owly.clnt.StatsWinTypeperfTest.java

/**
 * @param args//from  w ww.  j  ava2 s .  c o m
 */
public static void main(String[] args) {

    Logger log = Logger.getLogger(StatsWinTypeperfTest.class);

    StatsWinTypeperf testStats = new StatsWinTypeperf();
    JSONObject topjson = new JSONObject();

    System.out.println("Date is: " + testStats.getActualDate());
    System.out.println("My Host  is: " + testStats.getMyhost());
    log.debug("Before StatsWinTypeperf ");
    topjson = testStats.getWinTypeperf();
    log.debug("After StatsWinTypeperf ");
    System.out.println("JSON: " + topjson.toString());
    System.out.println("Header: " + testStats.getHeader());

}

From source file:contractEditor.contractHOST4.java

public static void main(String[] args) {

    JSONObject obj = new JSONObject();
    obj.put("name", "HOST4");
    obj.put("context", "VM-deployment");

    //obj.put("Context", new Integer);

    HashMap serviceDescription = new HashMap();
    serviceDescription.put("location", "France");
    serviceDescription.put("certificate", "true");
    serviceDescription.put("volume", "100_GB");
    serviceDescription.put("price", "3_euro");

    obj.put("serviceDescription", serviceDescription);

    HashMap gauranteeTerm = new HashMap();
    gauranteeTerm.put("availability", "more_98_percentage");
    obj.put("gauranteeTerm", gauranteeTerm);

    //Constraint1

    ArrayList creationConstraint1 = new ArrayList();
    ArrayList totalConstraint = new ArrayList();
    totalConstraint.add(creationConstraint1);

    obj.put("creationConstraint", totalConstraint);

    try {/*from   ww  w. j a  va 2 s .c o  m*/

        FileWriter file = new FileWriter("confSP" + File.separator + "Host4.json");
        file.write(obj.toJSONString());
        file.flush();
        file.close();

    } catch (IOException e) {
        e.printStackTrace();
    }

    System.out.print(obj);

    /*
            
    JSONParser parser = new JSONParser();
            
    try {
            
    Object obj2 = parser.parse(new FileReader("confSP\\confHost1.json"));
            
    JSONObject jsonObject = (JSONObject) obj2;
            
        HashMap serviceDescription2=(HashMap) jsonObject.get("serviceDescription");
                 
        method.printHashMap(serviceDescription2);
                
                
        HashMap gauranteeTerm2=(HashMap) jsonObject.get("gauranteeTerm");
                 
        method.printHashMap(gauranteeTerm2);
                
                
                
        ArrayList creationConstraint=(ArrayList) jsonObject.get("creationConstraint");
                
        method.printArrayList(creationConstraint);
            
            
    } catch (FileNotFoundException e) {
    e.printStackTrace();
    } catch (IOException e) {
    e.printStackTrace();
    } catch (ParseException e) {
    e.printStackTrace();
    }
            
            
            
            
            
    */

}