Example usage for org.json JSONObject getNames

List of usage examples for org.json JSONObject getNames

Introduction

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

Prototype

public static String[] getNames(Object object) 

Source Link

Document

Get an array of field names from an Object.

Usage

From source file:com.rapid.actions.Validation.java

public Validation(RapidHttpServlet rapidServlet, JSONObject jsonAction) throws Exception {
    // set the xml version
    super();//from   w  w  w. j a  va 2s .com
    // 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 controls
        if (!"controls".equals(key) && !"passActions".equals(key) && !"failActions".equals(key))
            addProperty(key, jsonAction.get(key).toString());
    }
    // grab any controls
    JSONArray jsonControls = jsonAction.optJSONArray("controls");
    // if we had some
    if (jsonControls != null) {
        // instantiate our contols collection
        _controls = new ArrayList<String>();
        // loop the json Controls
        for (int i = 0; i < jsonControls.length(); i++) {
            // add into our collection
            _controls.add(jsonControls.getString(i));
        }
    }

    // grab any passActions
    JSONArray jsonPassActions = jsonAction.optJSONArray("passActions");
    // if we had some
    if (jsonPassActions != null) {
        // instantiate our pass actions collection
        _passActions = Control.getActions(rapidServlet, jsonPassActions);
    }

    // grab any failActions
    JSONArray jsonFailActions = jsonAction.optJSONArray("failActions");
    // if we had some
    if (jsonFailActions != null) {
        // instantiate our fail actions collection
        _failActions = Control.getActions(rapidServlet, jsonFailActions);
    }
}

From source file:com.reignite.parser.QueryParser.java

private Criterion processField(JSONObject where) throws ParserException {
    String field = JSONObject.getNames(where)[0];
    JSONObject exp;//from   w  w  w  . ja  v a2 s  .  co m
    try {
        exp = where.getJSONObject(field);
    } catch (JSONException e) {
        throw new ParserException("field expressions must be JSON Object: " + field);
    }
    ExpressionType type = ExpressionType.get(JSONObject.getNames(exp)[0]);
    Object value = null;
    // if the field is a join
    if (field.indexOf(".") > -1) {
        String join = field.substring(0, field.indexOf("."));
        query.createJoin(join);
    }
    switch (type) {
    case IN:
    case NOT_IN:
        try {
            value = createArray(exp.getJSONArray(type.getName()));
        } catch (JSONException e) {
            throw new ParserException("in and not in expressions must be arrays: " + exp);
        }
        break;
    default:
        try {
            value = createValue(exp.get(type.getName()));
        } catch (JSONException e) {
            throw new ParserException("expressions must have a value: " + exp);
        }
    }

    switch (type) {
    case GREATER_THAN:
        return Restrictions.gt(field, value);
    case IN:
        return Restrictions.in(field, (Object[]) value);
    case LESS_THAN:
        return Restrictions.lt(field, value);
    case LIKE:
        MatchMode match = MatchMode.EXACT;
        String toMatch = value.toString();
        if (value.toString().startsWith("%")) {
            match = MatchMode.END;
            toMatch = toMatch.substring(1);
        }
        if (value.toString().endsWith("%")) {
            toMatch = toMatch.substring(0, toMatch.length() - 1);
            if (match == MatchMode.END) {
                match = MatchMode.ANYWHERE;
            } else {
                match = MatchMode.START;
            }
        }
        return Restrictions.ilike(field, toMatch, match);
    case NOT_EQUAL:
        if (value == null) {
            return Restrictions.isNotNull(field);
        }
        return Restrictions.ne(field, value);
    case NOT_IN:
        return Restrictions.not(Restrictions.in(field, (Object[]) value));
    case EQUAL:
    default:
        if (value == null) {
            return Restrictions.isNull(field);
        }
        return Restrictions.eq(field, value);
    }

}

From source file:com.reignite.parser.QueryParser.java

private void addFields(JSONObject jobj) throws ParserException {
    if (jobj.has("fields")) {
        JSONArray fields;//  w  ww .j a  v a2  s.c  o m
        try {
            fields = jobj.getJSONArray("fields");
        } catch (JSONException e) {
            throw new ParserException("fields must be a JSONArray containing either strings or JSONObjects");
        }

        for (int i = 0; i < fields.length(); i++) {
            Object field;
            try {
                field = fields.get(i);
            } catch (JSONException e) {
                throw new ParserException("fields must not be an empty JSONArray if it is included.");
            }
            if (field instanceof String) {
                query.addField(field.toString());
            } else if (field instanceof JSONObject) {
                JSONObject jField = (JSONObject) field;
                String[] names = JSONObject.getNames(jField);
                for (String aggType : names) {
                    QueryType type = QueryType.valueOf(aggType.toUpperCase());
                    try {
                        // query.addField(jField.getString(aggType));
                        query.addAggregate(type, jField.getString(aggType));
                    } catch (JSONException e) {
                        throw new ParserException(
                                "aggregate fields must be of the form {sum|avg|count:'field'}");
                    }
                }
            }
        }
    }
}

From source file:datafu.hourglass.avro.AvroMultipleInputsUtil.java

/**
 * Gets the schema for a particular input split. 
 * //w  w  w .ja v a 2  s.  co m
 * @param conf configuration to get schema from
 * @param split input split to get schema for
 * @return schema
 */
public static Schema getInputKeySchemaForSplit(Configuration conf, InputSplit split) {
    String path = ((FileSplit) split).getPath().toString();
    _log.info("Determining schema for input path " + path);
    JSONObject schemas;
    try {
        schemas = getInputSchemas(conf);
    } catch (JSONException e1) {
        throw new RuntimeException(e1);
    }
    Schema schema = null;
    if (schemas != null) {
        for (String key : JSONObject.getNames(schemas)) {
            _log.info("Checking against " + key);
            if (path.startsWith(key)) {
                try {
                    schema = new Schema.Parser().parse(schemas.getString(key));
                    _log.info("Input schema found: " + schema.toString(true));
                    break;
                } catch (JSONException e) {
                    throw new RuntimeException(e);
                }
            }
        }
    }
    if (schema == null) {
        _log.info("Could not determine input schema");
    }
    return schema;
}

From source file:com.rapid.actions.Mobile.java

public Mobile(RapidHttpServlet rapidServlet, JSONObject jsonAction) throws Exception {
    this();/*from  w  w w  .  j a v  a 2  s .co  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 success and error actions
        if (!"successActions".equals(key) && !"errorActions".equals(key) && !"onlineActions".equals(key))
            addProperty(key, jsonAction.get(key).toString());
    }

    // upload images was modified to have a number of gallery ids (rather than 1) migrate for old versions
    String type = getProperty("actionType");
    // if this is upload images
    if ("uploadImages".equals(type)) {
        // get any single gallery controlId
        String galleryControlId = getProperty("galleryControlId");
        // if not null
        if (galleryControlId != null) {
            // empty the property
            _properties.remove("galleryControlId");
            // move it into the galleryControlIds
            _properties.put("galleryControlIds", "[\"" + galleryControlId + "\"]");
        }
    }

    // grab any successActions
    JSONArray jsonSuccessActions = jsonAction.optJSONArray("successActions");
    // if we had some
    if (jsonSuccessActions != null) {
        _successActions = Control.getActions(rapidServlet, jsonSuccessActions);
    }

    // grab any errorActions
    JSONArray jsonErrorActions = jsonAction.optJSONArray("errorActions");
    // if we had some
    if (jsonErrorActions != null) {
        // instantiate our contols collection
        _errorActions = Control.getActions(rapidServlet, jsonErrorActions);
    }

    // grab any onlineActions
    JSONArray jsonOnlineActions = jsonAction.optJSONArray("onlineActions");
    // if we had some
    if (jsonOnlineActions != null) {
        // instantiate our contols collection
        _onlineActions = Control.getActions(rapidServlet, jsonOnlineActions);
    }
}

From source file:org.sc.probro.json.ValidatedJSON.java

public ValidatedJSON(String schemaName, JSONObject obj) {
    super(obj, JSONObject.getNames(obj));
    this.schemaName = schemaName;
}

From source file:com.rapid.actions.Datacopy.java

public Datacopy(RapidHttpServlet rapidServlet, JSONObject jsonAction) throws Exception {
    // call the super parameterless constructor which sets the xml version
    super();//from w  w w .ja  v  a2s  .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 dataCopies
        if (!"dataCopies".equals(key))
            addProperty(key, jsonAction.get(key).toString());
    }
    // look for a dataCopies array
    JSONArray jsonDataCopies = jsonAction.optJSONArray("dataCopies");
    // if we got one
    if (jsonDataCopies != null) {
        // instantiate our collection
        _dataCopies = new ArrayList<DataCopy>();
        // loop it
        for (int i = 0; i < jsonDataCopies.length(); i++) {
            // add this one
            _dataCopies.add(new DataCopy(jsonDataCopies.getJSONObject(i)));
        }
    }
}

From source file:org.openstatic.util.JSONUtil.java

public static String json2table(JSONObject jobject) throws Exception {
    String return_string = "<table cellspacing=\"0\" cellpadding=\"4\" style=\"border-color: #000000; border-style: solid; border-width: 1px; background-color: #FFFFFF; color: #000000;\">\r\n";
    String[] fnames = JSONObject.getNames(jobject);
    if (fnames != null) {
        return_string += "<tr style=\"background-color: #AAAAAA; color: #000000;\"><th>KEY</th><th>VALUE</th></tr>";
        for (int i = 0; i < fnames.length; i++) {
            Object value = jobject.get(fnames[i]);
            String string_value = "";
            if (value != null) {
                if (value instanceof JSONObject)
                    string_value = json2table((JSONObject) value);
                else if (value instanceof JSONArray) {
                    string_value = json2table((JSONArray) value);
                } else {
                    string_value = htmlEntityEncode(jobject.getString(fnames[i]));
                }/*from ww w.  j  a v a2  s. c  o  m*/
            }
            return_string += "<tr><td style=\"border-bottom: 1px dotted #999999; border-right: 1px dotted #999999; vertical-align: text-top;\">"
                    + fnames[i] + "<td style=\"border-bottom: 1px dotted #999999;\">" + string_value
                    + "</td></tr>";
        }
    }
    return_string += "</table>";
    return return_string;
}

From source file:com.rapid.actions.Database.java

public Database(RapidHttpServlet rapidServlet, JSONObject jsonAction) throws Exception {
    // call the super parameterless constructor which sets the xml version
    super();/*from  ww w  .ja v  a 2  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 (!"query".equals(key) && !"showLoading".equals(key) && !"childDatabaseActions".equals(key)
                && !"successActions".equals(key) && !"errorActions".equals(key) && !"childActions".equals(key))
            addProperty(key, jsonAction.get(key).toString());
    }

    // try and build the query object
    JSONObject jsonQuery = jsonAction.optJSONObject("query");

    // check we got one
    if (jsonQuery != null) {
        // get the parameters                  
        ArrayList<Parameter> inputs = getParameters(jsonQuery.optJSONArray("inputs"));
        ArrayList<Parameter> outputs = getParameters(jsonQuery.optJSONArray("outputs"));
        String sql = jsonQuery.optString("SQL");
        boolean multiRow = jsonQuery.optBoolean("multiRow");
        int databaseConnectionIndex = jsonQuery.optInt("databaseConnectionIndex");
        // make the object
        _query = new Query(inputs, outputs, sql, multiRow, databaseConnectionIndex);
    }

    // look for showLoading
    _showLoading = jsonAction.optBoolean("showLoading");

    // grab any successActions
    JSONArray jsonChildDatabaseActions = jsonAction.optJSONArray("childDatabaseActions");
    // if we had some 
    if (jsonChildDatabaseActions != null) {
        // instantiate collection
        _childDatabaseActions = new ArrayList<Database>();
        // loop them
        for (int i = 0; i < jsonChildDatabaseActions.length(); i++) {
            // get one
            JSONObject jsonChildDatabaseAction = jsonChildDatabaseActions.getJSONObject(i);
            // instantiate and add to collection
            _childDatabaseActions.add(new Database(rapidServlet, jsonChildDatabaseAction));
        }
    }

    // grab any successActions
    JSONArray jsonSuccessActions = jsonAction.optJSONArray("successActions");
    // if we had some instantiate our collection
    if (jsonSuccessActions != null)
        _successActions = Control.getActions(rapidServlet, jsonSuccessActions);

    // grab any errorActions
    JSONArray jsonErrorActions = jsonAction.optJSONArray("errorActions");
    // if we had some instantiate our collection
    if (jsonErrorActions != null)
        _errorActions = Control.getActions(rapidServlet, jsonErrorActions);

}

From source file:com.dianping.lion.util.JsonParser.java

/**
 * ??config?/* ww  w .j  a v  a  2s  . c  o  m*/
 * @param dbContent ?DB?
 * @return
 * @throws Exception
 */
public Map<String, Boolean> getDBAlias(String dbContent) throws Exception {
    Map<String, Boolean> dbAliases = null;
    JSONObject jsonObj = new JSONObject(dbContent);
    String[] names = JSONObject.getNames(jsonObj);
    dbAliases = new HashMap<String, Boolean>();
    a: for (int i = 0; i < names.length; i++) {
        String dsName = names[i];
        if (TIMESTAMP.equals(dsName)) {
            continue;
        } else {
            JSONObject envDSContent = jsonObj.getJSONObject(dsName);
            dsName = "data-source." + dsName;
            String[] envs = JSONObject.getNames(envDSContent);
            for (int j = 0; j < envs.length; j++) {
                if (REMOVED.equals(envs[j])) {
                    dbAliases.put(dsName, true);
                    continue a;
                }
            }
            dbAliases.put(dsName, false);
        }
    }
    return dbAliases;
}