Example usage for org.json JSONArray getString

List of usage examples for org.json JSONArray getString

Introduction

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

Prototype

public String getString(int index) throws JSONException 

Source Link

Document

Get the string associated with an index.

Usage

From source file:nl.spellenclubeindhoven.dominionshuffle.LoadSaveActivity.java

public Slot[] getSlotList() {
    Slot[] result = new Slot[NUM_OF_SLOTS];
    int slotCount = 0;

    try {/*w  w  w  . j a va  2  s.  c  o  m*/
        JSONArray jsonArray = new JSONArray(DataReader.readStringFromFile(this, "slots.json"));
        for (int i = 0; i < jsonArray.length() && i < NUM_OF_SLOTS; i++) {
            if (jsonArray.isNull(i))
                result[i] = new Slot();
            else
                result[i] = new Slot(jsonArray.getString(i));
            slotCount++;
        }
    } catch (JSONException ignore) {
    }

    for (int i = slotCount; i < NUM_OF_SLOTS; i++) {
        result[i] = new Slot();
    }

    return result;
}

From source file:com.jennifer.ui.util.TimeUtil.java

public static String format(long d, JSONArray step) {

    String type = step.getString(0);
    String format = "";

    if (Time.YEARS.equals(type)) {
        format = "yyyy";
    } else if (Time.MONTHS.equals(type)) {
        format = "yyyy-MM";
    } else if (Time.DAYS.equals(type)) {
        format = "MM-dd";
    } else if (Time.HOURS.equals(type)) {
        format = "HH";
    } else if (Time.MINUTES.equals(type)) {
        format = "HH:mm";
    } else if (Time.SECONDS.equals(type)) {
        format = "HH:mm:ss";
    } else if (Time.MILLISECONDS.equals(type)) {
        format = "ss";
    }//from   ww  w  . ja v a 2 s.com

    return format(get(d), format);
}

From source file:com.fuse.billing.android.IabHelper.java

public String querySkuDetailsAsJsonString(String itemType, String skuArrayJsonString) throws IabException {
    try {/* ww  w  .  ja  v  a 2  s  .  com*/
        JSONArray skuJsonArray = new JSONArray(skuArrayJsonString);
        List<String> skuList = new ArrayList<String>();
        for (int i = 0; i < skuJsonArray.length(); i++) {
            skuList.add(skuJsonArray.getString(i));
        }
        List<SkuDetails> skuDetailsList = new ArrayList<SkuDetails>();
        int r = querySkuDetails(itemType, skuList, skuDetailsList);
        if (r != BILLING_RESPONSE_RESULT_OK) {
            throw new IabException(r, "Error querying sku details");
        }
        JSONArray skuDetailsJsonArray = new JSONArray();
        for (SkuDetails skuDetails : skuDetailsList) {
            skuDetailsJsonArray.put(skuDetails.toJSON());
        }
        return skuDetailsJsonArray.toString();
    } catch (RemoteException e) {
        throw new IabException(IABHELPER_REMOTE_EXCEPTION, "Remote exception while refreshing inventory.", e);
    } catch (JSONException e) {
        throw new IabException(IABHELPER_BAD_RESPONSE,
                "Error parsing JSON response while querying sku details.", e);
    }
}

From source file:com.snappy.couchdb.CouchDBHelper.java

/**
 * Parse a single user JSON object/*from ww w.ja  va 2  s . c  o  m*/
 * 
 * @param json
 * @return
 * @throws JSONException
 */
public static User parseSingleUserObject(JSONObject json) throws JSONException {
    User user = null;
    ArrayList<String> contactsIds = new ArrayList<String>();

    if (json != null) {

        if (json.has(Const.ERROR)) {
            appLogout(null, false, isInvalidToken(json));
            return null;
        }

        JSONArray rows = json.getJSONArray(Const.ROWS);
        JSONObject row = rows.getJSONObject(0);
        JSONObject userJson = row.getJSONObject(Const.VALUE);

        user = sGsonExpose.fromJson(userJson.toString(), User.class);

        if (userJson.has(Const.FAVORITE_GROUPS)) {
            JSONArray favorite_groups = userJson.getJSONArray(Const.FAVORITE_GROUPS);

            List<String> groups = new ArrayList<String>();

            for (int i = 0; i < favorite_groups.length(); i++) {
                groups.add(favorite_groups.getString(i));
            }

            user.setGroupIds(groups);
        }

        if (userJson.has(Const.CONTACTS)) {
            JSONArray contacts = userJson.getJSONArray(Const.CONTACTS);

            for (int i = 0; i < contacts.length(); i++) {
                contactsIds.add(contacts.getString(i));
            }

            user.setContactIds(contactsIds);
        }
    }

    return user;
}

From source file:com.snappy.couchdb.CouchDBHelper.java

/**
* Parse a single user JSON object//from   ww w  . j  av  a2s. c  om
* 
* @param json
* @return
* @throws JSONException
* @throws SpikaException 
*/
public static User parseSingleUserObjectWithoutRowParam(JSONObject userJson)
        throws JSONException, SpikaException {
    User user = null;
    ArrayList<String> contactsIds = new ArrayList<String>();

    if (userJson != null) {

        if (userJson.length() == 0) {
            return null;
        }

        if (userJson.has(Const.ERROR)) {
            appLogout(null, false, isInvalidToken(userJson));
            throw new SpikaException(ConnectionHandler.getError(userJson));
        }

        user = sGsonExpose.fromJson(userJson.toString(), User.class);

        if (userJson.has(Const.FAVORITE_GROUPS)) {
            JSONArray favorite_groups = userJson.getJSONArray(Const.FAVORITE_GROUPS);

            List<String> groups = new ArrayList<String>();

            for (int i = 0; i < favorite_groups.length(); i++) {
                groups.add(favorite_groups.getString(i));
            }

            user.setGroupIds(groups);
        }

        if (userJson.has(Const.CONTACTS)) {
            JSONArray contacts = userJson.getJSONArray(Const.CONTACTS);

            for (int i = 0; i < contacts.length(); i++) {
                contactsIds.add(contacts.getString(i));
            }

            user.setContactIds(contactsIds);
        }
    }

    return user;
}

From source file:com.snappy.couchdb.CouchDBHelper.java

/**
 * Parse multi JSON objects of type user
 * /*from   w w  w.j a  v  a2s . c o m*/
 * @param json
 * @return
 * @throws JSONException 
 */
public static List<User> parseMultiUserObjects(JSONObject json) throws JSONException {

    List<User> users = null;
    ArrayList<String> contactsIds = new ArrayList<String>();

    if (json != null) {

        if (json.has(Const.ERROR)) {
            appLogout(null, false, isInvalidToken(json));
            return null;
        }

        users = new ArrayList<User>();

        // Get the element that holds the users ( JSONArray )
        JSONArray rows = json.getJSONArray(Const.ROWS);

        for (int i = 0; i < rows.length(); i++) {

            JSONObject row = rows.getJSONObject(i);
            JSONObject userJson = row.getJSONObject(Const.VALUE);

            User user = new User();

            user = sGsonExpose.fromJson(userJson.toString(), User.class);

            if (userJson.has(Const.CONTACTS)) {

                JSONArray contacts = userJson.getJSONArray(Const.CONTACTS);

                for (int j = 0; j < contacts.length(); j++) {
                    contactsIds.add(contacts.getString(j));
                }

                user.setContactIds(contactsIds);
            }

            if (userJson.has(Const.FAVORITE_GROUPS)) {
                JSONArray favorite_groups = userJson.getJSONArray(Const.FAVORITE_GROUPS);

                List<String> groups = new ArrayList<String>();

                for (int k = 0; k < favorite_groups.length(); k++) {
                    groups.add(favorite_groups.getString(k));
                }

                user.setGroupIds(groups);
            }

            users.add(user);
        }
    }

    return users;
}

From source file:com.snappy.couchdb.CouchDBHelper.java

/**
 * Parse multi JSON objects of type user for search users
 * //from   w w w.j av  a 2s  . co  m
 * @param json
 * @return
 * @throws JSONException 
 */
public static List<User> parseSearchUsersResult(JSONArray jsonArray) throws JSONException {

    List<User> users = null;
    ArrayList<String> contactsIds = new ArrayList<String>();

    if (jsonArray != null) {

        users = new ArrayList<User>();

        // Get the element that holds the users ( JSONArray )

        for (int i = 0; i < jsonArray.length(); i++) {

            JSONObject userJson = jsonArray.getJSONObject(i);

            User user = new User();

            user = sGsonExpose.fromJson(userJson.toString(), User.class);

            if (userJson.has(Const.CONTACTS)) {

                JSONArray contacts = userJson.getJSONArray(Const.CONTACTS);

                for (int j = 0; j < contacts.length(); j++) {
                    contactsIds.add(contacts.getString(j));
                }

                user.setContactIds(contactsIds);
            }

            if (userJson.has(Const.FAVORITE_GROUPS)) {
                JSONArray favorite_groups = userJson.getJSONArray(Const.FAVORITE_GROUPS);

                List<String> groups = new ArrayList<String>();

                for (int k = 0; k < favorite_groups.length(); k++) {
                    groups.add(favorite_groups.getString(k));
                }

                user.setGroupIds(groups);
            }

            users.add(user);
        }
    }

    return users;
}

From source file:com.snappy.couchdb.CouchDBHelper.java

/**
 * Parse user JSON objects from get user contacts call
 * /*from w ww.  j ava  2  s.c o m*/
 * @param json
 * @return
 * @throws JSONException 
 */
public static List<User> parseUserContacts(JSONObject json) throws JSONException {

    List<User> users = null;

    if (json != null) {

        if (json.has(Const.ERROR)) {
            appLogout(null, false, isInvalidToken(json));
            return null;
        }

        users = new ArrayList<User>();

        // Get the element that holds the users ( JSONArray )
        JSONArray rows = json.getJSONArray(Const.ROWS);

        for (int i = 0; i < rows.length(); i++) {

            JSONObject row = rows.getJSONObject(i);
            if (!row.isNull(Const.DOC)) {
                JSONObject userJson = row.getJSONObject(Const.DOC);

                User user = new User();

                user = sGsonExpose.fromJson(userJson.toString(), User.class);

                if (userJson.has(Const.FAVORITE_GROUPS)) {
                    JSONArray favorite_groups = userJson.getJSONArray(Const.FAVORITE_GROUPS);

                    List<String> groups = new ArrayList<String>();

                    for (int z = 0; z < favorite_groups.length(); z++) {
                        groups.add(favorite_groups.getString(z));
                    }

                    user.setGroupIds(groups);
                }

                if (userJson.has(Const.CONTACTS)) {
                    JSONArray contacts = userJson.getJSONArray(Const.CONTACTS);

                    List<String> contactsIds = new ArrayList<String>();

                    for (int j = 0; j < contacts.length(); j++) {
                        contactsIds.add(contacts.getString(j));
                    }
                    user.setContactIds(contactsIds);
                }

                users.add(user);
            }
        }
    }

    return users;
}

From source file:com.corumgaz.mobilsayac.VoiceRecognizer.VoiceRecognizer.java

/**
  * Fire an intent to start the speech recognition activity.
  *//from   ww  w  .ja  v a  2  s  . com
  * @param args Argument array with the following string args: [req code][number of matches][prompt string]
  */
private void startSpeechRecognitionActivity(JSONArray args) {
    int maxMatches = 0;
    String prompt = "";
    String language = Locale.getDefault().toString();

    try {
        if (args.length() > 0) {
            // Maximum number of matches, 0 means the recognizer decides
            String temp = args.getString(0);
            maxMatches = Integer.parseInt(temp);
        }
        if (args.length() > 1) {
            // Optional text prompt
            prompt = args.getString(1);
        }
        if (args.length() > 2) {
            // Optional language specified
            language = args.getString(2);
        }
    } catch (Exception e) {
        Log.e(LOG_TAG, String.format("startSpeechRecognitionActivity exception: %s", e.toString()));
    }

    // Create the intent and set parameters
    Intent intent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
    intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL, RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);

    intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE, language);

    if (maxMatches > 0)
        intent.putExtra(RecognizerIntent.EXTRA_MAX_RESULTS, maxMatches);
    if (!prompt.equals(""))
        intent.putExtra(RecognizerIntent.EXTRA_PROMPT, prompt);
    cordova.startActivityForResult(this, intent, REQUEST_CODE);
}

From source file:com.phonegap.plugins.FloatingActivityPlugin.FloatingActivityPlugin.java

@Override
public boolean execute(String action, JSONArray args, CallbackContext callbackContext) throws JSONException {
    // TODO Auto-generated method stub
    Context context = cordova.getActivity().getApplicationContext();
    PackageManager pm = cordova.getActivity().getPackageManager();
    _checkerService = null;/*from ww w . j a  v a 2 s. c  o m*/
    String packageName;
    Boolean result = true;

    try {
        packageName = args.getString(0);
    } catch (JSONException e) {
        return false;
    }
    result = launch(pm, context, packageName, context);
    if (result == true) {
        callbackContext.success(0);
        return true;
    } else {
        callbackContext.error(0);
        return false;
    }
}