Example usage for org.json.simple JSONObject get

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

Introduction

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

Prototype

V get(Object key);

Source Link

Document

Returns the value to which the specified key is mapped, or null if this map contains no mapping for the key.

Usage

From source file:at.ac.tuwien.dsg.rSybl.planningEngine.staticData.ActionEffectsCassandraCluster.java

public static HashMap<String, List<ActionEffect>> getActionEffects() {
    if (actionEffects.isEmpty()) {
        actionEffects = new HashMap<String, List<ActionEffect>>();

        JSONParser parser = new JSONParser();

        try {//  w ww. ja v  a  2  s  .  com
            InputStream inputStream = Configuration.class.getClassLoader()
                    .getResourceAsStream(Configuration.getEffectsPath());
            Object obj = parser.parse(new InputStreamReader(inputStream));

            JSONObject jsonObject = (JSONObject) obj;

            for (Object actionName : jsonObject.keySet()) {
                ActionEffect actionEffect = new ActionEffect();
                String myaction = (String) actionName;
                actionEffect.setActionType((String) actionName);
                actionEffect.setActionName(myaction);
                JSONObject object = (JSONObject) jsonObject.get(myaction);
                List<ActionEffect> l = new ArrayList<ActionEffect>();
                for (Object actions : object.keySet()) {

                    JSONObject scaleinDescription = (JSONObject) object.get(actions);
                    String targetUnit = (String) scaleinDescription.get("targetUnit");
                    actionEffect.setTargetedEntityID(targetUnit);

                    JSONObject effects = (JSONObject) scaleinDescription.get("effects");

                    for (Object effectPerUnit : effects.keySet()) {
                        //System.out.println(effects.toString());
                        String affectedUnit = (String) effectPerUnit;
                        JSONObject metriceffects = (JSONObject) effects.get(affectedUnit);
                        for (Object metric : metriceffects.keySet()) {
                            String metricName = (String) metric;
                            try {
                                actionEffect.setActionEffectForMetric(metricName,
                                        (Double) metriceffects.get(metricName), affectedUnit);

                                //System.out.println("metricName="+metricName+" metric effect="+metriceffects.get(metricName)+"Affected unit="+affectedUnit);

                            } catch (Exception e) {

                                actionEffect.setActionEffectForMetric(metricName,
                                        ((Long) metriceffects.get(metricName) + 0.0), affectedUnit);

                                //   System.out.println("metricName="+metricName+" metric effect="+metriceffects.get(metricName)+"Affected unit="+affectedUnit);

                            }
                        }
                    }
                }

                l.add(actionEffect);
                actionEffects.put(actionEffect.getTargetedEntityID(), l);
            }

        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (ParseException e) {
            e.printStackTrace();
        }
    }
    return actionEffects;
}

From source file:at.ac.tuwien.dsg.rSybl.planningEngine.staticData.ActionEffects.java

public static HashMap<String, List<ActionEffect>> getActionConditionalEffects() {
    if (applicationSpecificActionEffects.isEmpty() && defaultActionEffects.isEmpty()) {
        PlanningLogger.logger.info("~~~~~~~~~~Action effects is empty, reading the effects ! ");
        JSONParser parser = new JSONParser();

        try {//w  w w . ja  va 2 s  .  c om
            InputStream inputStream = Configuration.class.getClassLoader()
                    .getResourceAsStream(Configuration.getEffectsPath());
            Object obj = parser.parse(new InputStreamReader(inputStream));

            JSONObject jsonObject = (JSONObject) obj;

            for (Object actionName : jsonObject.keySet()) {

                String myaction = (String) actionName;

                JSONObject object = (JSONObject) jsonObject.get(myaction);

                for (Object actions : object.keySet()) {

                    ActionEffect actionEffect = new ActionEffect();
                    actionEffect.setActionType((String) myaction);
                    actionEffect.setActionName((String) actions);
                    JSONObject scaleinDescription = (JSONObject) object.get(actions);
                    if (scaleinDescription.containsKey("conditions")) {
                        JSONArray conditions = (JSONArray) jsonObject.get("conditions");
                        for (int i = 0; i < conditions.size(); i++) {
                            actionEffect.addCondition((String) conditions.get(i));
                        }
                    }
                    String targetUnit = (String) scaleinDescription.get("targetUnit");
                    actionEffect.setTargetedEntityID(targetUnit);

                    JSONObject effects = (JSONObject) scaleinDescription.get("effects");

                    for (Object effectPerUnit : effects.keySet()) {
                        //System.out.println(effects.toString());
                        String affectedUnit = (String) effectPerUnit;
                        JSONObject metriceffects = (JSONObject) effects.get(affectedUnit);
                        for (Object metric : metriceffects.keySet()) {
                            String metricName = (String) metric;
                            try {
                                actionEffect.setActionEffectForMetric(metricName,
                                        (Double) metriceffects.get(metricName), affectedUnit);
                            } catch (Exception e) {
                                actionEffect.setActionEffectForMetric(metricName,
                                        ((Long) metriceffects.get(metricName)).doubleValue(), affectedUnit);
                            }
                        }

                    }

                    if (applicationSpecificActionEffects.get(actionEffect.getTargetedEntityID()) == null) {
                        List<ActionEffect> l = new ArrayList<ActionEffect>();
                        l.add(actionEffect);
                        applicationSpecificActionEffects.put(actionEffect.getTargetedEntityID(), l);

                    } else {
                        applicationSpecificActionEffects.get(actionEffect.getTargetedEntityID())
                                .add(actionEffect);
                    }
                }

            }

        } catch (Exception e) {

            PlanningLogger.logger.info("~~~~~~~~~~Retrying reading the effects  ");
            parser = new JSONParser();

            try {
                InputStream inputStream = Configuration.class.getClassLoader()
                        .getResourceAsStream(Configuration.getEffectsPath());
                Object obj = parser.parse(new InputStreamReader(inputStream));

                JSONObject jsonObject = (JSONObject) obj;

                for (Object actionName : jsonObject.keySet()) {

                    String myaction = (String) actionName;
                    ActionEffect actionEffect = new ActionEffect();
                    actionEffect.setActionType((String) myaction);
                    actionEffect.setActionName((String) myaction);

                    JSONObject object = (JSONObject) jsonObject.get(myaction);
                    JSONObject metrics = (JSONObject) object.get("effects");
                    for (Object me : metrics.keySet()) {
                        String metric = (String) me;
                        Double metricEffect = (Double) metrics.get(metric);
                        actionEffect.setActionEffectForMetric(metric, metricEffect, "");

                    }
                    defaultActionEffects.put(myaction, actionEffect);

                }

            } catch (Exception ex) {
                PlanningLogger.logger
                        .error("Error when reading the effects!!!!!!!!!!!!!!!!!!" + ex.getMessage());
            }

        }
    }
    return applicationSpecificActionEffects;

}

From source file:mas.MAS_TOP_PAPERS.java

public static String getConferenceName(int id) {

    String url = "https://api.datamarket.azure.com/MRC/MicrosoftAcademic/v2/Conference?$filter=ID%20eq%20" + id
            + "&$format=json";
    while (true) {
        try {//w  w  w.j av a  2 s  .  c  o  m
            StringBuilder csv_str = new StringBuilder();
            final String json = getData2(url, 0);
            JSONParser parser = new JSONParser();
            JSONObject jsonObj = (JSONObject) parser.parse(json);
            final JSONObject dObj = (JSONObject) jsonObj.get("d");
            final JSONArray results = (JSONArray) dObj.get("results");
            if (results.size() == 0) {
                System.out.println("results is Empty, break.");
                break;
            } else {
                //                    System.out.println("Conf: results# = " + results.size());
                for (Object conf : results) {
                    JSONObject confObj = (JSONObject) conf;
                    String shortName = normalized((String) confObj.get("ShortName"));
                    if (!shortName.equals("")) {
                        return shortName;
                    } else {
                        String fullName = normalized((String) confObj.get("FullName"));
                        return fullName;
                    }
                }
            }
            //                System.out.println("json= " + jsonObj);
        } catch (ParseException ex) {
            System.out.println(ex.getMessage() + " Cause: " + ex.getCause());
            Logger.getLogger(MAS_TOP_PAPERS.class.getName()).log(Level.SEVERE, null, ex);
            try {
                Thread.sleep(5000L);
            } catch (InterruptedException ex1) {
                Logger.getLogger(MAS_TOP_PAPERS.class.getName()).log(Level.SEVERE, null, ex1);
            }
        }
    }
    return null;
}

From source file:mas.MAS_TOP_PAPERS.java

public static void extractConference(int start) {
    String file_prefix = "conferences";
    String csv_file_path = "data/" + file_prefix + ".csv";
    String json_dump_file_path = "data/" + file_prefix + "_dump.json";
    String url = "https://api.datamarket.azure.com/MRC/MicrosoftAcademic/v2/Conference?";
    url += "$format=json";
    while (true) {
        try {/*from w  w w .  ja  v  a2  s.co m*/
            StringBuilder csv_str = new StringBuilder();
            final String json = getData2(url, start);
            JSONParser parser = new JSONParser();
            JSONObject jsonObj = (JSONObject) parser.parse(json);
            final JSONObject dObj = (JSONObject) jsonObj.get("d");
            final JSONArray results = (JSONArray) dObj.get("results");
            if (results.size() == 0) {
                System.out.println("results is Empty, break.");
                break;
            } else {
                System.out.println("Conference: start = " + start + " results# = " + results.size());
                for (Object paper : results) {
                    JSONObject paperObj = (JSONObject) paper;
                    Long id = (Long) paperObj.get("ID");
                    String shortName = normalized((String) paperObj.get("ShortName"));
                    String fullName = normalized((String) paperObj.get("FullName"));
                    String homepage = normalized((String) paperObj.get("Homepage"));
                    csv_str.append(id).append(SEPERATOR).append(shortName).append(SEPERATOR).append(fullName)
                            .append(SEPERATOR).append(homepage).append(NEWLINE);
                }
                IOUtils.writeDataIntoFile(json + "\n", json_dump_file_path);
                IOUtils.writeDataIntoFile(csv_str.toString(), csv_file_path);
                start += 100;
                Thread.sleep(300L);
            }
            //                System.out.println("json= " + jsonObj);
        } catch (ParseException ex) {
            Logger.getLogger(MAS_TOP_PAPERS.class.getName()).log(Level.SEVERE, null, ex);
        } catch (InterruptedException ex) {
            Logger.getLogger(MAS_TOP_PAPERS.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
}

From source file:mas.MAS_TOP_PAPERS.java

public static void extractJournal(int start) {
    String file_prefix = "journals";
    String csv_file_path = "data/" + file_prefix + ".csv";
    String json_dump_file_path = "data/" + file_prefix + "_dump.json";
    String url = "https://api.datamarket.azure.com/MRC/MicrosoftAcademic/v2/Journal?";
    url += "$format=json";
    while (true) {
        try {/*from  w  ww  .j a va2  s.  co  m*/
            StringBuilder csv_str = new StringBuilder();
            final String json = getData2(url, start);
            JSONParser parser = new JSONParser();
            JSONObject jsonObj = (JSONObject) parser.parse(json);
            final JSONObject dObj = (JSONObject) jsonObj.get("d");
            final JSONArray results = (JSONArray) dObj.get("results");
            if (results.size() == 0) {
                System.out.println("results is Empty, break.");
                break;
            } else {
                System.out.println("Journals: start = " + start + " results# = " + results.size());
                for (Object paper : results) {
                    JSONObject paperObj = (JSONObject) paper;
                    Long id = (Long) paperObj.get("ID");
                    String shortName = normalized((String) paperObj.get("ShortName"));
                    String fullName = normalized((String) paperObj.get("FullName"));
                    String homepage = normalized((String) paperObj.get("Homepage"));
                    csv_str.append(id).append(SEPERATOR).append(shortName).append(SEPERATOR).append(fullName)
                            .append(SEPERATOR).append(homepage).append(NEWLINE);
                }
                IOUtils.writeDataIntoFile(json + "\n", json_dump_file_path);
                IOUtils.writeDataIntoFile(csv_str.toString(), csv_file_path);
                start += 100;
                Thread.sleep(300L);
            }
            //                System.out.println("json= " + jsonObj);
        } catch (ParseException ex) {
            Logger.getLogger(MAS_TOP_PAPERS.class.getName()).log(Level.SEVERE, null, ex);
        } catch (InterruptedException ex) {
            Logger.getLogger(MAS_TOP_PAPERS.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
}

From source file:mas.MAS_TOP_PAPERS.java

public static void extractAuthor(int start) {
    String file_prefix = "authors";
    String csv_file_path = "data/" + file_prefix + ".csv";
    String json_dump_file_path = "data/" + file_prefix + "_dump.json";
    String url = "https://api.datamarket.azure.com/MRC/MicrosoftAcademic/v2/Author?$select=ID,Name,NativeName,Affiliation,AffiliationID,Version";
    url += "&$format=json";
    while (true) {
        try {/*from  w  w  w  .j a  va 2s  .c o  m*/
            StringBuilder csv_str = new StringBuilder();
            final String json = getData2(url, start);
            JSONParser parser = new JSONParser();
            JSONObject jsonObj = (JSONObject) parser.parse(json);
            final JSONObject dObj = (JSONObject) jsonObj.get("d");
            final JSONArray results = (JSONArray) dObj.get("results");
            if (results.size() == 0) {
                System.out.println("results is Empty, break.");
                break;
            } else {
                System.out.println("Author: start = " + start + " results# = " + results.size());
                for (Object paper : results) {
                    JSONObject paperObj = (JSONObject) paper;
                    Long id = (Long) paperObj.get("ID");
                    String name = normalized((String) paperObj.get("Name"));
                    String nativeName = normalized((String) paperObj.get("NativeName"));
                    String affiliation = normalized((String) paperObj.get("Affiliation"));
                    Long affiliationID = (Long) paperObj.get("AffiliationID");
                    Long version = (Long) paperObj.get("Version");
                    csv_str.append(id).append(SEPERATOR).append(name).append(SEPERATOR).append(nativeName)
                            .append(SEPERATOR).append(affiliation).append(SEPERATOR).append(affiliationID)
                            .append(SEPERATOR).append(version).append(NEWLINE);
                }
                IOUtils.writeDataIntoFile(json + "\n", json_dump_file_path);
                IOUtils.writeDataIntoFile(csv_str.toString(), csv_file_path);
                start += 100;
                Thread.sleep(300L);
            }
            //                System.out.println("json= " + jsonObj);
        } catch (ParseException ex) {
            Logger.getLogger(MAS_TOP_PAPERS.class.getName()).log(Level.SEVERE, null, ex);
        } catch (InterruptedException ex) {
            Logger.getLogger(MAS_TOP_PAPERS.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
}

From source file:mml.handler.get.MMLGetMMLHandler.java

/**
 * Get a dialect/*w w w.  jav  a 2  s .c  o  m*/
 * @param docid the docid of the dialect
 * @param version1 the version id that may specify a dialect variant
 * @return an Element (div) containing the content
 */
public static String getDialect(String docid, String version1) throws MMLTestException {
    try {
        Connection conn = Connector.getConnection();
        String path = docid;
        if (version1 != null && !version1.equals("/base"))
            path += version1;
        String dialect = conn.getFromDb(Database.DIALECTS, path);
        if (dialect != null) {
            JSONObject jObj = (JSONObject) JSONValue.parse(dialect);
            dialect = (String) jObj.get(JSONKeys.BODY);
        } else {
            while (path.length() > 0 && dialect == null) {
                path = Utils.chomp(path);
                String bson = conn.getFromDb(Database.DIALECTS, path);
                if (bson != null) {
                    JSONObject jObj = (JSONObject) JSONValue.parse(bson);
                    dialect = (String) jObj.get(JSONKeys.BODY);
                }
            }
        }
        if (dialect == null)
            throw new MMLException("No dialect for " + path + " found");
        else
            return dialect;
    } catch (Exception e) {
        throw new MMLTestException(e);
    }
}

From source file:mas.MAS_TOP_PAPERS.java

public static void extractAffilition(int start) {
    String file_prefix = "affilitions";
    String csv_file_path = "data/" + file_prefix + ".csv";
    String json_dump_file_path = "data/" + file_prefix + "_dump.json";
    String url = "https://api.datamarket.azure.com/MRC/MicrosoftAcademic/v2/Affiliation?";
    url += "$format=json";
    while (true) {
        try {//from   www . j  av a 2  s.  c om
            StringBuilder csv_str = new StringBuilder();
            final String json = getData2(url, start);
            JSONParser parser = new JSONParser();
            JSONObject jsonObj = (JSONObject) parser.parse(json);
            final JSONObject dObj = (JSONObject) jsonObj.get("d");
            final JSONArray results = (JSONArray) dObj.get("results");
            if (results.size() == 0) {
                System.out.println("results is Empty, break.");
                break;
            } else {
                System.out.println("Affilition: start = " + start + " results# = " + results.size());
                for (Object paper : results) {
                    JSONObject paperObj = (JSONObject) paper;
                    Long id = (Long) paperObj.get("ID");
                    String officialName = normalized((String) paperObj.get("OfficialName"));
                    String displayName = normalized((String) paperObj.get("DisplayName"));
                    String nativeName = normalized((String) paperObj.get("NativeName"));
                    Long parentID = (Long) paperObj.get("ParentID");
                    String homepage = normalized((String) paperObj.get("Homepage"));
                    String shortName = normalized((String) paperObj.get("ShortName"));
                    Long type = (Long) paperObj.get("Type");
                    csv_str.append(id).append(SEPERATOR).append(officialName).append(SEPERATOR)
                            .append(displayName).append(SEPERATOR).append(nativeName).append(SEPERATOR)
                            .append(parentID).append(SEPERATOR).append(homepage).append(SEPERATOR)
                            .append(shortName).append(SEPERATOR).append(type).append(NEWLINE);
                }
                IOUtils.writeDataIntoFile(json + "\n", json_dump_file_path);
                IOUtils.writeDataIntoFile(csv_str.toString(), csv_file_path);
                start += 100;
                Thread.sleep(300L);
            }
            //                System.out.println("json= " + jsonObj);
        } catch (ParseException ex) {
            Logger.getLogger(MAS_TOP_PAPERS.class.getName()).log(Level.SEVERE, null, ex);
        } catch (InterruptedException ex) {
            Logger.getLogger(MAS_TOP_PAPERS.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
}

From source file:mas.MAS_TOP_PAPERS.java

public static void extractPaper_Author(int start) {
    String file_prefix = "paper_authors";
    String csv_file_path = "data/" + file_prefix + ".csv";
    String json_dump_file_path = "data/" + file_prefix + "_dump.json";
    String url = "https://api.datamarket.azure.com/MRC/MicrosoftAcademic/v2/Paper_Author?";
    url += "$format=json";
    while (true) {
        try {//www . ja v  a 2  s .co m
            StringBuilder csv_str = new StringBuilder();
            final String json = getData2(url, start);
            JSONParser parser = new JSONParser();
            JSONObject jsonObj = (JSONObject) parser.parse(json);
            final JSONObject dObj = (JSONObject) jsonObj.get("d");
            final JSONArray results = (JSONArray) dObj.get("results");
            if (results.size() == 0) {
                System.out.println("results is Empty, break.");
                break;
            } else {
                System.out.println("Paper_Author: start = " + start + " results# = " + results.size());
                for (Object paper : results) {
                    JSONObject paperObj = (JSONObject) paper;
                    Long paperID = (Long) paperObj.get("PaperID");

                    Long seqID = (Long) paperObj.get("SeqID");

                    Long authorID = (Long) paperObj.get("authorID");
                    String name = normalized((String) paperObj.get("Name"));
                    String affiliation = normalized((String) paperObj.get("Affiliation"));

                    Long affiliationID = (Long) paperObj.get("AffiliationID");
                    csv_str.append(paperID).append(SEPERATOR).append(seqID).append(SEPERATOR).append(authorID)
                            .append(SEPERATOR).append(name).append(SEPERATOR).append(affiliation)
                            .append(SEPERATOR).append(affiliationID).append(NEWLINE);
                }
                IOUtils.writeDataIntoFile(json + "\n", json_dump_file_path);
                IOUtils.writeDataIntoFile(csv_str.toString(), csv_file_path);
                start += 100;
                Thread.sleep(300L);
            }
            //                System.out.println("json= " + jsonObj);
        } catch (ParseException ex) {
            Logger.getLogger(MAS_TOP_PAPERS.class.getName()).log(Level.SEVERE, null, ex);
        } catch (InterruptedException ex) {
            Logger.getLogger(MAS_TOP_PAPERS.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
}

From source file:mas.MAS_VLDB.java

public static String getConferenceName(int id) {

    String url = "https://api.datamarket.azure.com/MRC/MicrosoftAcademic/v2/Conference?$filter=ID%20eq%20" + id
            + "&$format=json";
    while (true) {
        try {//from  ww w  . j  ava  2  s .  c  o m
            StringBuilder csv_str = new StringBuilder();
            final String json = getData2(url, 0);
            JSONParser parser = new JSONParser();
            JSONObject jsonObj = (JSONObject) parser.parse(json);
            final JSONObject dObj = (JSONObject) jsonObj.get("d");
            final JSONArray results = (JSONArray) dObj.get("results");
            if (results.size() == 0) {
                System.out.println("results is Empty, break.");
                break;
            } else {
                //                    System.out.println("Conf: results# = " + results.size());
                for (Object conf : results) {
                    JSONObject confObj = (JSONObject) conf;
                    String shortName = normalized((String) confObj.get("ShortName"));
                    if (!shortName.equals("")) {
                        return shortName;
                    } else {
                        String fullName = normalized((String) confObj.get("FullName"));
                        return fullName;
                    }
                }
            }
            //                System.out.println("json= " + jsonObj);
        } catch (ParseException ex) {
            System.out.println(ex.getMessage() + " Cause: " + ex.getCause());
            Logger.getLogger(MAS_VLDB.class.getName()).log(Level.SEVERE, null, ex);
            try {
                Thread.sleep(5000L);
            } catch (InterruptedException ex1) {
                Logger.getLogger(MAS_VLDB.class.getName()).log(Level.SEVERE, null, ex1);
            }
        }
    }
    return null;
}