List of usage examples for org.json JSONObject getNames
public static String[] getNames(Object object)
From source file:org.araqne.confdb.file.Importer.java
private Map<String, Object> parse(JSONObject jsonObject) throws IOException { Map<String, Object> m = new HashMap<String, Object>(); String[] names = JSONObject.getNames(jsonObject); if (names == null) return m; for (String key : names) { try {/*from w w w . j av a2 s . c o m*/ Object value = jsonObject.get(key); if (value == JSONObject.NULL) value = null; else if (value instanceof JSONArray) value = parse((JSONArray) value); else if (value instanceof JSONObject) value = parse((JSONObject) value); m.put(key, value); } catch (JSONException e) { logger.error("araqne confdb: cannot parse json", e); throw new IOException(e); } } return m; }
From source file:com.rapid.actions.Webservice.java
public Webservice(RapidHttpServlet rapidServlet, JSONObject jsonAction) throws Exception { // set the xml version super();//from w w w .j a v a2 s . c o m // save all key/values from the json into the properties for (String key : JSONObject.getNames(jsonAction)) { // add all json properties to our properties, except for query if (!"request".equals(key) && !"root".equals(key) && !"showLoading".equals(key) && !"successActions".equals(key) && !"errorActions".equals(key)) addProperty(key, jsonAction.get(key).toString()); } // try and build the query object JSONObject jsonQuery = jsonAction.optJSONObject("request"); // check we got one if (jsonQuery != null) { // get the parameters ArrayList<Parameter> inputs = getParameters(jsonQuery.optJSONArray("inputs")); String type = jsonQuery.optString("type"); String url = jsonQuery.optString("url"); String action = jsonQuery.optString("action"); String body = jsonQuery.optString("body"); String transform = jsonQuery.optString("transform"); String root = jsonQuery.optString("root"); ArrayList<Parameter> outputs = getParameters(jsonQuery.optJSONArray("outputs")); // make the object _request = new Request(inputs, type, url, action, body, transform, root, outputs); } // look for showLoading _showLoading = jsonAction.optBoolean("showLoading"); // grab any successActions JSONArray jsonSuccessActions = jsonAction.optJSONArray("successActions"); // if we had some if (jsonSuccessActions != null) { // instantiate our success actions collection _successActions = Control.getActions(rapidServlet, jsonSuccessActions); } // grab any errorActions JSONArray jsonErrorActions = jsonAction.optJSONArray("errorActions"); // if we had some if (jsonErrorActions != null) { // instantiate our error actions collection _errorActions = Control.getActions(rapidServlet, jsonErrorActions); } }
From source file:com.google.enterprise.connector.db.diffing.JsonDocument.java
public String toJson() { // JSON does not support custom serialization, so we have to find // the InputStreamFactory for the content and serialize it // ourselves. This could be cleaner if we supported toString on the // InputStreamFactory implementations, but that would mean less // control over when the LOB was materialized in memory. try {/* ww w. j av a2 s. co m*/ StringWriter buffer = new StringWriter(); JSONWriter writer = new JSONWriter(buffer); writer.object(); for (String name : JSONObject.getNames(jsonObject)) { writer.key(name).value(toJson(name, jsonObject.get(name))); } writer.endObject(); return buffer.toString(); } catch (IOException e) { throw new SnapshotRepositoryRuntimeException("Error serializing document " + objectId, e); } catch (JSONException e) { throw new SnapshotRepositoryRuntimeException("Error serializing document " + objectId, e); } }
From source file:com.rapid.actions.Group.java
public Group(RapidHttpServlet rapidServlet, JSONObject jsonAction) throws Exception { // call the super parameterless constructor which sets the xml version super();/*from w w w . ja v a2 s.c om*/ // save all key/values from the json into the properties for (String key : JSONObject.getNames(jsonAction)) { // add all json properties to our properties, except for the ones we want directly accessible if (!"actions".equals(key)) addProperty(key, jsonAction.get(key).toString()); } // grab any actions JSONArray jsonActions = jsonAction.optJSONArray("actions"); // if we had some if (jsonActions != null) { _actions = Control.getActions(rapidServlet, jsonActions); } }
From source file:com.google.enterprise.connector.db.diffing.DBSnapshotRepositoryTest.java
/** * Compares two JSON strings for equality. The order of the * name/value pairs in the string is not important (and that's why * this method is required, or we would just compare the strings * with each other).//from w w w .j a va 2 s. c om */ private void assertJsonEquals(String message, String expectedString, String actualString) throws JSONException { JSONObject expected = new JSONObject(expectedString); JSONObject actual = new JSONObject(actualString); Set<String> expectedNames = Sets.newHashSet(JSONObject.getNames(expected)); Set<String> actualNames = Sets.newHashSet(JSONObject.getNames(actual)); assertEquals(message, expectedNames, actualNames); for (String name : expectedNames) { assertEquals(message, expected.get(name), actual.get(name)); } }
From source file:cz.autoclient.league_of_legends.DataLoader.java
/** * Loops through given object and returns name properties for every entry. * By name properties I mean {x:{name:something}, y: ...}, not the json keys * @param o //www . j av a2 s . c o m * @return array of names using for this entries in game */ public static ArrayList<String> listJSONNames(JSONObject o) { ArrayList<String> names = new ArrayList<>(); String[] ids = JSONObject.getNames(o); //System.out.println("Fetching "+ids.length+" champion names."); for (String id : ids) { try { names.add(o.getJSONObject(id).getString("name")); } catch (JSONException ex) { //System.err.println("Error fetching champion "+id+" name: "+ex); //Ignore and continue } } return names; }
From source file:com.github.koraktor.steamcondenser.community.GameItem.java
/** * Creates a new instance of a GameItem with the given data * * @param inventory The inventory this item is contained in * @param itemData The data specifying this item * @throws WebApiException on Web API errors *//*from www . jav a2s. c om*/ public GameItem(GameInventory inventory, JSONObject itemData) throws SteamCondenserException { this.inventory = inventory; try { this.defindex = itemData.getInt("defindex"); this.backpackPosition = (int) itemData.getLong("inventory") & 0xffff; this.count = itemData.getInt("quantity"); this.craftable = !itemData.optBoolean("flag_cannot_craft"); this.id = itemData.getInt("id"); this.itemClass = this.getSchemaData().getString("item_class"); this.itemSet = this.inventory.getItemSchema().getItemSets() .get(this.getSchemaData().optString("item_set")); this.level = itemData.getInt("level"); this.name = this.getSchemaData().getString("item_name"); this.preliminary = (itemData.getLong("inventory") & 0x40000000) != 0; this.originalId = itemData.getInt("original_id"); this.quality = this.inventory.getItemSchema().getQualities().get(itemData.getInt("quality")); this.tradeable = !itemData.optBoolean("flag_cannot_trade"); this.type = this.getSchemaData().getString("item_type_name"); if (itemData.has("origin")) { this.origin = this.inventory.getItemSchema().getOrigins().get(itemData.getInt("origin")); } JSONArray attributesData = this.getSchemaData().optJSONArray("attributes"); if (attributesData == null) { attributesData = new JSONArray(); } if (itemData.has("attributes")) { JSONArray itemAttributes = itemData.getJSONArray("attributes"); for (int i = 0; i < itemAttributes.length(); i++) { attributesData.put(itemAttributes.get(i)); } } this.attributes = new ArrayList<JSONObject>(); for (int i = 0; i < attributesData.length(); i++) { JSONObject attributeData = attributesData.getJSONObject(i); Object attributeKey = attributeData.opt("defindex"); if (attributeKey == null) { attributeKey = attributeData.opt("name"); } if (attributeKey != null) { JSONObject schemaAttributeData = inventory.getItemSchema().getAttributes().get(attributeKey); for (String key : JSONObject.getNames(schemaAttributeData)) { attributeData.put(key, schemaAttributeData.get(key)); } this.attributes.add(attributeData); } } } catch (JSONException e) { throw new WebApiException("Could not parse JSON data.", e); } }
From source file:org.archive.porky.JSON.java
/** * Convert JSON object into a Pig object, recursively convert * children as well./*from ww w .ja v a2 s.c o m*/ */ public static Object fromJSON(Object o) throws IOException, JSONException { if (o instanceof String || o instanceof Long || o instanceof Double || o instanceof Integer) { return o; } else if (JSONObject.NULL.equals(o)) { return null; } else if (o instanceof JSONObject) { JSONObject json = (JSONObject) o; Map<String, Object> map = new HashMap<String, Object>(json.length()); // If getNames() returns null, then it's an empty JSON object. String[] names = JSONObject.getNames(json); if (names == null) return map; for (String key : JSONObject.getNames(json)) { Object value = json.get(key); // Recurse the value map.put(key, fromJSON(value)); } // Now, check to see if the map keys match the formula for // a Tuple, that is if they are: "$0", "$1", "$2", ... // First, peek to see if there is a "$0" key, if so, then // start moving the map entries into a Tuple. if (map.containsKey("$0")) { Tuple tuple = TupleFactory.getInstance().newTuple(map.size()); for (int i = 0; i < map.size(); i++) { // If any of the expected $N keys is not found, give // up and return the map. if (!map.containsKey("$" + i)) return map; tuple.set(i, map.get("$" + i)); } return tuple; } return map; } else if (o instanceof JSONArray) { JSONArray json = (JSONArray) o; List<Tuple> tuples = new ArrayList<Tuple>(json.length()); for (int i = 0; i < json.length(); i++) { tuples.add(TupleFactory.getInstance().newTuple(fromJSON(json.get(i)))); } DataBag bag = BagFactory.getInstance().newDefaultBag(tuples); return bag; } else if (o instanceof Boolean) { // Since Pig doesn't have a true boolean data type, we map it to // String values "true" and "false". if (((Boolean) o).booleanValue()) { return "true"; } return "false"; } else { // FIXME: What to do here? throw new IOException("Unknown data-type serializing from JSON: " + o); } }
From source file:org.everit.json.schema.ObjectSchema.java
private Stream<String> getAdditionalProperties(final JSONObject subject) { String[] names = JSONObject.getNames(subject); if (names == null) { return Stream.empty(); } else {/*from w w w . ja v a 2 s . co m*/ return Arrays.stream(names).filter(key -> !propertySchemas.containsKey(key)) .filter(key -> !matchesAnyPattern(key)); } }
From source file:org.everit.json.schema.ObjectSchema.java
private void testPatternProperties(final JSONObject subject) { String[] propNames = JSONObject.getNames(subject); if (propNames == null || propNames.length == 0) { return;//w w w.j a v a 2 s. co m } for (Entry<Pattern, Schema> entry : patternProperties.entrySet()) { for (String propName : propNames) { if (entry.getKey().matcher(propName).find()) { entry.getValue().validate(subject.get(propName)); } } } }