Example usage for org.json JSONArray put

List of usage examples for org.json JSONArray put

Introduction

In this page you can find the example usage for org.json JSONArray put.

Prototype

public JSONArray put(Object value) 

Source Link

Document

Append an object value.

Usage

From source file:com.whizzosoftware.hobson.dto.property.PropertyContainerClassDTO.java

public JSONObject toJSON() {
    JSONObject json = super.toJSON();
    json.put(JSONAttributes.DESCRIPTION_TEMPLATE, descriptionTemplate);
    if (supportedProperties != null) {
        JSONArray array = new JSONArray();
        for (TypedPropertyDTO p : supportedProperties) {
            array.put(p.toJSON());
        }// w ww .  j  a  v a  2 s .c  o  m
        json.put(JSONAttributes.SUPPORTED_PROPERTIES, array);
    }
    return json;
}

From source file:com.soomla.store.domain.data.BalanceDrivenPriceModel.java

/**
 * docs in {@link AbstractPriceModel#toJSONObject()}
 *//*from   w w  w.  j av a2s.  c  o  m*/
@Override
public JSONObject toJSONObject() throws JSONException {
    JSONObject parentJsonObject = super.toJSONObject();
    JSONObject jsonObject = new JSONObject();

    Iterator<?> keys = parentJsonObject.keys();
    while (keys.hasNext()) {
        String key = (String) keys.next();
        jsonObject.put(key, parentJsonObject.get(key));
    }

    JSONArray valuesPerBalance = new JSONArray();
    for (HashMap<String, Integer> currencyValue : mCurrencyValuePerBalance) {
        JSONObject currencyValues = new JSONObject();
        for (String key : currencyValue.keySet()) {
            currencyValues.put(key, currencyValue.get(key));
        }
        valuesPerBalance.put(currencyValues);
    }
    jsonObject.put(JSONConsts.GOOD_PRICE_MODEL_VALUES, valuesPerBalance);

    return jsonObject;
}

From source file:com.mirasense.scanditsdk.plugin.Marshal.java

public static JSONArray createEventArgs(String eventName, JSONObject arg) {
    JSONArray args = new JSONArray();
    args.put(eventName);
    args.put(arg);//from w ww.  ja v  a2  s .co  m
    return args;
}

From source file:com.mirasense.scanditsdk.plugin.Marshal.java

public static JSONArray createEventArgs(String eventName, int arg) {
    JSONArray args = new JSONArray();
    args.put(eventName);
    args.put(arg);//  www  . j a v  a  2s. c  o m
    return args;
}

From source file:com.mirasense.scanditsdk.plugin.Marshal.java

public static JSONArray createEventArgs(String eventName, String arg) {
    JSONArray args = new JSONArray();
    args.put(eventName);
    args.put(arg);//from  ww w .  jav a2s .co m
    return args;
}

From source file:net.algart.simagis.live.json.minimal.SimagisLiveUtils.java

public static JSONObject parseArgs(String... args) {
    final Pattern[] opts = new Pattern[] { Pattern.compile("^--([\\w\\d\\-]++)"),
            Pattern.compile("^-([\\w\\d\\-]++)"), Pattern.compile("^--([\\w\\d\\-]++)=(.*)"),
            Pattern.compile("^-([\\w\\d\\-]++)=(.*)") };
    final JSONObject result = new JSONObject();
    final JSONArray resultArgs = openArray(result, "args");
    final JSONArray resultValues = openArray(result, "values");
    final JSONObject options = openObject(result, "options");
    args: for (final String arg : args) {
        resultArgs.put(arg);
        for (final Pattern opt : opts) {
            final Matcher matcher = opt.matcher(arg);
            if (matcher.matches()) {
                try {
                    switch (matcher.groupCount()) {
                    case 1:
                        options.put(matcher.group(1), true);
                        break;
                    case 2:
                        options.put(matcher.group(1), matcher.group(2));
                        break;
                    }/*ww  w . j  ava2s  . c  om*/
                } catch (JSONException e) {
                    throw new AssertionError(e);
                }
                continue args;
            }
        }
        resultValues.put(arg);
    }
    return result;
}

From source file:net.algart.simagis.live.json.minimal.SimagisLiveUtils.java

private static void putAttribute(JSONArray attributesJson, String type, String caption, Object value,
        boolean advanced) throws JSONException {
    final JSONObject attribute = new JSONObject();
    attribute.put("type", type);
    attribute.put("caption", caption);
    attribute.put("value", value);
    attribute.put("advanced", advanced);
    attributesJson.put(attribute);
}

From source file:com.phonegap.ContactAccessorSdk5.java

/** 
 * This method takes the fields required and search options in order to produce an 
 * array of contacts that matches the criteria provided.
 * @param fields an array of items to be used as search criteria
 * @param options that can be applied to contact searching
 * @return an array of contacts /*www.j ava  2 s  .  c om*/
 */
@Override
public JSONArray search(JSONArray fields, JSONObject options) {
    long totalEnd;
    long totalStart = System.currentTimeMillis();

    // Get the find options
    String searchTerm = "";
    int limit = Integer.MAX_VALUE;
    boolean multiple = true;

    if (options != null) {
        searchTerm = options.optString("filter");
        if (searchTerm.length() == 0) {
            searchTerm = "%";
        } else {
            searchTerm = "%" + searchTerm + "%";
        }
        try {
            multiple = options.getBoolean("multiple");
            if (!multiple) {
                limit = 1;
            }
        } catch (JSONException e) {
            // Multiple was not specified so we assume the default is true.
        }
    } else {
        searchTerm = "%";
    }

    //Log.d(LOG_TAG, "Search Term = " + searchTerm);
    //Log.d(LOG_TAG, "Field Length = " + fields.length());
    //Log.d(LOG_TAG, "Fields = " + fields.toString());

    // Loop through the fields the user provided to see what data should be returned.
    HashMap<String, Boolean> populate = buildPopulationSet(fields);

    // Build the ugly where clause and where arguments for one big query.
    WhereOptions whereOptions = buildWhereClause(fields, searchTerm);

    // Get all the id's where the search term matches the fields passed in.
    Cursor idCursor = mApp.getContentResolver().query(ContactsContract.Data.CONTENT_URI,
            new String[] { ContactsContract.Data.CONTACT_ID }, whereOptions.getWhere(),
            whereOptions.getWhereArgs(), ContactsContract.Data.CONTACT_ID + " ASC");

    // Create a set of unique ids
    //Log.d(LOG_TAG, "ID cursor query returns = " + idCursor.getCount());
    Set<String> contactIds = new HashSet<String>();
    while (idCursor.moveToNext()) {
        contactIds.add(idCursor.getString(idCursor.getColumnIndex(ContactsContract.Data.CONTACT_ID)));
    }
    idCursor.close();

    // Build a query that only looks at ids
    WhereOptions idOptions = buildIdClause(contactIds, searchTerm);

    // Do the id query
    Cursor c = mApp.getContentResolver().query(ContactsContract.Data.CONTENT_URI, null, idOptions.getWhere(),
            idOptions.getWhereArgs(), ContactsContract.Data.CONTACT_ID + " ASC");

    //Log.d(LOG_TAG, "Cursor length = " + c.getCount());

    String contactId = "";
    String rawId = "";
    String oldContactId = "";
    boolean newContact = true;
    String mimetype = "";

    JSONArray contacts = new JSONArray();
    JSONObject contact = new JSONObject();
    JSONArray organizations = new JSONArray();
    JSONArray addresses = new JSONArray();
    JSONArray phones = new JSONArray();
    JSONArray emails = new JSONArray();
    JSONArray ims = new JSONArray();
    JSONArray websites = new JSONArray();
    JSONArray photos = new JSONArray();

    if (c.getCount() > 0) {
        while (c.moveToNext() && (contacts.length() <= (limit - 1))) {
            try {
                contactId = c.getString(c.getColumnIndex(ContactsContract.Data.CONTACT_ID));
                rawId = c.getString(c.getColumnIndex(ContactsContract.Data.RAW_CONTACT_ID));

                // If we are in the first row set the oldContactId
                if (c.getPosition() == 0) {
                    oldContactId = contactId;
                }

                // When the contact ID changes we need to push the Contact object 
                // to the array of contacts and create new objects.
                if (!oldContactId.equals(contactId)) {
                    // Populate the Contact object with it's arrays
                    // and push the contact into the contacts array
                    contacts.put(populateContact(contact, organizations, addresses, phones, emails, ims,
                            websites, photos));

                    // Clean up the objects
                    contact = new JSONObject();
                    organizations = new JSONArray();
                    addresses = new JSONArray();
                    phones = new JSONArray();
                    emails = new JSONArray();
                    ims = new JSONArray();
                    websites = new JSONArray();
                    photos = new JSONArray();

                    // Set newContact to true as we are starting to populate a new contact
                    newContact = true;
                }

                // When we detect a new contact set the ID and display name.
                // These fields are available in every row in the result set returned.
                if (newContact) {
                    newContact = false;
                    contact.put("id", contactId);
                    contact.put("rawId", rawId);
                    contact.put("displayName", c.getString(
                            c.getColumnIndex(ContactsContract.CommonDataKinds.StructuredName.DISPLAY_NAME)));
                }

                // Grab the mimetype of the current row as it will be used in a lot of comparisons
                mimetype = c.getString(c.getColumnIndex(ContactsContract.Data.MIMETYPE));

                if (mimetype.equals(ContactsContract.CommonDataKinds.StructuredName.CONTENT_ITEM_TYPE)
                        && isRequired("name", populate)) {
                    contact.put("name", nameQuery(c));
                } else if (mimetype.equals(ContactsContract.CommonDataKinds.Phone.CONTENT_ITEM_TYPE)
                        && isRequired("phoneNumbers", populate)) {
                    phones.put(phoneQuery(c));
                } else if (mimetype.equals(ContactsContract.CommonDataKinds.Email.CONTENT_ITEM_TYPE)
                        && isRequired("emails", populate)) {
                    emails.put(emailQuery(c));
                } else if (mimetype.equals(ContactsContract.CommonDataKinds.StructuredPostal.CONTENT_ITEM_TYPE)
                        && isRequired("addresses", populate)) {
                    addresses.put(addressQuery(c));
                } else if (mimetype.equals(ContactsContract.CommonDataKinds.Organization.CONTENT_ITEM_TYPE)
                        && isRequired("organizations", populate)) {
                    organizations.put(organizationQuery(c));
                } else if (mimetype.equals(ContactsContract.CommonDataKinds.Im.CONTENT_ITEM_TYPE)
                        && isRequired("ims", populate)) {
                    ims.put(imQuery(c));
                } else if (mimetype.equals(ContactsContract.CommonDataKinds.Note.CONTENT_ITEM_TYPE)
                        && isRequired("note", populate)) {
                    contact.put("note",
                            c.getString(c.getColumnIndex(ContactsContract.CommonDataKinds.Note.NOTE)));
                } else if (mimetype.equals(ContactsContract.CommonDataKinds.Nickname.CONTENT_ITEM_TYPE)
                        && isRequired("nickname", populate)) {
                    contact.put("nickname",
                            c.getString(c.getColumnIndex(ContactsContract.CommonDataKinds.Nickname.NAME)));
                } else if (mimetype.equals(ContactsContract.CommonDataKinds.Website.CONTENT_ITEM_TYPE)
                        && isRequired("urls", populate)) {
                    websites.put(websiteQuery(c));
                } else if (mimetype.equals(ContactsContract.CommonDataKinds.Event.CONTENT_ITEM_TYPE)) {
                    if (ContactsContract.CommonDataKinds.Event.TYPE_BIRTHDAY == c
                            .getInt(c.getColumnIndex(ContactsContract.CommonDataKinds.Event.TYPE))
                            && isRequired("birthday", populate)) {
                        contact.put("birthday", c.getString(
                                c.getColumnIndex(ContactsContract.CommonDataKinds.Event.START_DATE)));
                    }
                } else if (mimetype.equals(ContactsContract.CommonDataKinds.Photo.CONTENT_ITEM_TYPE)
                        && isRequired("photos", populate)) {
                    photos.put(photoQuery(c, contactId));
                }
            } catch (JSONException e) {
                Log.e(LOG_TAG, e.getMessage(), e);
            }

            // Set the old contact ID 
            oldContactId = contactId;
        }

        // Push the last contact into the contacts array
        if (contacts.length() < limit) {
            contacts.put(
                    populateContact(contact, organizations, addresses, phones, emails, ims, websites, photos));
        }
    }
    c.close();

    totalEnd = System.currentTimeMillis();
    Log.d(LOG_TAG, "Total time = " + (totalEnd - totalStart));
    return contacts;
}

From source file:com.grarak.kerneladiutor.database.Provider.java

public void delete(int position) {
    JSONArray jsonArray = new JSONArray();
    try {//from  w  w  w.  j a va  2  s  .  co  m
        for (int i = 0; i < length(); i++) {
            if (i != position) {
                jsonArray.put(mDatabaseItems.getJSONObject(i));
            }
        }
        mDatabaseItems = jsonArray;
    } catch (JSONException e) {
        e.printStackTrace();
    }
}

From source file:br.ufmt.ic.pawii.util.Estados.java

public static String getEstados(String estado, String nome, String email) {
    try {/*w  w w.j ava 2 s. com*/

        String strCidades;

        Statement stm;

        Connection con = ConnBD.getConnection();

        if (con == null) {
            throw new SQLException("Erro conectando");
        }

        stm = con.createStatement();

        String sql = "SELECT codigo,municipio FROM municipios" + " WHERE uf='" + estado
                + "' ORDER BY municipio ASC";

        ResultSet rs = stm.executeQuery(sql);

        JSONArray cidades = new JSONArray();

        while (rs.next()) {

            String codigo = rs.getString(1);
            String cidade = rs.getString(2);

            JSONObject jsonCidade = new JSONObject();
            jsonCidade.put("codigo", codigo);
            jsonCidade.put("nome", cidade);
            cidades.put(jsonCidade);
        }

        JSONObject jsonRetorno = new JSONObject();
        jsonRetorno.put("cidades", cidades);
        jsonRetorno.put("seuNome", nome);
        jsonRetorno.put("seuEmail", email);

        strCidades = jsonRetorno.toString();

        return strCidades;

    } catch (SQLException ex) {
        System.out.println("Error: " + ex.getMessage());
    } catch (Exception e) {
        System.out.println("Error: " + e.getMessage());
    }

    return null;
}