Example usage for org.json.simple JSONObject keySet

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

Introduction

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

Prototype

Set<K> keySet();

Source Link

Document

Returns a Set view of the keys contained in this map.

Usage

From source file:com.turt2live.uuid.turt2live.v1.ApiV1Service.java

@Override
public List<PlayerRecord> doBulkLookup(String... playerNames) {
    String list = combine(playerNames);
    String response = doUrlRequest(getConnectionUrl() + "/uuid/list/" + list);

    if (response != null) {
        JSONObject json = (JSONObject) JSONValue.parse(response);

        if (json.containsKey("results")) {
            JSONObject object = (JSONObject) json.get("results");
            List<PlayerRecord> records = new ArrayList<>();

            for (Object key : object.keySet()) {
                String name = key.toString();
                UUID uuid = convertUuid((String) object.get(key));

                if (uuid == null || name.equals("unknown"))
                    continue;

                PlayerRecord record = new Turt2LivePlayerRecord(uuid, name);
                records.add(record);//w  w w. java 2  s.c  o m
            }

            return records;
        }
    }

    return null;
}

From source file:functionaltests.RestSchedulerJobPaginationTest.java

private void checkRevisionJobsInfo(String url, String... expectedIds) throws Exception {
    Set<String> expected = new HashSet<>(Arrays.asList(expectedIds));
    Set<String> actual = new HashSet<>();

    JSONObject page = getRequestJSONObject(url);
    JSONObject map = (JSONObject) page.get("map");
    Assert.assertEquals(1, map.keySet().size());
    JSONArray jobs = (JSONArray) map.get(map.keySet().iterator().next());
    for (int i = 0; i < jobs.size(); i++) {
        JSONObject job = (JSONObject) jobs.get(i);
        actual.add((String) job.get("jobid"));
    }/*from   www. j a  v  a 2s . c  o m*/
    Assert.assertEquals("Unexpected result of 'revisionjobsinfo' request (" + url + ")", expected, actual);
}

From source file:net.bashtech.geobot.JSONUtil.java

public static String getRace(String channel) {
    String raceID = "";

    Set<String> entrantSet = new HashSet<String>();
    try {/*www.  j a  v a  2 s .  co m*/
        JSONParser parser = new JSONParser();
        Object obj = parser.parse(BotManager.getRemoteContent("http://api.speedrunslive.com:81/races"));

        JSONObject jsonObject = (JSONObject) obj;
        int count = Integer.parseInt((String) jsonObject.get("count"));
        JSONArray raceList = (JSONArray) jsonObject.get("races");

        for (int i = 0; i < count; i++) {
            JSONObject races = (JSONObject) raceList.get(i);
            String id = (String) races.get("id");

            JSONObject entrants = (JSONObject) races.get("entrants");
            Set<String> entrantNames = entrants.keySet();
            String entrantsString = entrants.toJSONString();
            if (entrantsString.contains(channel)) {
                raceID = id;
                entrantSet.add(channel);
                for (String s : entrantNames) {

                    JSONObject entrant = (JSONObject) entrants.get(s);
                    String entranttwitch = (String) entrant.get("twitch");
                    if (entrantSet.size() < 5) {
                        entrantSet.add(entranttwitch);
                    }

                }
            }

        }
        if (entrantSet.size() > 0) {
            String raceLink = "http://speedrun.tv/race:" + raceID;
            for (String s : entrantSet) {
                raceLink = raceLink + "/" + s;
            }
            return raceLink;
        } else {
            return null;
        }

    } catch (Exception ex) {
        System.out.println("Failed to get races.");
        return null;
    }
}

From source file:importer.Archive.java

/**
 * Using the project version info set long names for each short name
 * @param docid the project docid/*from  w  w w .j ava  2s  .  co  m*/
 * @throws ImporterException 
 */
public void setVersionInfo(String docid) throws ImporterException {
    try {
        String project = Connector.getConnection().getFromDb(Database.PROJECTS, docid);
        if (project == null) {
            String[] parts = docid.split("/");
            if (parts.length == 3)
                docid = parts[0] + "/" + parts[1];
            else
                throw new Exception("Couldn't find project " + docid);
            project = Connector.getConnection().getFromDb(Database.PROJECTS, docid);
            if (project == null)
                throw new Exception("Couldn't find project " + docid);
        }
        JSONObject jObj = (JSONObject) JSONValue.parse(project);
        if (jObj.containsKey(JSONKeys.VERSIONS)) {
            JSONArray versions = (JSONArray) jObj.get(JSONKeys.VERSIONS);
            for (int i = 0; i < versions.size(); i++) {
                JSONObject version = (JSONObject) versions.get(i);
                Set<String> keys = version.keySet();
                Iterator<String> iter = keys.iterator();
                while (iter.hasNext()) {
                    String key = iter.next();
                    nameMap.put(key, (String) version.get(key));
                }
            }
        }
    } catch (Exception e) {
        throw new ImporterException(e);
    }
}

From source file:minor.eft.ETFQuote.java

@Override
public String execute() {

    System.out.println("Ticker in Commodity Quote: " + getCticker());
    System.out.println("Checking");
    //List<CommodityDetails> sd2 = cdf.getCommodityData(getCticker());
    //System.out.println("Size of return Value: "+sd2.size());
    /*/*w  ww  .  jav a  2 s  .c  o  m*/
    for(CommodityDetails cd : sd2){
    cdf.create(cd);
    }*/
    JSONObject quote = eTFDetailsFacade.getETFData(getCticker());
    System.out.println("Array List: " + quote.toJSONString());
    //System.out.println("CDF String: "+cdf.toString());
    //System.out.println("Checking again");

    Set<String> st = quote.keySet();
    try {
        for (String s : st) {
            if (quote.get(s) == null) {
                System.out.println("IN IF");
                //quote.remove(s);
                quote.put(s, "NA");
            }
        }
    } catch (Exception e) {
        System.out.println(e);
    }
    ETFDetails cd = new ETFDetails(quote);
    System.out.println(cd.getPERatio());
    System.out.println(cd.getEBITDA());
    System.out.println(cd.getPreviousClose());
    System.out.println(cd.getDaysHigh());
    System.out.println(cd.getVolume());
    setSd(cd);
    //setSd(sd.add(cd));
    if (true) {
        return "success";
    } else {
        System.out.println("Checking again again");
        return "error";
    }
}

From source file:com.turt2live.uuid.turt2live.v2.ApiV2Service.java

@Override
public List<PlayerRecord> doBulkLookup(String... playerNames) {
    String list = combine(playerNames);
    String response = doUrlRequest(getConnectionUrl() + "/uuid/list/" + list);

    if (response != null) {
        JSONObject json = (JSONObject) JSONValue.parse(response);

        if (json.containsKey("results")) {
            JSONObject object = (JSONObject) json.get("results");
            List<PlayerRecord> records = new ArrayList<>();

            for (Object key : object.keySet()) {
                String name = key.toString();
                UUID uuid = UUID.fromString((String) object.get(key));

                if (uuid == null || name.equals("unknown"))
                    continue;

                PlayerRecord record = new Turt2LivePlayerRecord(uuid, name);
                records.add(record);//  w  w w . java  2  s . c  o  m
            }

            return records;
        }
    }

    return null;
}

From source file:com.turt2live.uuid.turt2live.v2.ApiV2Service.java

@Override
public List<PlayerRecord> doBulkLookup(UUID... uuids) {
    String list = combine(uuids);
    String response = doUrlRequest(getConnectionUrl() + "/name/list/" + list);

    if (response != null) {
        JSONObject json = (JSONObject) JSONValue.parse(response);

        if (json.containsKey("results")) {
            JSONObject object = (JSONObject) json.get("results");
            List<PlayerRecord> records = new ArrayList<>();

            for (Object key : object.keySet()) {
                UUID uuid = UUID.fromString(key.toString());
                String name = (String) object.get(key);

                if (uuid == null || name.equals("unknown"))
                    continue;

                // Note: v2 returns a v1 compatible player record
                PlayerRecord record = new Turt2LivePlayerRecord(uuid, name);
                records.add(record);/* ww  w.  j  a va  2s. c o  m*/
            }

            return records;
        }
    }

    return null;
}

From source file:com.relayrides.pushy.apns.util.ApnsPayloadBuilderTest.java

@Test
public void testSetShowActionButton() throws ParseException {
    this.builder.setShowActionButton(true);

    {//  w  ww  . j a v  a  2s .  c  o  m
        final JSONObject aps = this
                .extractApsObjectFromPayloadString(this.builder.buildWithDefaultMaximumLength());

        assertNull(aps.get("alert"));
    }

    this.builder.setShowActionButton(false);

    {
        final JSONObject aps = this
                .extractApsObjectFromPayloadString(this.builder.buildWithDefaultMaximumLength());
        final JSONObject alert = (JSONObject) aps.get("alert");

        assertTrue(alert.keySet().contains("action-loc-key"));
        assertNull(alert.get("action-loc-key"));
    }

    final String actionButtonKey = "action.key";
    this.builder.setLocalizedActionButtonKey(actionButtonKey);
    this.builder.setShowActionButton(true);

    {
        final JSONObject aps = this
                .extractApsObjectFromPayloadString(this.builder.buildWithDefaultMaximumLength());
        final JSONObject alert = (JSONObject) aps.get("alert");

        assertEquals(actionButtonKey, alert.get("action-loc-key"));
    }

    this.builder.setShowActionButton(false);

    {
        final JSONObject aps = this
                .extractApsObjectFromPayloadString(this.builder.buildWithDefaultMaximumLength());
        final JSONObject alert = (JSONObject) aps.get("alert");

        assertTrue(alert.keySet().contains("action-loc-key"));
        assertNull(alert.get("action-loc-key"));
    }
}

From source file:functionaltests.RestSchedulerJobPaginationTest.java

@Test
public void testJobsPagination() throws Exception {
    Assert.assertEquals("Test expects only one node", 1, RestFuncTHelper.DEFAULT_NUMBER_OF_NODES);

    JobId jobId1 = getScheduler().submit(createJob(NonTerminatingJob.class));
    waitJobState(jobId1, JobStatus.RUNNING, 30000);

    JobId jobId2 = submitDefaultJob();//from  w  w  w. ja v  a 2s  .c  o  m
    JobId jobId3 = submitDefaultJob();

    checkPagingRequests1();

    // check 'jobsinfo' and 'revisionjobsinfo' provide job's attributes

    JSONObject page;
    JSONArray jobs;

    page = getRequestJSONObject(getResourceUrl("jobsinfo"));
    jobs = (JSONArray) page.get("list");
    checkJob((JSONObject) jobs.get(2), JobStatus.RUNNING, 1, 0);

    page = getRequestJSONObject(getResourceUrl("revisionjobsinfo"));
    JSONObject map = (JSONObject) page.get("map");
    Assert.assertEquals(1, map.keySet().size());
    jobs = (JSONArray) map.get(map.keySet().iterator().next());
    checkJob((JSONObject) jobs.get(2), JobStatus.RUNNING, 1, 0);

    checkFiltering1();

    String resource = "jobs/" + jobId1.value() + "/kill";
    String schedulerUrl = getResourceUrl(resource);
    HttpPut httpPut = new HttpPut(schedulerUrl);
    setSessionHeader(httpPut);
    HttpResponse response = executeUriRequest(httpPut);
    assertHttpStatusOK(response);

    waitJobState(jobId1, JobStatus.KILLED, 30000);
    waitJobState(jobId2, JobStatus.FINISHED, 30000);
    waitJobState(jobId3, JobStatus.FINISHED, 30000);

    checkFiltering2();

    // check 'jobsinfo' and 'revisionjobsinfo' provide updated job's attributes

    JSONObject job = null;

    page = getRequestJSONObject(getResourceUrl("jobsinfo"));
    jobs = (JSONArray) page.get("list");
    checkJob(findJob("1", jobs), JobStatus.KILLED, 0, 0);

    page = getRequestJSONObject(getResourceUrl("revisionjobsinfo"));
    map = (JSONObject) page.get("map");
    Assert.assertEquals(1, map.keySet().size());
    jobs = (JSONArray) map.get(map.keySet().iterator().next());
    checkJob(findJob("1", jobs), JobStatus.KILLED, 0, 0);

    checkPagingRequests2();
}

From source file:gwap.game.quiz.PlayNRatingCommunicationResource.java

private UserPerceptionRating readOutJSONData(HttpServletRequest request) throws IOException {
    UserPerceptionRating userRecommendedRating = null;
    BufferedReader reader = request.getReader();
    StringBuilder sb = new StringBuilder();
    String line = reader.readLine();
    while (line != null) {
        sb.append(line + "\n");
        line = reader.readLine();//from w w  w .  java 2  s  . co m
    }
    reader.close();
    String data = sb.toString();

    JSONParser parser = new JSONParser();

    userRecommendedRating = new UserPerceptionRating();

    Object obj;
    PerceptionPair[] perceptionPair = new PerceptionPair[5];
    try {
        obj = parser.parse(data);
        JSONObject jsonObject = (JSONObject) obj;

        PerceptionPair p;
        for (Object k : jsonObject.keySet()) {

            Object value = jsonObject.get(k);
            String key = (String) k;

            if (key.equals("SID")) {
                this.sessionID = (String) value;
            } else if (key.equals("QuestionNumber")) {
                userRecommendedRating.setQuestionNumber(value);
            } else if (key.equals("LinearVsMalerisch")) {
                p = new PerceptionPair();
                p.setPairname((String) k);
                p.setValue((Long) value);
                perceptionPair[0] = p;
            } else if (key.equals("FlaecheVsTiefe")) {
                p = new PerceptionPair();
                p.setPairname((String) k);
                p.setValue((Long) value);
                perceptionPair[1] = p;
            } else if (key.equals("GeschlossenVsOffen")) {
                p = new PerceptionPair();
                p.setPairname((String) k);
                p.setValue((Long) value);
                perceptionPair[2] = p;
            } else if (key.equals("VielheitVsEinheit")) {
                p = new PerceptionPair();
                p.setPairname((String) k);
                p.setValue((Long) value);
                perceptionPair[3] = p;
            } else if (key.equals("KlarheitVsUnklarheitUndBewegtheit")) {
                p = new PerceptionPair();
                p.setPairname((String) k);
                p.setValue((Long) value);
                perceptionPair[4] = p;
            } else if (key.equals("FillOutTimeMs")) {
                Long v;
                // for Java
                if (value instanceof Double) {
                    v = ((Double) value).longValue();
                    // for HTML5
                } else {
                    v = (Long) value;
                }

                userRecommendedRating.setFillOutTimeMs(v);
            }

        }
    } catch (ParseException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (ClassCastException e) {
        e.printStackTrace();
    }
    userRecommendedRating.setPairs(perceptionPair);
    return userRecommendedRating;
}