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:mas.MAS_TOP_PAPERS.java

public static void extractPapers(int start, int conf) {
    String csv_file_path = "data/papers_200_conf.csv";
    String json_dump_file_path = "data/papers_200_conf_dump.json";
    String url = "https://api.datamarket.azure.com/MRC/MicrosoftAcademic/v2/Paper?$select=ID,Title,Year,ConfID,JourID&$filter=Year%20ge%202000%20and%20ConfID%20eq%20"
            + conf + "&$format=json";
    // String url = "https://api.datamarket.azure.com/MRC/MicrosoftAcademic/v2/Paper?$select=ID,DocType,Title,Year,ConfID,JourID&$filter=Year%20gt%202001&$format=json";
    while (true) {
        IOUtils.writeDataIntoFile(start + "", paper_last, false);
        try {//w  ww  .j  a  v  a  2s  .com
            StringBuilder csv_str = new StringBuilder();
            final String json = getData2(url, start);
            //                System.out.println("json=" + json);
            if (json == null) {
                System.out.println("json is null. skip. old start=" + start);
                start += 100;
                Thread.sleep(10000L);
                continue;
            }
            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: start = " + start + " results# = " + results.size());
                for (Object paper : results) {
                    JSONObject paperObj = (JSONObject) paper;
                    //                        Long docType = (Long) paperObj.get("DocType");
                    //                        Long year = (Long) paperObj.get("Year");
                    //                        Long jourID = (Long) paperObj.get("JourID");
                    Long confID = (Long) paperObj.get("ConfID");
                    Long id = (Long) paperObj.get("ID");
                    String title = (String) paperObj.get("Title");
                    title = normalized(title);
                    csv_str.append(id).append(SEPERATOR).append(confID).append(SEPERATOR).append(title)
                            .append(NEWLINE);
                }
                IOUtils.writeDataIntoFile(json + "\n", json_dump_file_path);
                IOUtils.writeDataIntoFile(csv_str.toString(), csv_file_path);
                start += 100;
                Thread.sleep(250L);
            }
            //                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);
            start += 100;
            try {
                Thread.sleep(1000L);
            } catch (InterruptedException ex1) {
                Logger.getLogger(MAS_TOP_PAPERS.class.getName()).log(Level.SEVERE, null, ex1);
            }

        } catch (InterruptedException ex) {
            System.out.println(ex.getMessage() + " Cause: " + ex.getCause());
            Logger.getLogger(MAS_TOP_PAPERS.class.getName()).log(Level.SEVERE, null, ex);
            start += 100;
            try {
                Thread.sleep(1000L);
            } catch (InterruptedException ex1) {
                Logger.getLogger(MAS_TOP_PAPERS.class.getName()).log(Level.SEVERE, null, ex1);
            }

        }
    }
}

From source file:mas.MAS_VLDB.java

public static void extractCitation(int start, String papers_filter, String csv_file, String json_file) {
    String csv_file_path = csv_file;
    String json_dump_file_path = json_file;
    String url = "https://api.datamarket.azure.com/MRC/MicrosoftAcademic/v2/Paper_Ref?$select=SrcID,DstID&$filter="
            + papers_filter + "&$format=json";
    while (true) {
        IOUtils.writeDataIntoFile(start + "", paper_last, false);
        try {/*from   w  ww .  j  av  a  2  s.c  om*/
            StringBuilder csv_str = new StringBuilder();
            final String json = getData2(url, start);
            //                System.out.println("json=" + json);
            if (json == null) {
                System.out.println("json is null. skip. old start=" + start);
                start += 100;
                Thread.sleep(1000L);
                continue;
            }
            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: start = " + start + " results# = " + results.size());
                for (Object cite : results) {
                    JSONObject citeObj = (JSONObject) cite;
                    //                        Long docType = (Long) paperObj.get("DocType");
                    //                        Long year = (Long) paperObj.get("Year");
                    //                        Long jourID = (Long) paperObj.get("JourID");
                    Long srcID = (Long) citeObj.get("SrcID");
                    Long dstID = (Long) citeObj.get("DstID");
                    //                        String title = (String) citeObj.get("Title");
                    //                        title = normalized(title);
                    csv_str.append(srcID).append(SEPERATOR).append(dstID).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) {
            System.out.println(ex.getMessage() + " Cause: " + ex.getCause());
            Logger.getLogger(MAS_VLDB.class.getName()).log(Level.SEVERE, null, ex);
            start += 100;
            try {
                Thread.sleep(5000L);
            } catch (InterruptedException ex1) {
                Logger.getLogger(MAS_VLDB.class.getName()).log(Level.SEVERE, null, ex1);
            }

        } catch (InterruptedException ex) {
            System.out.println(ex.getMessage() + " Cause: " + ex.getCause());
            Logger.getLogger(MAS_VLDB.class.getName()).log(Level.SEVERE, null, ex);
            start += 100;
            try {
                Thread.sleep(5000L);
            } catch (InterruptedException ex1) {
                Logger.getLogger(MAS_VLDB.class.getName()).log(Level.SEVERE, null, ex1);
            }

        }
    }
}

From source file:mas.MAS_VLDB.java

public static void extractPapers(int start, int conf) {
    String csv_file_path = "data/papers_200_conf.csv";
    String json_dump_file_path = "data/papers_200_conf_dump.json";
    String url = "https://api.datamarket.azure.com/MRC/MicrosoftAcademic/v2/Paper?$select=ID,Title,Year,ConfID,JourID&$filter=Year%20ge%202000%20and%20ConfID%20eq%20"
            + conf + "&$format=json";
    // String url = "https://api.datamarket.azure.com/MRC/MicrosoftAcademic/v2/Paper?$select=ID,DocType,Title,Year,ConfID,JourID&$filter=Year%20gt%202001&$format=json";
    while (true) {
        IOUtils.writeDataIntoFile(start + "", paper_last, false);
        try {//from w ww. j  a  v  a 2  s .  c  om
            StringBuilder csv_str = new StringBuilder();
            final String json = getData2(url, start);
            //                System.out.println("json=" + json);
            if (json == null) {
                System.out.println("json is null. skip. old start=" + start);
                start += 100;
                Thread.sleep(10000L);
                continue;
            }
            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: start = " + start + " results# = " + results.size());
                for (Object paper : results) {
                    JSONObject paperObj = (JSONObject) paper;
                    //                        Long docType = (Long) paperObj.get("DocType");
                    //                        Long year = (Long) paperObj.get("Year");
                    //                        Long jourID = (Long) paperObj.get("JourID");
                    Long confID = (Long) paperObj.get("ConfID");
                    Long id = (Long) paperObj.get("ID");
                    String title = (String) paperObj.get("Title");
                    title = normalized(title);
                    csv_str.append(id).append(SEPERATOR).append(confID).append(SEPERATOR).append(title)
                            .append(NEWLINE);
                }
                IOUtils.writeDataIntoFile(json + "\n", json_dump_file_path);
                IOUtils.writeDataIntoFile(csv_str.toString(), csv_file_path);
                start += 100;
                Thread.sleep(250L);
            }
            //                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);
            start += 100;
            try {
                Thread.sleep(1000L);
            } catch (InterruptedException ex1) {
                Logger.getLogger(MAS_VLDB.class.getName()).log(Level.SEVERE, null, ex1);
            }

        } catch (InterruptedException ex) {
            System.out.println(ex.getMessage() + " Cause: " + ex.getCause());
            Logger.getLogger(MAS_VLDB.class.getName()).log(Level.SEVERE, null, ex);
            start += 100;
            try {
                Thread.sleep(1000L);
            } catch (InterruptedException ex1) {
                Logger.getLogger(MAS_VLDB.class.getName()).log(Level.SEVERE, null, ex1);
            }

        }
    }
}

From source file:org.openimaj.image.dataset.BingImageDataset.java

private static List<ImageDataSourceResponse> performSinglePageQuery(ImageDataSourceQuery query) {
    final HttpClient httpclient = HttpClients.createDefault();

    try {/*from  w  ww.ja v  a 2  s. c  o m*/
        final URI uri = query.buildURI();
        final HttpGet request = new HttpGet(uri);
        request.setHeader("Ocp-Apim-Subscription-Key", query.accountKey);

        final HttpResponse response = httpclient.execute(request);

        if (response.getStatusLine().getStatusCode() == HttpStatus.SC_UNAUTHORIZED) {
            throw new IOException(
                    "HTTP ERROR 401: Unauthorized Recieved. " + "You probably have the incorrect API Key");
        }
        final HttpEntity entity = response.getEntity();

        if (entity != null) {
            try {
                final JSONParser parser = new JSONParser();
                final JSONObject o = (JSONObject) parser.parse(EntityUtils.toString(entity));

                final JSONArray jresults = ((JSONArray) o.get("value"));
                final List<ImageDataSourceResponse> results = new ArrayList<>(jresults.size());

                for (final Object jro : jresults) {
                    results.add(new ImageDataSourceResponse((JSONObject) jro));
                }

                return results;
            } catch (final Exception e) {
                e.printStackTrace();
            }
        }
    } catch (final IOException e) {
        e.printStackTrace();
    } catch (final URISyntaxException e) {
        e.printStackTrace();
    }

    return null;
}

From source file:de.dailab.plistacontest.client.RecommenderItem.java

/**
 * Parse the ORP json Messages.//from   w  w  w .j ava 2 s. c  om
 * 
 * @param _jsonMessageBody
 * @return the parsed values encapsulated in a map; null if an error has
 *         been detected.
 */
public static RecommenderItem parseRecommendationRequest(String _jsonMessageBody) {

    try {
        final JSONObject jsonObj = (JSONObject) JSONValue.parse(_jsonMessageBody);

        // parse JSON structure to obtain "context.simple"
        JSONObject jsonObjectContext = (JSONObject) jsonObj.get("context");
        JSONObject jsonObjectContextSimple = (JSONObject) jsonObjectContext.get("simple");

        Long domainID = -3L;
        try {
            domainID = Long.valueOf(jsonObjectContextSimple.get("27").toString());
        } catch (Exception ignored) {
            try {
                domainID = Long.valueOf(jsonObjectContextSimple.get("domainId").toString());
            } catch (Exception e) {
                System.err.println("[Exception] no domainID found in " + _jsonMessageBody);
            }
        }

        Long itemID = null;
        try {
            itemID = Long.valueOf(jsonObjectContextSimple.get("25").toString());
        } catch (Exception ignored) {
            try {
                itemID = Long.valueOf(jsonObjectContextSimple.get("itemId").toString());
            } catch (Exception e) {
                System.err.println("[Exception] no itemID found in " + _jsonMessageBody);
            }
        }

        Long userID = -2L;
        try {
            userID = Long.valueOf(jsonObjectContextSimple.get("57").toString());
        } catch (Exception ignored) {
            try {
                userID = Long.valueOf(jsonObjectContextSimple.get("userId").toString());
            } catch (Exception e) {
                System.err.println("[INFO] no userID found in " + _jsonMessageBody);
            }
        }

        long timeStamp = 0;
        try {
            timeStamp = (Long) jsonObj.get("created_at") + 0L;
        } catch (Exception ignored) {
            timeStamp = (Long) jsonObj.get("timestamp");
        }

        try {
            userID = Long.valueOf(jsonObjectContextSimple.get("userId").toString());
        } catch (Exception e) {
            System.err.println("[INFO] no userID found in " + _jsonMessageBody);
        }

        Long limit = 0L;
        try {
            limit = (Long) jsonObj.get("limit");
        } catch (Exception e) {
            System.err.println("[Exception] no limit found in " + _jsonMessageBody);
        }

        RecommenderItem result = new RecommenderItem(userID, itemID, domainID, timeStamp);
        result.setNumberOfRequestedResults(limit.intValue());

        return result;
    } catch (Exception e) {
        e.printStackTrace();
    }
    return null;
}

From source file:hoot.services.controllers.job.JobResource.java

private static JSONObject processJob(String jobId, JSONObject command) throws NativeInterfaceException {
    logger.debug("processing Job: {}", jobId);
    command.put("jobId", jobId);

    String resourceName = command.get("caller").toString();
    JobFieldsValidator validator = new JobFieldsValidator(resourceName);

    Map<String, String> paramsMap = paramsToMap(command);

    List<String> missingList = new ArrayList<>();
    if (!validator.validateRequiredExists(paramsMap, missingList)) {
        logger.error("Missing following required field(s): {}", missingList);
    }/* w w  w .ja v  a2s  .c om*/

    logger.debug("calling native request Job: {}", jobId);

    return jobExecMan.exec(command);
}

From source file:fr.zcraft.zlib.tools.mojang.UUIDFetcher.java

/**
 * Fetches the original UUID of the given name.
 *
 * As example, let's say we have a Foo account with the UUID {@code 00000} renamed to Bar, and
 * another account with UUID {@code 11111} was renamed later to Foo. Calling this method with
 * {@code "Foo"} will return {@code 00000}.
 *
 * @param name The player's name./*from w ww.  j a v  a2 s. c om*/
 *
 * @return An internal object containing the current player name and his UUID.
 * @throws IOException If an exception occurs while contacting the Mojang API.
 */
static private User fetchOriginalUUID(String name) throws IOException {
    HttpURLConnection connection = getGETConnection(TIMED_PROFILE_URL + name + "?at=" + NAME_CHANGE_TIMESTAMP);

    JSONObject object;

    try {
        object = (JSONObject) readResponse(connection);
    } catch (ParseException ex) {
        throw new IOException("Invalid response from server, unable to parse received JSON : " + ex.toString());
    }

    if (object == null)
        return null;

    User user = new User();
    user.name = name;
    user.uuid = fromMojangUUID((String) object.get("id"));
    return user;
}

From source file:de.dailab.plistacontest.client.RecommenderItem.java

/**
 * parse the event notification.//from www  . j  a v  a  2s  .  c om
 * 
 * @param _jsonMessageBody
 * @return
 */
public static RecommenderItem parseEventNotification(final String _jsonMessageBody) {

    try {
        final JSONObject jsonObj = (JSONObject) JSONValue.parse(_jsonMessageBody);

        // parse JSON structure to obtain "context.simple"
        JSONObject jsonObjectContext = (JSONObject) jsonObj.get("context");
        JSONObject jsonObjectContextSimple = (JSONObject) jsonObjectContext.get("simple");

        Long domainID = -3L;
        try {
            domainID = Long.valueOf(jsonObjectContextSimple.get("27").toString());
        } catch (Exception ignored) {
            try {
                domainID = Long.valueOf(jsonObj.get("domainID").toString());
            } catch (Exception e) {
                System.err.println("[Exception] no domainID found in " + _jsonMessageBody);
            }
        }

        Long itemID = null;
        try {
            itemID = Long.valueOf(jsonObjectContextSimple.get("25").toString());
        } catch (Exception ignored) {
            try {
                itemID = Long.valueOf(jsonObj.get("itemID").toString());
            } catch (Exception e) {
                System.err.println("[Exception] no itemID found in " + _jsonMessageBody);
            }
        }

        Long userID = -2L;
        try {
            userID = Long.valueOf(jsonObjectContextSimple.get("57").toString());
        } catch (Exception ignored) {
            try {
                userID = Long.valueOf(jsonObj.get("userID").toString());
            } catch (Exception e) {
                System.err.println("[Exception] no userID found in " + _jsonMessageBody);
            }
        }

        // impressionType
        String notificationType = null;
        try {
            notificationType = jsonObj.get("type") + "";
        } catch (Exception e) {
            e.printStackTrace();
        }

        // event_type due to the idomaar data format
        String eventType = null;
        try {
            eventType = jsonObj.get("event_type") + "";
            if (!"null".equals(eventType)) {
                notificationType = eventType;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

        // list of displayed recs
        List<Long> listOfDisplayedRecs = new ArrayList<Long>(6);
        try {
            Object jsonObjectRecsTmp = jsonObj.get("recs");
            if (jsonObjectRecsTmp == null || !(jsonObjectRecsTmp instanceof JSONObject)) {
                System.err.println("[INFO] impression without recs " + jsonObj);
            } else {
                JSONObject jsonObjectRecs = (JSONObject) jsonObjectRecsTmp;
                JSONObject jsonObjectRecsInt = (JSONObject) jsonObjectRecs.get("ints");
                JSONArray array = (JSONArray) jsonObjectRecsInt.get("3");
                for (Object arrayEntry : array) {
                    listOfDisplayedRecs.add(Long.valueOf(arrayEntry + ""));
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
            System.err.println("invalid jsonObject: " + jsonObj);
        }

        long timeStamp = 0;
        try {
            timeStamp = (Long) jsonObj.get("created_at") + 0L;
        } catch (Exception ignored) {
            timeStamp = (Long) jsonObj.get("timestamp");
        }

        // create the result and return
        RecommenderItem result = new RecommenderItem(userID, itemID, domainID, timeStamp);
        result.setNotificationType(notificationType);
        result.setListOfDisplayedRecs(listOfDisplayedRecs);

        return result;

    } catch (Throwable t) {
        t.printStackTrace();
        return null;
    }
}

From source file:com.worldline.easycukes.commons.helpers.JSONHelper.java

/**
 * Returns <b>true</b> if JSON object o1 is equals to JSON object o2.
 *
 * @param o1 a {@link JSONObject} containing some JSON data
 * @param o2 another {@link JSONObject} containing some JSON data
 * @return true if the json objects are equals
 *//*from www .  ja va  2  s .c om*/
public static boolean equals(@NonNull JSONObject o1, @NonNull JSONObject o2) {
    if (o1 == o2)
        return true;
    if (o1.size() != o2.size())
        return false;
    try {
        final Iterator<Entry<String, Object>> i = o1.entrySet().iterator();
        while (i.hasNext()) {
            final Entry<String, Object> e = i.next();
            final String key = e.getKey();
            final Object value1 = e.getValue();
            final Object value2 = o2.get(key);
            if (value1 == null) {
                if (!(o2.get(key) == null && o2.containsKey(key)))
                    return false;
            } else if (value1 instanceof JSONObject) {
                if (!(value2 instanceof JSONObject))
                    return false;
                if (!equals((JSONObject) value1, (JSONObject) value2))
                    return false;
            } else if (value1 instanceof JSONArray) {

                if (!(value2 instanceof JSONArray))
                    return false;
                if (!equals((JSONArray) value1, (JSONArray) value2))
                    return false;
            } else if (!value1.equals(value2))
                return false;
        }
    } catch (final Exception unused) {
        unused.printStackTrace();
        return false;
    }
    return true;
}

From source file:com.ibm.bluemix.samples.PostgreSQLReportedErrors.java

/**
 * Establishes a connection to the database
 * //from  w ww . ja v a 2  s .c  o  m
 * @return the established connection
 * 
 * @throws Exception
 *          a custom exception thrown if no postgresql service URL was found
 */
public static Connection getConnection() throws Exception {
    Map<String, String> env = System.getenv();

    if (env.containsKey("VCAP_SERVICES")) {
        // we are running on cloud foundry, let's grab the service details from vcap_services
        JSONParser parser = new JSONParser();
        JSONObject vcap = (JSONObject) parser.parse(env.get("VCAP_SERVICES"));
        JSONObject service = null;

        // We don't know exactly what the service is called, but it will contain "postgresql"
        for (Object key : vcap.keySet()) {
            String keyStr = (String) key;
            if (keyStr.toLowerCase().contains("postgresql")) {
                service = (JSONObject) ((JSONArray) vcap.get(keyStr)).get(0);
                break;
            }
        }

        if (service != null) {
            JSONObject creds = (JSONObject) service.get("credentials");
            String name = (String) creds.get("name");
            String host = (String) creds.get("host");
            Long port = (Long) creds.get("port");
            String user = (String) creds.get("user");
            String password = (String) creds.get("password");

            String url = "jdbc:postgresql://" + host + ":" + port + "/" + name;

            return DriverManager.getConnection(url, user, password);
        }
    }
    throw new Exception(
            "No PostgreSQL service URL found. Make sure you have bound the correct services to your app.");
}