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.starr.smartbuilds.service.BuildService.java

public List<Block> parseBlocks(String data) throws ParseException {
    List<Item> itemsDB = itemDAO.listItems();
    JSONParser parser = new JSONParser();
    JSONObject json = (JSONObject) parser.parse(data);
    List<Block> blocks = new ArrayList();
    for (Object arr : json.values()) {
        JSONObject json_block = (JSONObject) arr;
        Block block = new Block();
        block.setName((String) json_block.get("name"));
        block.setComment((String) json_block.get("comment"));
        List<Item> items = new ArrayList<Item>();
        JSONArray json_items = (JSONArray) json_block.get("items");
        if (json_items != null) {
            for (Object obj : json_items) {
                String img = (String) obj;
                Long imgId = Long.parseLong(img);
                for (Item i : itemsDB) {
                    if (i.getId() == imgId) {
                        items.add(i);//from  ww w . j  ava  2s  .c om
                    }
                }
            }
        }
        block.setItems(items);
        blocks.add(block);
    }
    return blocks;
}

From source file:matrix.CreateTextMatrix.java

public void textMatrix()
        throws UnsupportedEncodingException, FileNotFoundException, IOException, ParseException {

    CosSim cossim = new CosSim();
    JSONParser jParser = new JSONParser();
    BufferedReader in = new BufferedReader(new InputStreamReader(
            new FileInputStream("/Users/nSabri/Desktop/tweetMatris/userTweets.json"), "ISO-8859-9"));
    JSONArray a = (JSONArray) jParser.parse(in);
    File fout = new File("/Users/nSabri/Desktop/tweetMatris.csv");
    FileOutputStream fos = new FileOutputStream(fout);
    BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(fos));

    for (int i = 0; i < 1000; i++) {

        for (int j = 0; j < 1000; j++) {

            JSONObject tweet1 = (JSONObject) a.get(i);
            JSONObject tweet2 = (JSONObject) a.get(j);
            String tweetText1 = tweet1.get("tweets").toString();
            String tweetText2 = tweet2.get("tweets").toString();

            double CosSimValue = cossim.Cosine_Similarity_Score(tweetText1, tweetText2);
            CosSimValue = Double.parseDouble(new DecimalFormat("##.###").format(CosSimValue));
            bw.write(Double.toString(CosSimValue) + ", ");

        }/*from  www  . j av  a  2s. c  om*/
        bw.newLine();
    }
    bw.close();

}

From source file:matrix.CreateUrlMatrix.java

public void urlMatrix()
        throws UnsupportedEncodingException, FileNotFoundException, IOException, ParseException {

    CosSim cossim = new CosSim();
    JSONParser jParser = new JSONParser();
    BufferedReader in = new BufferedReader(new InputStreamReader(
            new FileInputStream("/Users/nSabri/Desktop/tweetMatris/userTweetsUrls.json"), "ISO-8859-9"));
    JSONArray a = (JSONArray) jParser.parse(in);
    File fout = new File("/Users/nSabri/Desktop/urlMatris.csv");
    FileOutputStream fos = new FileOutputStream(fout);
    BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(fos));

    for (int i = 0; i < a.size(); i++) {

        for (int j = 0; j < a.size(); j++) {

            JSONObject tweet1 = (JSONObject) a.get(i);
            JSONObject tweet2 = (JSONObject) a.get(j);
            String tweetUrl1 = tweet1.get("title").toString() + tweet1.get("meta").toString();
            System.out.println(tweetUrl1);
            String tweetUrl2 = tweet2.get("title").toString() + tweet1.get("meta").toString();
            System.out.println(tweetUrl2);

            double CosSimValue = cossim.Cosine_Similarity_Score(tweetUrl1, tweetUrl2);
            CosSimValue = Double.parseDouble(new DecimalFormat("##.###").format(CosSimValue));
            bw.write(Double.toString(CosSimValue) + ", ");

        }//ww  w  .j a va2s  .co m
        bw.newLine();
    }
    bw.close();

}

From source file:com.AandC.GemsCraft.Configuration.ConfigKey.java

public static int getPort() {
    try {/* w  w w.  j  av a2 s. com*/
        JSONParser parser = new JSONParser();
        Object obj = parser.parse(new FileReader("/sdcard/GemsCraft/config.json"));
        JSONObject jsonObject = (JSONObject) obj;
        port = jsonObject.get("Port");
    } catch (Exception e) {
        e.printStackTrace();
    }
    return port;
}

From source file:h.scratchpads.list.json.JSONReader.java

private static JSONArray readJSON(String jsonFileUrl, LogFile logFile) {
    String info;//from ww  w. jav a  2s .  co m
    String jsonStr = URLReader.readFromUrl(jsonFileUrl, logFile);
    JSONArray arrayJSON = null;
    if (!HkStrings.isNullOrEmptyString(jsonStr)) {
        JSONParser parser = new JSONParser();
        Object obj = null;
        try {
            obj = parser.parse(jsonStr);
            arrayJSON = (JSONArray) obj;
        } catch (ParseException ex) {
            info = "Unable to parse JSON file at the URL: \n" + jsonFileUrl + "\n";
            info = info + "Exception: " + ex.getMessage();
            logFile.write("\n!!!\n" + info + "\n!!!\n");
            System.out.println(info);
        }
    } else {
        info = "No data from JSON file at URL: \n" + jsonFileUrl;
        logFile.write("\n!!!\n" + info + "\n!!!\n");
        System.out.println(info);
    }
    return arrayJSON;
}

From source file:client.InterfaceSalon.java

/**
 * Creates new form InterfaceSalon/*from   w w w .ja va 2  s .  c  o  m*/
 * @param liste
 * @param client
 */
public InterfaceSalon(String liste, Client client) {
    initComponents();
    this.c = client;
    this.lobbyListJSON = new JSONArray();
    parser = new JSONParser();

    setLobbyList(liste);
}

From source file:capabilities.Display.java

@Override
public String adaptRole(String dbResult) throws Exception {
    // Variveis retornadas do WURFL em JSON
    int resolution_width;
    int resolution_height;
    int columns;/*from  www.  j av a2  s .  c  o m*/
    int rows;
    int physical_screen_width;
    int physical_screen_height;
    String dual_orientation;

    // Conversao do JSON de entrada para as variaveis respectivas
    JSONObject capabilities;
    JSONParser parser = new JSONParser();
    capabilities = (JSONObject) parser.parse(dbResult);
    //JSONObject capabilities = (JSONObject) my_obj.get("capabilities");
    System.out.println("\t" + capabilities);

    resolution_width = Integer.parseInt((String) capabilities.get("resolution_width"));
    resolution_height = Integer.parseInt((String) capabilities.get("resolution_height"));
    columns = Integer.parseInt((String) capabilities.get("columns"));
    rows = Integer.parseInt((String) capabilities.get("rows"));
    physical_screen_width = Integer.parseInt((String) capabilities.get("physical_screen_width"));
    physical_screen_height = Integer.parseInt((String) capabilities.get("physical_screen_height"));
    dual_orientation = (String) capabilities.get("dual_orientation");

    // Criar um novo JSON e adicionar as informaes  ele.
    JSONObject virtual = new JSONObject();

    if (physical_screen_width < physical_screen_height) {
        virtual.put("orientation_preferred", "portrait");
        virtual.put("thumbs_only", "true");
    } else {
        virtual.put("orientation_preferred", "landscape");
        virtual.put("thumbs_only", "false");
    }

    // Clculo da dimenso em polegadas da diagonal do dispositivo
    double diagonal = Math.sqrt(
            physical_screen_width * physical_screen_width + physical_screen_height * physical_screen_height);
    diagonal *= 0.039370;

    if (diagonal < 4) {
        virtual.put("average_size", "small_dispositive");
    } else if (diagonal > 5.5) {
        virtual.put("average_size", "large_dispositive");
    } else {
        virtual.put("average_size", "medium_dispositive");
    }

    // Adicionar esse novo JSON ao JSON de entrada e retorn-lo
    capabilities.put("virtual", virtual);
    return capabilities.toJSONString();
}

From source file:com.wso2.mobile.mdm.services.ProcessMessage.java

public ProcessMessage(Context context, int mode, String message, String recepient) {
    // TODO Auto-generated constructor stub
    JSONParser jp = new JSONParser();
    params = new HashMap<String, String>();
    try {// w w w .j a  v a 2s .co  m

        JSONObject jobj = new JSONObject(message);
        Log.v("JSON OUTPUT : ", jobj.toString());
        params.put("code", (String) jobj.get("message"));
        if (jobj.has("data")) {
            params.put("data", ((JSONObject) jobj.get("data")).toString());
        }

        operation = new Operation(context, mode, params, recepient);
    } catch (Exception e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

}

From source file:com.serena.rlc.provider.artifactory.domain.Repository.java

public static Repository parseSingle(String options) {
    JSONParser parser = new JSONParser();
    try {/*from  w  ww.  j a v a  2 s  . co m*/
        Object parsedObject = parser.parse(options);
        Repository repository = parseSingle((JSONObject) parsedObject);
        return repository;
    } catch (ParseException e) {
        logger.error("Error while parsing input JSON - " + options, e);
    }
    return null;
}

From source file:localworker.LocalWorker.java

@SuppressWarnings("unchecked")
@Override//from  w w w . j  a  v a 2  s .  c o m
public void run() {
    JSONParser parser = new JSONParser();
    JSONObject json;
    String task_id = null;
    String task;

    try {

        while (true) {
            //waiting up to 100ms for an element to become available.
            String messageBody = jobQ.poll(100, TimeUnit.MILLISECONDS);

            if (messageBody != null) {

                json = (JSONObject) parser.parse(messageBody);

                task_id = json.get("task_id").toString();
                task = json.get("task").toString();

                Thread.sleep(Long.parseLong(task));

                JSONObject result = new JSONObject();
                result.put("task_id", task_id);
                result.put("result", "0");
                respQ.put(result.toString());

                //System.out.println(Thread.currentThread().getName()+" sleep done!");
            }
        }

    } catch (Exception e) {
        JSONObject result = new JSONObject();
        result.put("task_id", task_id);
        result.put("result", "1");
        try {
            respQ.put(result.toString());

        } catch (InterruptedException e1) {
            e1.printStackTrace();
        }
    }
}