Example usage for org.json.simple.parser JSONParser JSONParser

List of usage examples for org.json.simple.parser JSONParser JSONParser

Introduction

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

Prototype

JSONParser

Source Link

Usage

From source file:com.domsplace.DomsCommands.Objects.Chat.DomsChatObject.java

public static DomsChatObject createFromString(String msg) {
    try {/*from  ww  w  .j  a  v  a  2  s. c o m*/
        JSONParser parser = new JSONParser();
        JSONObject object = (JSONObject) parser.parse(msg);

        DomsChatObject obj = new DomsChatObject();

        for (Object item : object.keySet()) {
            Object value = object.get(item);
            String key = item.toString();
        }

        return obj;
    } catch (Exception e) {
        return null;
    }
}

From source file:msuresh.raftdistdb.RaftClient.java

public static void SetValue(String name, String key, String value) throws FileNotFoundException {
    if (key == null || key.isEmpty()) {
        return;/*w w w. j  av a  2 s  .com*/
    }
    File configFile = new File(Constants.STATE_LOCATION + name + ".info");
    if (!configFile.exists() || configFile.isDirectory()) {
        FileNotFoundException ex = new FileNotFoundException();
        throw ex;
    }
    try {
        System.out.println("Adding key .. hold on..");
        String content = new Scanner(configFile).useDelimiter("\\Z").next();
        JSONObject config = (JSONObject) (new JSONParser()).parse(content);
        Long numPart = (Long) config.get("countPartitions");
        Integer shardId = key.hashCode() % numPart.intValue();
        JSONArray memberJson = (JSONArray) config.get(shardId.toString());
        List<Address> members = new ArrayList<>();
        for (int i = 0; i < memberJson.size(); i++) {
            JSONObject obj = (JSONObject) memberJson.get(i);
            Long port = (Long) obj.get("port");
            String address = (String) obj.get("address");
            members.add(new Address(address, port.intValue()));
        }
        CopycatClient client = CopycatClient.builder(members).withTransport(new NettyTransport()).build();
        client.open().join();
        client.submit(new PutCommand(key, value)).get();
        client.close().join();
        while (client.isOpen()) {
            Thread.sleep(1000);
        }
        System.out.println("key " + key + " with value : " + value + " has been added to the cluster");
    } catch (Exception ex) {
        System.out.println(ex.toString());
    }
}

From source file:component.Configuration.java

public static boolean loadConfigFile() {
    Config loadConfig;/*from  w w  w.j  av a  2s  .  co  m*/
    try {
        File file = new File("config.json");
        if (!file.exists()) {
            return false;
        }
        JSONParser parser = new JSONParser();
        JSONObject object;
        object = (JSONObject) parser.parse(new FileReader(file));
        loadConfig = new Config();
        loadConfig.load(object);
        if (loadConfig.isConfigFill()) {
            Configuration.config = loadConfig;
            return true;
        }
        return false;
    } catch (IOException | ParseException ex) {
        return false;
    }
}

From source file:com.serena.rlc.provider.jenkins.domain.Job.java

public static Job parseSingle(String options) {
    JSONParser parser = new JSONParser();
    try {/*from ww  w  .ja va2  s. c o  m*/
        Object parsedObject = parser.parse(options);
        JSONObject jsonObject = (JSONObject) parsedObject;
        Job job = parseSingle(jsonObject);
        return job;
    } catch (ParseException e) {
        logger.error("Error while parsing input JSON - " + options, e);
    }
    return null;
}

From source file:it.polimi.proximityapi.jsonLogic.POIJsonParser.java

public static ArrayList<POI> parsePOIFile(String jsonString) {
    ArrayList<POI> poiList = new ArrayList<>();
    JSONParser parser = new JSONParser();

    JSONArray poiArray;//from  w w  w  .  j a  v a  2 s  . c  o m
    try {
        poiArray = (JSONArray) parser.parse(jsonString);
        for (int i = 0; i < poiArray.size(); i++) {
            JSONObject jsonPOI = (JSONObject) poiArray.get(i);
            POI poi = new POI();
            poi.setName((String) jsonPOI.get(JsonStrings.NAME));
            poi.setLatitude(Double.parseDouble((String) jsonPOI.get(JsonStrings.LATITUDE)));
            poi.setLongitude(Double.parseDouble((String) jsonPOI.get(JsonStrings.LONGITUDE)));
            if (!jsonPOI.get(JsonStrings.RADIUS).equals(""))
                poi.setRadius(Float.parseFloat((String) jsonPOI.get(JsonStrings.RADIUS)));
            else {
                //Default radius set when it is not set in the corresponding JSON object
                poi.setRadius(TechnologyManager.BT_START_GEOFENCE_RADIUS);
            }
            poi.setBeaconUuid((String) jsonPOI.get(JsonStrings.BEACON_UUID));
            poiList.add(poi);
        }
    } catch (ParseException e) {
        e.printStackTrace();
    }
    return poiList;
}

From source file:luceneprueba.utils.FileParser.java

static public void createFilesFromJSONArray() throws IOException {
    File dir = new File("files/input");
    File[] files = dir.listFiles();

    if (files == null) {
        System.out.println("No existe la carpeta \'input\' dentro de la carpeta files.");
        return;/*from  w ww.  j a  v  a2s  .  co  m*/
    }

    if (files.length == 0) {
        System.out.println("No hay ningun archivo en la carpeta \'input\' para ser indexado");
        return;
    }

    BufferedReader br;
    String fileContent;
    JSONArray jsonArray = null;
    JSONParser jsonParser = new JSONParser();
    int i = 1;
    FileWriter datosReviews = null;
    try {
        datosReviews = new FileWriter("files/output/datos_reviews.txt");
    } catch (IOException ex) {
        ex.printStackTrace(System.out);
    }
    for (File file : files) {
        if (file.isFile() && file.canRead() && file.getName().endsWith(".txt")) {
            System.out.println("Leyendo el archivo: " + file.getName());
            FileWriter contentReviews;
            try {
                br = new BufferedReader(new FileReader(file));
                fileContent = br.readLine();
                jsonArray = (JSONArray) jsonParser.parse(fileContent);
                Iterator it = jsonArray.iterator();
                DecimalFormat formato = new DecimalFormat("000000");
                while (it.hasNext()) {
                    JSONObject json = (JSONObject) it.next();
                    if (json.get("Genre") != null && json.get("Date") != null) {
                        contentReviews = new FileWriter(
                                "files/output/clasificador/review_clasificador_" + formato.format(i) + ".txt");
                        datosReviews.write(Integer.toString(i) + "_" + (String) json.get("Date") + "_"
                                + (String) json.get("Genre") + "_" + (String) json.get("Score") + "\n");
                        contentReviews.write(Integer.toString(i) + " " + (String) json.get("Review"));
                        i++;
                        contentReviews.close();
                    }
                }
                br.close();
            } catch (FileNotFoundException ex) {
                ex.printStackTrace(System.out);
            } catch (IOException | ParseException ex) {
                ex.printStackTrace(System.out);
            }
        }
    }
    datosReviews.close();
}

From source file:com.blogspot.ryanfx.auth.GoogleUtil.java

/**
 * Returns an email address associated with an auth token, if it exists.
 * @param token auth token//from   w w w .  j  a  v  a  2  s. c  o  m
 * @return
 * @throws LoginException
 * @throws IOException
 * @throws ParseException
 */
public static String submitUserToken(String token) throws LoginException, IOException, ParseException {
    JSONObject jsonObject = null;
    String email = null;
    JSONParser parser = new JSONParser();
    String jsonResult = null;
    jsonResult = issueTokenGetRequest(token);
    if (jsonResult != null) {
        jsonObject = (JSONObject) parser.parse(jsonResult);
        //Gets the email value from the json result
        email = (String) jsonObject.get("email");
        //Logs the request for our knowledge
        Logger.getLogger(GoogleUtil.class.getName()).severe("Garage request from user: " + email);
    }
    return email;
}

From source file:bizlogic.Sensors.java

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

    Statement st = null;/*w w  w .j ava 2 s.co m*/
    ResultSet rs = null;

    try {
        st = DBcon.createStatement();
        rs = st.executeQuery("SELECT * FROM USERCONF.SENSORLIST");

    } catch (SQLException ex) {
        Logger lgr = Logger.getLogger(Sensors.class.getName());
        lgr.log(Level.SEVERE, ex.getMessage(), ex);
    }
    try {
        FileWriter sensorsFile = new FileWriter("/var/lib/tomcat8/webapps/ROOT/Records/sensors.json");
        sensorsFile.write("");
        sensorsFile.flush();

        JSONParser parser = new JSONParser();

        JSONObject Records = new JSONObject();

        JSONObject operation_Obj = new JSONObject();
        JSONObject operand_Obj = new JSONObject();
        JSONObject unit_Obj = new JSONObject();
        JSONObject name_Obj = new JSONObject();
        JSONObject ip_Obj = new JSONObject();
        JSONObject port_Obj = new JSONObject();

        int _total = 0;

        JSONArray sensorList = new JSONArray();

        while (rs.next()) {

            JSONObject sensor_Obj = new JSONObject();
            int id = rs.getInt("sensor_id");
            String operation = rs.getString("operation");
            int operand = rs.getInt("operand");
            String unit = rs.getString("unit");
            String name = rs.getString("name");
            String ip = rs.getString("IP");
            int port = rs.getInt("port");

            sensor_Obj.put("recid", id);
            sensor_Obj.put("operation", operation);
            sensor_Obj.put("operand", operand);
            sensor_Obj.put("unit", unit);
            sensor_Obj.put("name", name);
            sensor_Obj.put("IP", ip);
            sensor_Obj.put("port", port);

            sensorList.add(sensor_Obj);
            _total++;

        }
        rs.close();

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

        sensorsFile.write(Records.toJSONString());
        sensorsFile.flush();
        sensorsFile.close();
    }

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

From source file:com.serena.rlc.provider.jira.domain.Project.java

public static List<Project> parse(String options) {
    List<Project> list = new ArrayList<>();
    JSONParser parser = new JSONParser();
    try {/*w w w  .j  av a  2s.c  o m*/
        Object parsedObject = parser.parse(options);
        JSONArray array = (JSONArray) parsedObject;
        for (Object object : array) {
            Project obj = new Project();
            JSONObject jsonObject = (JSONObject) object;
            obj.setId((String) jsonObject.get("id"));
            obj.setName((String) jsonObject.get("name"));
            obj.setKey((String) jsonObject.get("key"));
            list.add(obj);
        }
    } catch (ParseException e) {
        logger.error("Error while parsing input JSON - " + options, e);
    }

    return list;
}

From source file:it.polimi.logging.LogMessageUtils.java

public static String getSenderID(String msgJson) {
    JSONParser parser = new JSONParser();
    JSONObject jsonMsg;//  w  w  w  .  j a  v  a 2  s. c o  m
    String senderID = "";
    try {
        jsonMsg = (JSONObject) parser.parse(msgJson);
        senderID = (String) jsonMsg.get(JsonStrings.SENDER);
        return senderID;
    } catch (ParseException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

    return null;
}