Example usage for org.json.simple JSONObject values

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

Introduction

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

Prototype

Collection<V> values();

Source Link

Document

Returns a Collection view of the values contained in this map.

Usage

From source file:models.Calais.java

private JSONObject parseJSON(Reader reader) throws IOException, ParseException {
    myComparator = new Comparator<JSONObject>() {
        public int compare(JSONObject jsonObject, JSONObject jsonObject1) {
            double relevance = (Double) jsonObject.get("relevance");
            double relevance1 = (Double) jsonObject1.get("relevance");
            if (relevance < relevance1)
                return 1;
            return -1;
        }/*from  w  w w .  j ava  2  s  . c  o  m*/
    };

    JSONParser parser = new JSONParser();
    JSONObject jsonObject = (JSONObject) parser.parse(reader);
    JSONObject result = new JSONObject();
    TreeSet<JSONObject> people = new TreeSet<JSONObject>(myComparator);
    TreeSet<JSONObject> companies = new TreeSet<JSONObject>(myComparator);

    for (Object value : jsonObject.values()) {
        JSONObject jsonValue = (JSONObject) value;
        String type = (String) jsonValue.get("_type");
        if (type != null && type.equalsIgnoreCase("Person")) {
            people.add(jsonValue);
        } else if (type != null && type.equalsIgnoreCase("Company")) {
            companies.add(jsonValue);
        }
    }

    List<String> peopleList = new ArrayList<String>();
    List<String> companyList = new ArrayList<String>();

    for (JSONObject person : people) {
        peopleList.add((String) person.get("name"));
    }
    for (JSONObject company : companies) {
        companyList.add((String) company.get("name"));
    }

    JSONObject resultValue = new JSONObject();
    resultValue.put("people", peopleList);
    resultValue.put("companies", companyList);
    result.put("result", resultValue);

    return result;
}

From source file:org.opencastproject.adminui.endpoint.ListProvidersEndpointTest.java

@Test
public void testGetGeneric() throws ParseException {
    JSONObject all = (JSONObject) parser
            .parse(given().log().all().pathParam("id", TestListProvidersEndpoint.PROVIDER_NAME).expect()
                    .statusCode(HttpStatus.SC_OK).contentType(ContentType.JSON)
                    .body("2", containsString(TestListProvidersEndpoint.PROVIDER_VALUES[2]))
                    .body("", hasValue("z")).when().get(rt.host("/{id}.json")).asString());

    assertEquals(TestListProvidersEndpoint.PROVIDER_VALUES.length, all.entrySet().size());

    given().log().all().pathParam("id", "missingprovider").expect().statusCode(HttpStatus.SC_NOT_FOUND).when()
            .get(rt.host("/{id}.json"));

    int limit = 2;
    int offset = 2;
    JSONObject limited = (JSONObject) parser
            .parse(given().log().all().pathParam("id", TestListProvidersEndpoint.PROVIDER_NAME)
                    .queryParam("limit", limit).queryParam("offset", offset).expect()
                    .statusCode(HttpStatus.SC_OK).when().get(rt.host("/{id}.json")).asString());

    assertEquals(limit, limited.entrySet().size());

    Object[] allValues = all.values().toArray();
    Object[] limitedValues = limited.values().toArray();

    for (int i = 0; i < limitedValues.length; i++) {
        assertEquals(limitedValues[i], allValues[offset + i]);
    }/* ww w  .  j  ava2  s.  c o m*/
}

From source file:org.eclipse.birt.report.engine.emitter.json.JsonEmitter.java

@SuppressWarnings("unchecked")
@Override/*from  w  w w  .  j av  a  2 s  . c om*/
public void endTableGroup(ITableGroupContent tableGroup) throws BirtException {
    JSONObject groupObj = groupObjStack.pop();
    JSONArray valueArray = new ArrayList<JSONArray>(groupObj.values()).get(0);
    valueArray.addAll(jsonObjectList);
    jsonObjectList.clear();

    if (groupObjStack.size() != 0) {
        JSONObject parentGrpObj = groupObjStack.pop();
        valueArray = new ArrayList<JSONArray>(parentGrpObj.values()).get(0);
        valueArray.add(groupObj);
        groupObjStack.push(parentGrpObj);
    } else {
        groupObjectList.add(groupObj);
    }
}

From source file:org.jolokia.converter.object.TabularDataConverter.java

private void putRowsToTabularData(TabularDataSupport pTabularData, JSONObject pValue, int pLevel) {
    TabularType type = pTabularData.getTabularType();
    for (Object value : pValue.values()) {
        if (!(value instanceof JSONObject)) {
            throw new IllegalArgumentException(
                    "Cannot convert " + pValue + " to type " + type + " because the object values provided ("
                            + value.getClass() + ") is not of the expected type JSONObject at level " + pLevel);
        }//from ww w .j a v  a2 s  .  co  m
        JSONObject jsonValue = (JSONObject) value;
        if (pLevel > 1) {
            putRowsToTabularData(pTabularData, jsonValue, pLevel - 1);
        } else {
            pTabularData.put((CompositeData) getDispatcher().convertToObject(type.getRowType(), jsonValue));
        }
    }
}

From source file:org.jolokia.handler.list.MBeanInfoData.java

private Object navigatePath() {
    int size = pathStack.size();
    JSONObject innerMap = infoMap;

    while (size > 0) {
        Collection vals = innerMap.values();
        if (vals.size() == 0) {
            return innerMap;
        } else if (vals.size() != 1) {
            throw new IllegalStateException(
                    "Internal: More than one key found when extracting with path: " + vals);
        }/*from   ww w.  j  av  a  2s  .  c om*/
        Object value = vals.iterator().next();

        // End leaf, return it ....
        if (size == 1) {
            return value;
        }
        // Dive in deeper ...
        if (!(value instanceof JSONObject)) {
            throw new IllegalStateException(
                    "Internal: Value within path extraction must be a Map, not " + value.getClass());
        }
        innerMap = (JSONObject) value;
        --size;
    }
    return innerMap;
}

From source file:org.wso2.carbon.apimgt.impl.definitions.APIDefinitionFromOpenAPISpec.java

/**
 * This method returns the oauth scopes according to the given swagger
 *
 * @param resourceConfigsJSON resource json
 * @return scope set//from   w  w  w. ja  v a2s .co  m
 * @throws APIManagementException
 */
@Override
public Set<Scope> getScopes(String resourceConfigsJSON) throws APIManagementException {
    Set<Scope> scopeList = new LinkedHashSet<Scope>();
    JSONObject swaggerObject;
    JSONParser parser = new JSONParser();
    try {
        swaggerObject = (JSONObject) parser.parse(resourceConfigsJSON);

        //Check whether security definitions are defined or not
        if (swaggerObject.get(APIConstants.SWAGGER_X_WSO2_SECURITY) != null) {
            JSONObject securityDefinitionsObjects = (JSONObject) swaggerObject
                    .get(APIConstants.SWAGGER_X_WSO2_SECURITY);

            for (JSONObject securityDefinition : (Iterable<JSONObject>) securityDefinitionsObjects.values()) {
                //Read scopes from custom wso2 scopes

                if (securityDefinition.get(APIConstants.SWAGGER_X_WSO2_SCOPES) != null) {
                    JSONArray oauthScope = (JSONArray) securityDefinition
                            .get(APIConstants.SWAGGER_X_WSO2_SCOPES);
                    for (Object anOauthScope : oauthScope) {
                        Scope scope = new Scope();
                        JSONObject scopeObj = (JSONObject) anOauthScope;
                        scope.setKey((String) scopeObj.get(APIConstants.SWAGGER_SCOPE_KEY));
                        scope.setName((String) scopeObj.get(APIConstants.SWAGGER_NAME));
                        scope.setDescription((String) scopeObj.get(APIConstants.SWAGGER_DESCRIPTION));
                        scope.setRoles(scopeObj.get(APIConstants.SWAGGER_ROLES).toString());

                        scopeList.add(scope);
                    }
                }
            }
        }
    } catch (ParseException e) {
        handleException("Invalid resource configuration ", e);
    }
    return scopeList;
}

From source file:rss.RSSController.java

public void parseElement(java.util.Vector<channel> tabChannels, javax.swing.DefaultListModel names) {
    if (_inputs.size() > 0) {
        String input = _inputs.get(0);
        JSONParser parser = new JSONParser();
        String jsonText = input;/*from   w w  w . j a v a  2s  .c  o  m*/
        try {
            JSONArray array = (JSONArray) parser.parse(jsonText);
            tabChannels.removeAllElements();
            for (int i = 0; i < array.size(); ++i) {
                channel c = new channel();
                JSONObject obj = (JSONObject) array.get(i);
                Object[] tab = obj.values().toArray();
                System.out.println(tab);
                JSONArray arr = (JSONArray) obj.get("items");
                c.setName((String) obj.get("title"));
                for (int j = 0; j < arr.size(); ++j) {
                    JSONObject o = (JSONObject) arr.get(j);
                    c.push((String) o.get("description"));
                }
                tabChannels.addElement(c);
                names.addElement((String) tab[3]);
                //                 return (c);

            }
        } catch (ParseException pe) {
        }

    } else {
    }
}

From source file:rss.RSSController.java

public void parseTab(channel c) {
    System.out.println("parse tab");
    JSONParser parser = new JSONParser();
    System.out.println(_input);//from www  . ja va  2  s .  c om
    String jsonText = _input;
    try {
        JSONArray array = (JSONArray) parser.parse(jsonText);
        System.out.println(array.size());
        for (int i = 0; i < array.size(); ++i) {
            JSONObject obj = (JSONObject) array.get(i);
            Object[] tab = obj.values().toArray();
            JSONArray arr = (JSONArray) tab[4];
            c.setName((String) tab[3]);
            for (int j = 0; j < arr.size(); ++j) {
                JSONObject o = (JSONObject) arr.get(j);
                System.out.println((String) o.get("description"));
                c.push((String) o.get("description"));
            }
        }
    } catch (ParseException pe) {
    }
}

From source file:us.mn.state.health.lims.result.action.ResultsLogbookUpdateAction.java

private void getSelectedReflexes(String reflexJSONResult, Map<String, List<String>> triggersToReflexesMap) {
    if (!GenericValidator.isBlankOrNull(reflexJSONResult)) {
        JSONParser parser = new JSONParser();
        try {/*from   www  . jav a2  s  .  c o m*/
            JSONObject jsonResult = (JSONObject) parser.parse(reflexJSONResult.replaceAll("'", "\""));

            for (Object compoundReflexes : jsonResult.values()) {
                if (compoundReflexes != null) {
                    String triggerIds = (String) ((JSONObject) compoundReflexes).get("triggerIds");
                    List<String> selectedReflexIds = new ArrayList<String>();
                    JSONArray selectedReflexes = (JSONArray) ((JSONObject) compoundReflexes).get("selected");
                    for (Object selectedReflex : selectedReflexes) {
                        selectedReflexIds.add(((String) selectedReflex));
                    }
                    triggersToReflexesMap.put(triggerIds.trim(), selectedReflexIds);
                }
            }
        } catch (ParseException e) {
            e.printStackTrace();
        }
    }
}