Example usage for org.json.simple JSONObject put

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

Introduction

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

Prototype

V put(K key, V value);

Source Link

Document

Associates the specified value with the specified key in this map (optional operation).

Usage

From source file:com.jilk.ros.rosbridge.implementation.JSON.java

private static JSONObject convertObjectToJSONObject(Object o) {
    JSONObject result = new JSONObject();
    for (Field f : o.getClass().getFields()) {
        Object fieldObject = getFieldObject(f, o);
        if (fieldObject != null) {
            Object resultObject;//w w  w  . ja v  a 2  s  .  c o m
            if (Indication.isBase64Encoded(f))
                resultObject = convertByteArrayToBase64JSONString(fieldObject);
            else if (Indication.asArray(f))
                resultObject = convertObjectToJSONArray(fieldObject);
            else
                resultObject = convertElementToJSON(fieldObject);
            result.put(f.getName(), resultObject);
        }
    }
    return result;
}

From source file:co.mcme.animations.animations.commands.AnimationFactory.java

public static boolean saveAnimationData(Player p) {
    //Perform Animation integrity checks
    if (animationName.trim().isEmpty()) {
        p.sendMessage(ChatColor.RED/*from  w w w. j  av a  2 s .c  o m*/
                + "Animation name has not been set. Use /anim name while in Animation setup mode to set up the animation name");
        return false;
    }

    if ((null == animationDescription) || (animationDescription.trim().isEmpty())) {
        p.sendMessage(ChatColor.RED
                + "The animation needs a description. Use /anim description while in Animation setup mode to set up the animation description");
        return false;
    }

    if (null == origin) {
        p.sendMessage(ChatColor.RED
                + "Origin has not been set. Use /anim origin while in Animation setup mode to set up the animation origin");
        return false;
    }

    if (null == type) {
        p.sendMessage(ChatColor.RED
                + "Animation type has not been set. Use /anim type while in Animation setup mode to set up the animation type");
        return false;
    }

    if (frames.isEmpty()) {
        p.sendMessage(ChatColor.RED + "The Animation doesn't contain any Frame!");
        return false;
    }

    if (clips.isEmpty()) {
        p.sendMessage(ChatColor.RED + "The Animation doesn't contain any Clipboard!");
        return false;
    }
    if (triggers.isEmpty()) {
        p.sendMessage(ChatColor.RED + "The Animation doesn't contain any Trigger!");
        return false;
    }
    //Save all the schematics
    File animationFolder = new File(MCMEAnimations.MCMEAnimationsInstance.getDataFolder() + File.separator
            + "schematics" + File.separator + "animations" + File.separator + animationName);
    if (animationFolder.exists()) {
        try {
            delete(animationFolder);
        } catch (IOException ex) {
            Logger.getLogger(AnimationFactory.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
    animationFolder.mkdirs();

    for (MCMEClipboardStore cs : clips) {
        if (cs.getUses() > 0) {
            saveClipboardToFile(cs.getClip(), cs.getSchematicName(), animationFolder);
        }
    }

    //Save the configuration file
    JSONObject configuration = new JSONObject();
    configuration.put("name", animationName);
    configuration.put("world-index", p.getWorld().getName());
    JSONArray frameList = new JSONArray();
    JSONArray durationList = new JSONArray();
    for (int i = 0; i < frames.size(); i++) {
        frameList.add(frames.get(i).getSchematic().getSchematicName());
        durationList.add(frames.get(i).getDuration());
    }
    configuration.put("frames", frameList);
    configuration.put("durations", durationList);

    JSONArray originPoints = new JSONArray();
    originPoints.add((int) Math.floor(origin.getX()));
    originPoints.add((int) Math.floor(origin.getY()));
    originPoints.add((int) Math.floor(origin.getZ()));
    configuration.put("origin", originPoints);

    configuration.put("type", type.toString());

    JSONArray interactions = new JSONArray();
    for (AnimationTrigger at : triggers) {
        interactions.add(at.toJSON());
    }

    configuration.put("interactions", interactions);

    JSONArray animationActions = new JSONArray();
    for (AnimationAction a : actions) {
        animationActions.add(a.toJSON());
    }
    if (animationActions.size() > 0) {
        configuration.put("actions", animationActions);
    }

    configuration.put("creator", owner.getDisplayName());
    configuration.put("description", animationDescription);

    try {
        FileWriter fw = new FileWriter(new File(MCMEAnimations.MCMEAnimationsInstance.getDataFolder()
                + File.separator + "schematics" + File.separator + "animations" + File.separator + animationName
                + File.separator + "conf.json"));
        fw.write(configuration.toJSONString());
        fw.flush();
        fw.close();
    } catch (IOException ex) {
        p.sendMessage("Error saving Animation configuration file!");
        return false;
    }

    p.sendMessage(ChatColor.BLUE + "" + ChatColor.BOLD + "Animation " + animationName
            + " saved and ready to run! Use /anim reset to reload the configuration.");
    return true;
}

From source file:gov.nasa.jpl.memex.pooledtimeseries.PoT.java

private static void writeSimilarityToJSONFile(ArrayList<Path> files, double[][] similarities) {
      JSONObject root_json_obj = new JSONObject();

      for (int i = 0; i < similarities.length; i++) {
          JSONObject fileJsonObj = new JSONObject();

          for (int j = 0; j < similarities[0].length; j++) {
              fileJsonObj.put(files.get(j).getFileName(), similarities[i][j]);
          }// w  ww .j av a 2  s  .  c  om

          root_json_obj.put(files.get(i).getFileName(), fileJsonObj);
      }

      try {
          outputFile = outputFile.substring(0, outputFile.lastIndexOf('.')) + ".json";
          FileWriter file = new FileWriter(outputFile);
          file.write(root_json_obj.toJSONString());
          file.flush();
          file.close();
      } catch (IOException e) {
          e.printStackTrace();
      }
  }

From source file:com.stratio.deep.es.utils.UtilES.java

/**
 * converts from cell class to JSONObject
 *
 * @return/*ww  w  . j a  v  a 2  s . c  o m*/
 * @throws IllegalAccessException
 * @throws InstantiationException
 * @throws InvocationTargetException
 */
public static JSONObject getJsonFromCell(Cells cells)
        throws IllegalAccessException, InstantiationException, InvocationTargetException {

    JSONObject json = new JSONObject();
    for (Cell cell : cells) {
        if (cell.getCellValue() != null) {
            if (Collection.class.isAssignableFrom(cell.getCellValue().getClass())) {
                Collection c = (Collection) cell.getCellValue();
                Iterator iterator = c.iterator();
                List<JSONObject> innerJsonList = new ArrayList<>();

                while (iterator.hasNext()) {
                    innerJsonList.add(getJsonFromCell((Cells) iterator.next()));
                }
                json.put(cell.getCellName(), innerJsonList);
            } else if (Cells.class.isAssignableFrom(cell.getCellValue().getClass())) {
                json.put(cell.getCellName(), getJsonFromCell((Cells) cell.getCellValue()));
            } else {
                json.put(cell.getCellName(), cell.getCellValue());
            }

        }
    }

    return json;
}

From source file:fileoperations.FileOperations.java

public static JSONObject CSVToJSONObject(String CSVFileName) {
    BufferedReader fileReader = null;
    final String DELIMITER = ",";
    JSONObject ModuleVsOwners = new JSONObject();
    try {/*from ww  w  .j  a  v a 2  s.com*/
        String line = "";
        //Create the file reader
        fileReader = new BufferedReader(new FileReader(CSVFileName));

        //Read the file line by line
        while ((line = fileReader.readLine()) != null) {
            //Get all tokens available in line
            String[] tokens = line.split(DELIMITER);
            if (!tokens[0].equals("Module")) {
                ModuleVsOwners.put(tokens[0], tokens[1]);
            }
        }
        return ModuleVsOwners;
    } catch (Exception e) {

        e.printStackTrace();
        return null;
    } finally {
        try {
            fileReader.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

}

From source file:com.aerothai.database.user.UserService.java

/**
 * Method to check whether uname and pwd combination are correct
 * /*from  ww  w  . j ava 2s .  c o  m*/
 * @param uname
 * @param pwd
 * @return
 * @throws Exception
 */
public static String checkLogin(String uname, String pwd) throws Exception {
    boolean isUserAvailable = false;
    Connection dbConn = null;
    JSONObject obj = new JSONObject();
    JSONObject jsonData = new JSONObject();
    try {
        dbConn = DBConnection.createConnection();

        Statement stmt = dbConn.createStatement();
        String query = "SELECT * FROM user WHERE username = '" + uname + "' AND pwd=" + "'" + pwd + "'";
        System.out.println(query);
        ResultSet rs = stmt.executeQuery(query);

        while (rs.next()) {
            //System.out.println(rs.getString(1) + rs.getString(2) + rs.getString(3));
            isUserAvailable = true;
            //JSONObject jsonData = new JSONObject();
            jsonData.put("iduser", rs.getInt("iduser"));
            jsonData.put("name", rs.getString("name"));
            jsonData.put("lastname", rs.getString("lastname"));
            jsonData.put("idposition", rs.getInt("idposition"));
            jsonData.put("iddept", rs.getInt("iddept"));
            jsonData.put("idunit", rs.getInt("idunit"));
            jsonData.put("idrole", rs.getInt("idrole"));
            jsonData.put("address", rs.getString("address"));
            jsonData.put("email", rs.getString("email"));
            jsonData.put("tel", rs.getString("tel"));
            jsonData.put("actunit", rs.getString("actunit"));
            jsonData.put("username", rs.getString("username"));
            jsonData.put("photo", rs.getString("photo"));
            jsonData.put("actcust", rs.getString("actcust"));
            //objList.add(rs.getString("pwd"));

        }
        if (isUserAvailable) {

            obj.put("tag", "login");
            obj.put("msg", "done");
            obj.put("status", true);
            obj.put("data", jsonData.toJSONString());
        } else {
            obj.put("tag", "login");
            obj.put("msg", "Incorrect Email or Password");
            obj.put("status", false);
        }
    } 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:io.github.casnix.mcdropshop.util.configsys.Shops.java

public static boolean checkShopsFile() {
    File shopsFile = new File("./plugins/mcDropShop/Shops.json");

    if (!shopsFile.exists())
        return false;

    try {/*ww  w  .  j  av  a  2s.  c  o  m*/
        String configTable = new String(Files.readAllBytes(Paths.get("./plugins/mcDropShop/Shops.json")));

        JSONParser parser = new JSONParser();

        Object obj = parser.parse(configTable);

        JSONObject jsonObj = (JSONObject) obj;

        String listVersion = (String) jsonObj.get("listVersion");

        if (listVersion == null || !listVersion.equals("0.2.0")) {
            // Old version
            jsonObj.put("listVersion", "0.2.0");

            // Update Shops.json
            FileWriter shopsJSON = new FileWriter("./plugins/mcDropShop/Shops.json");

            shopsJSON.write(jsonObj.toJSONString());

            shopsJSON.flush();
            shopsJSON.close();
        }

        return true;
    } catch (Exception e) {
        Bukkit.getLogger().severe("mcDropShop failed on setup check in Shops.json");

        e.printStackTrace();

        return false;
    }
}

From source file:gov.nih.nci.caintegrator.application.lists.ajax.CommonListFunctions.java

public static String uniteLists(String[] sLists, String groupName, String groupType, String action) {

    JSONObject res = new JSONObject();
    String results = "pass";

    UserListBeanHelper helper = new UserListBeanHelper();
    try {//from   w  w  w  .j a  v a  2  s . c  o m
        List<String> al = Arrays.asList(sLists);
        if (action.equals("join")) {
            helper.uniteLists(al, groupName, ListType.valueOf(groupType));
        } else if (action.equalsIgnoreCase("difference")) {
            helper.differenceLists(al, groupName, ListType.valueOf(groupType));
        } else {
            if (helper.isIntersection(al)) {
                helper.intersectLists(al, groupName, ListType.valueOf(groupType));
            } else
                results = "fail";
        }
    } catch (Exception e) {
        results = "fail";
    }
    res.put("results", results);
    res.put("groupType", groupType);
    res.put("action", action);

    return res.toString();
}

From source file:ab.server.proxy.message.ProxyClickMessage.java

@SuppressWarnings("unchecked")
@Override/*from w  w  w. java 2  s .c o m*/
public JSONObject getJSON() {
    JSONObject o = new JSONObject();
    o.put("x", x);
    o.put("y", y);
    return o;
}

From source file:io.hawt.testing.env.builtin.NotFound.java

@Override
public void handle(HttpServletRequest req, HttpServletResponse resp) throws IOException {
    resp.setStatus(HttpServletResponse.SC_NOT_FOUND);
    resp.setContentType("application/json");
    resp.setCharacterEncoding("UTF-8");
    JSONObject r = new JSONObject();
    r.put("error", HttpServletResponse.SC_NOT_FOUND);
    r.put("message", "Environment not found");
    resp.getWriter().println(r.toJSONString());
    resp.getWriter().close();//from   ww w.ja v a  2  s  . c o m
}