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:org.apache.cordova.contacts.ContactAccessor.java

/**
 * Create a hash map of what data needs to be populated in the Contact object
 * @param fields the list of fields to populate
 * @return the hash map of required data
 */// www  .j  a v  a 2 s. co  m
protected HashMap<String, Boolean> buildPopulationSet(JSONObject options) {
    HashMap<String, Boolean> map = new HashMap<String, Boolean>();

    String key;
    try {
        JSONArray desiredFields = null;
        if (options != null && options.has("desiredFields")) {
            desiredFields = options.getJSONArray("desiredFields");
        }
        if (desiredFields == null || desiredFields.length() == 0) {
            map.put("displayName", true);
            map.put("name", true);
            map.put("nickname", true);
            map.put("phoneNumbers", true);
            map.put("emails", true);
            map.put("addresses", true);
            map.put("ims", true);
            map.put("organizations", true);
            map.put("birthday", true);
            map.put("note", true);
            map.put("urls", true);
            map.put("photos", true);
            map.put("categories", true);
        } else {
            for (int i = 0; i < desiredFields.length(); i++) {
                key = desiredFields.getString(i);
                if (key.startsWith("displayName")) {
                    map.put("displayName", true);
                } else if (key.startsWith("name")) {
                    map.put("displayName", true);
                    map.put("name", true);
                } else if (key.startsWith("nickname")) {
                    map.put("nickname", true);
                } else if (key.startsWith("phoneNumbers")) {
                    map.put("phoneNumbers", true);
                } else if (key.startsWith("emails")) {
                    map.put("emails", true);
                } else if (key.startsWith("addresses")) {
                    map.put("addresses", true);
                } else if (key.startsWith("ims")) {
                    map.put("ims", true);
                } else if (key.startsWith("organizations")) {
                    map.put("organizations", true);
                } else if (key.startsWith("birthday")) {
                    map.put("birthday", true);
                } else if (key.startsWith("note")) {
                    map.put("note", true);
                } else if (key.startsWith("urls")) {
                    map.put("urls", true);
                } else if (key.startsWith("photos")) {
                    map.put("photos", true);
                } else if (key.startsWith("categories")) {
                    map.put("categories", true);
                }
            }
        }
    } catch (JSONException e) {
        Log.e(LOG_TAG, e.getMessage(), e);
    }
    return map;
}

From source file:com.cranberrygame.phonegap.plugin.OptionsMenu.java

@Override
public boolean execute(String action, JSONArray args, CallbackContext callbackContext) {
    PluginResult result = null;/*from  www  .  j  av a 2  s.c  o  m*/

    try {
        //args.length()
        //args.getString(0)
        //args.getString(1)
        //args.Int(0)
        //args.Int(1)
        //args.getBoolean(0)
        //args.getBoolean(1)

        if (action.equals("setMenus")) {
            //Activity activity=cordova.getActivity();
            //webView         
            String menus = args.getString(0);
            Log.d("Menu", menus);

            this.menus = menus;

            result = new PluginResult(PluginResult.Status.OK);
        } else if (action.equals("showMenus")) {
            //Activity activity=cordova.getActivity();
            //webView         

            cordova.getActivity().openOptionsMenu();

            result = new PluginResult(PluginResult.Status.OK);
        } else {
            result = new PluginResult(PluginResult.Status.INVALID_ACTION);
        }
    } catch (JSONException e) {
        result = new PluginResult(PluginResult.Status.JSON_EXCEPTION);
    }

    callbackContext.sendPluginResult(result);
    return true;
}

From source file:org.adminmap.core.ubuntu.NetworkConfiguration.java

@Override
public void fromJSONString(String json) {

    JSONObject jsonObject = new JSONObject(json);

    this.ipAddress = jsonObject.getString("ipAddress");
    this.subnetMask = jsonObject.getString("subnetMask");
    this.broadcastAddress = jsonObject.getString("broadcastAddress");
    this.defaultGateway = jsonObject.getString("defaultGateway");

    JSONArray jsonArray = jsonObject.getJSONArray("nameservers");
    this.nameservers = new String[jsonArray.length()];

    for (int i = 0; i < jsonArray.length(); i++)
        this.nameservers[i] = (jsonArray.getString(i));

}

From source file:com.phonegap.ContactAccessor.java

/**
 * Create a hash map of what data needs to be populated in the Contact object
 * @param fields the list of fields to populate
 * @return the hash map of required data
 *///  w  w w  .  jav a2 s. c om
protected HashMap<String, Boolean> buildPopulationSet(JSONArray fields) {
    HashMap<String, Boolean> map = new HashMap<String, Boolean>();

    String key;
    try {
        for (int i = 0; i < fields.length(); i++) {
            key = fields.getString(i);
            if (key.startsWith("displayName")) {
                map.put("displayName", true);
            } else if (key.startsWith("name")) {
                map.put("name", true);
            } else if (key.startsWith("nickname")) {
                map.put("nickname", true);
            } else if (key.startsWith("phoneNumbers")) {
                map.put("phoneNumbers", true);
            } else if (key.startsWith("emails")) {
                map.put("emails", true);
            } else if (key.startsWith("addresses")) {
                map.put("addresses", true);
            } else if (key.startsWith("ims")) {
                map.put("ims", true);
            } else if (key.startsWith("organizations")) {
                map.put("organizations", true);
            } else if (key.startsWith("birthday")) {
                map.put("birthday", true);
            } else if (key.startsWith("anniversary")) {
                map.put("anniversary", true);
            } else if (key.startsWith("note")) {
                map.put("note", true);
            } else if (key.startsWith("relationships")) {
                map.put("relationships", true);
            } else if (key.startsWith("urls")) {
                map.put("urls", true);
            } else if (key.startsWith("photos")) {
                map.put("photos", true);
            }
        }
    } catch (JSONException e) {
        Log.e(LOG_TAG, e.getMessage(), e);
    }
    return map;
}

From source file:co.marcin.novaguilds.util.StringUtils.java

public static List<String> jsonToList(String json) {
    json = "{array:" + json + "}";
    JSONObject obj = new JSONObject(json);
    JSONArray arr = obj.optJSONArray("array");

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

    for (int i = 0; i < arr.length(); i++) {
        list.add(arr.getString(i));
    }/*from w  ww  . jav  a 2 s  .  c  o  m*/

    return list;
}

From source file:com.cranberrygame.phonegap.plugin.Util.java

@Override
public boolean execute(String action, JSONArray args, CallbackContext callbackContext) throws JSONException {
    PluginResult result = null;//from   w  w w  .  ja v a 2s.  c  o m

    if (GooglePlayServicesUtil.isGooglePlayServicesAvailable(this.cordova.getActivity()) != 0) {
        Log.e(LOG_TAG, "Google Play Services are unavailable");
        callbackContext.error("Unavailable");
        return true;
    } else {
        Log.d(LOG_TAG, "** Google Play Services are available **");
    }

    //args.length()
    //args.getString(0)
    //args.getString(1)
    //args.getInt(0)
    //args.getInt(1)
    //args.getBoolean(0)
    //args.getBoolean(1)

    if (action.equals("setUp")) {
        //Activity activity=cordova.getActivity();
        //webView
        //            

        final CallbackContext delayedCC = callbackContext;
        cordova.getActivity().runOnUiThread(new Runnable() {
            @Override
            public void run() {
                _setUp();

                PluginResult pr = new PluginResult(PluginResult.Status.OK);
                //pr.setKeepCallback(true);
                delayedCC.sendPluginResult(pr);
                //PluginResult pr = new PluginResult(PluginResult.Status.ERROR);
                //pr.setKeepCallback(true);
                //delayedCC.sendPluginResult(pr);               
            }
        });

        return true;
    } else if (action.equals("login")) {
        //Activity activity=cordova.getActivity();
        //webView
        //            

        loginCC = callbackContext;

        final CallbackContext delayedCC = callbackContext;
        cordova.getActivity().runOnUiThread(new Runnable() {
            @Override
            public void run() {

                if (getGameHelper().isSignedIn()) {
                    //PluginResult pr = new PluginResult(PluginResult.Status.OK);
                    //pr.setKeepCallback(true);
                    //delayedCC.sendPluginResult(pr);
                    PluginResult pr = new PluginResult(PluginResult.Status.ERROR, "Already logged in");
                    //pr.setKeepCallback(true);
                    delayedCC.sendPluginResult(pr);
                } else {
                    _login();
                }
            }
        });

        return true;
    } else if (action.equals("logout")) {
        //Activity activity=cordova.getActivity();
        //webView
        //            

        final CallbackContext delayedCC = callbackContext;
        cordova.getActivity().runOnUiThread(new Runnable() {
            @Override
            public void run() {
                if (getGameHelper().isSignedIn()) {
                    _logout();

                    PluginResult pr = new PluginResult(PluginResult.Status.OK);
                    //pr.setKeepCallback(true);
                    delayedCC.sendPluginResult(pr);
                    //PluginResult pr = new PluginResult(PluginResult.Status.ERROR);
                    //pr.setKeepCallback(true);
                    //delayedCC.sendPluginResult(pr);                  
                } else {
                    //PluginResult pr = new PluginResult(PluginResult.Status.OK);
                    //pr.setKeepCallback(true);
                    //delayedCC.sendPluginResult(pr);
                    PluginResult pr = new PluginResult(PluginResult.Status.ERROR, "Already logged out");
                    //pr.setKeepCallback(true);
                    delayedCC.sendPluginResult(pr);
                }
            }
        });

        return true;
    } else if (action.equals("getPlayerImage")) {
        //Activity activity=cordova.getActivity();
        //webView
        //            

        getPlayerImageCC = callbackContext;

        final CallbackContext delayedCC = callbackContext;
        cordova.getActivity().runOnUiThread(new Runnable() {
            @Override
            public void run() {
                if (getGameHelper().isSignedIn()) {
                    _getPlayerImage();
                } else {
                    //PluginResult pr = new PluginResult(PluginResult.Status.OK);
                    //pr.setKeepCallback(true);
                    //delayedCC.sendPluginResult(pr);
                    PluginResult pr = new PluginResult(PluginResult.Status.ERROR, "Not logged in");
                    //pr.setKeepCallback(true);
                    delayedCC.sendPluginResult(pr);
                }
            }
        });

        return true;
    } else if (action.equals("getPlayerScore")) {
        //Activity activity=cordova.getActivity();
        //webView
        //            
        final String leaderboardId = args.getString(0);
        Log.d(LOG_TAG, String.format("%s", leaderboardId));

        getPlayerScoreCC = callbackContext;

        final CallbackContext delayedCC = callbackContext;
        cordova.getActivity().runOnUiThread(new Runnable() {
            @Override
            public void run() {
                if (getGameHelper().isSignedIn()) {
                    _getPlayerScore(leaderboardId);
                } else {
                    //PluginResult pr = new PluginResult(PluginResult.Status.OK);
                    //pr.setKeepCallback(true);
                    //delayedCC.sendPluginResult(pr);
                    PluginResult pr = new PluginResult(PluginResult.Status.ERROR, "Not logged in");
                    //pr.setKeepCallback(true);
                    delayedCC.sendPluginResult(pr);
                }
            }
        });

        return true;
    } else if (action.equals("submitScore")) {
        //Activity activity=cordova.getActivity();
        //webView
        //
        final String leaderboardId = args.getString(0);
        Log.d(LOG_TAG, String.format("%s", leaderboardId));
        final int score = args.getInt(1);
        Log.d(LOG_TAG, String.format("%d", score));

        submitScoreCC = callbackContext;

        final CallbackContext delayedCC = callbackContext;
        cordova.getActivity().runOnUiThread(new Runnable() {
            @Override
            public void run() {
                if (getGameHelper().isSignedIn()) {
                    _submitScore(leaderboardId, score);
                } else {
                    //PluginResult pr = new PluginResult(PluginResult.Status.OK);
                    //pr.setKeepCallback(true);
                    //delayedCC.sendPluginResult(pr);
                    PluginResult pr = new PluginResult(PluginResult.Status.ERROR, "Not logged in");
                    //pr.setKeepCallback(true);
                    delayedCC.sendPluginResult(pr);
                }
            }
        });

        return true;
    } else if (action.equals("showLeaderboard")) {
        //Activity activity=cordova.getActivity();
        //webView
        //
        final String leaderboardId = args.getString(0);
        Log.d(LOG_TAG, String.format("%s", leaderboardId));

        final CallbackContext delayedCC = callbackContext;
        cordova.getActivity().runOnUiThread(new Runnable() {
            @Override
            public void run() {
                if (getGameHelper().isSignedIn()) {
                    _showLeaderboard(leaderboardId);

                    PluginResult pr = new PluginResult(PluginResult.Status.OK);
                    //pr.setKeepCallback(true);
                    delayedCC.sendPluginResult(pr);
                    //PluginResult pr = new PluginResult(PluginResult.Status.ERROR);
                    //pr.setKeepCallback(true);
                    //delayedCC.sendPluginResult(pr);                  
                } else {
                    //PluginResult pr = new PluginResult(PluginResult.Status.OK);
                    //pr.setKeepCallback(true);
                    //delayedCC.sendPluginResult(pr);
                    PluginResult pr = new PluginResult(PluginResult.Status.ERROR, "Not logged in");
                    //pr.setKeepCallback(true);
                    delayedCC.sendPluginResult(pr);
                }
            }
        });

        return true;
    } else if (action.equals("unlockAchievement")) {
        //Activity activity=cordova.getActivity();
        //webView
        //
        final String achievementId = args.getString(0);
        Log.d(LOG_TAG, String.format("%s", achievementId));

        unlockAchievementCC = callbackContext;

        final CallbackContext delayedCC = callbackContext;
        cordova.getActivity().runOnUiThread(new Runnable() {
            @Override
            public void run() {
                if (getGameHelper().isSignedIn()) {
                    _unlockAchievement(achievementId);
                } else {
                    //PluginResult pr = new PluginResult(PluginResult.Status.OK);
                    //pr.setKeepCallback(true);
                    //delayedCC.sendPluginResult(pr);
                    PluginResult pr = new PluginResult(PluginResult.Status.ERROR, "Not logged in");
                    //pr.setKeepCallback(true);
                    delayedCC.sendPluginResult(pr);
                }
            }
        });

        return true;
    } else if (action.equals("incrementAchievement")) {
        //Activity activity=cordova.getActivity();
        //webView
        //
        final String achievementId = args.getString(0);
        Log.d(LOG_TAG, String.format("%s", achievementId));
        final int stepsOrPercent = args.getInt(1);
        Log.d(LOG_TAG, String.format("%d", stepsOrPercent));

        incrementAchievementCC = callbackContext;

        final CallbackContext delayedCC = callbackContext;
        cordova.getActivity().runOnUiThread(new Runnable() {
            @Override
            public void run() {
                if (getGameHelper().isSignedIn()) {
                    _incrementAchievement(achievementId, stepsOrPercent);
                } else {
                    //PluginResult pr = new PluginResult(PluginResult.Status.OK);
                    //pr.setKeepCallback(true);
                    //delayedCC.sendPluginResult(pr);
                    PluginResult pr = new PluginResult(PluginResult.Status.ERROR, "Not logged in");
                    //pr.setKeepCallback(true);
                    delayedCC.sendPluginResult(pr);
                }
            }
        });

        return true;
    } else if (action.equals("showAchievements")) {
        //Activity activity=cordova.getActivity();
        //webView
        //

        final CallbackContext delayedCC = callbackContext;
        cordova.getActivity().runOnUiThread(new Runnable() {
            @Override
            public void run() {
                if (getGameHelper().isSignedIn()) {
                    _showAchievements();

                    PluginResult pr = new PluginResult(PluginResult.Status.OK);
                    //pr.setKeepCallback(true);
                    delayedCC.sendPluginResult(pr);
                    //PluginResult pr = new PluginResult(PluginResult.Status.ERROR);
                    //pr.setKeepCallback(true);
                    //delayedCC.sendPluginResult(pr);                  
                } else {
                    //PluginResult pr = new PluginResult(PluginResult.Status.OK);
                    //pr.setKeepCallback(true);
                    //delayedCC.sendPluginResult(pr);
                    PluginResult pr = new PluginResult(PluginResult.Status.ERROR, "Not logged in");
                    //pr.setKeepCallback(true);
                    delayedCC.sendPluginResult(pr);
                }
            }
        });

        return true;
    } else if (action.equals("resetAchievements")) {
        //Activity activity=cordova.getActivity();
        //webView
        //            

        final CallbackContext delayedCC = callbackContext;
        cordova.getActivity().runOnUiThread(new Runnable() {
            @Override
            public void run() {
                _resetAchievements();

                PluginResult pr = new PluginResult(PluginResult.Status.OK);
                //pr.setKeepCallback(true);
                delayedCC.sendPluginResult(pr);
                //PluginResult pr = new PluginResult(PluginResult.Status.ERROR);
                //pr.setKeepCallback(true);
                //delayedCC.sendPluginResult(pr);               
            }
        });

        return true;
    }

    return false; // Returning false results in a "MethodNotFound" error.   
}

From source file:src.Parser.java

public static void main(String[] args) throws JSONException {
    HttpQuery http = new HttpQuery();
    String response = "";
    try {//from ww w . j  a  va  2 s . c  o  m
        response = http.send(
                "http://router.project-osrm.org/viaroute?loc=50.4423,30.5298&loc=50.4433,30.5151&z=0&instructions=true&geometry=false");
    } catch (Exception ex) {
        Logger.getLogger(Parser.class.getName()).log(Level.SEVERE, null, ex);
    }
    JSONObject obj = new JSONObject(response);
    JSONArray arr = obj.getJSONArray("route_instructions");
    JSONArray arr2;
    for (int i = 0; i < arr.length(); i++) {
        arr2 = arr.getJSONArray(i);
        String str = arr2.getString(0) + ", " + arr2.getString(1) + ", " + arr2.getInt(2) + ", "
                + arr2.getInt(3) + ", " + arr2.getInt(4);
        System.out.println(str);

        System.out.println();
    }
    System.out.println();
    System.out.println(response);
}

From source file:com.tweetlanes.android.core.App.java

public void onPostSignIn(TwitterUser user, String oAuthToken, String oAuthSecret,
        SocialNetConstant.Type oSocialNetType) {

    if (user != null) {

        try {//from  w  w w. j  a  v  a  2  s  . c om

            final Editor edit = mPreferences.edit();
            String userIdAsString = Long.toString(user.getId());

            AccountDescriptor account = new AccountDescriptor(this, user, oAuthToken, oAuthSecret,
                    oSocialNetType, user.getProfileImageUrl(TwitterManager.ProfileImageSize.BIGGER));
            edit.putString(getAccountDescriptorKey(user.getId()), account.toString());

            String accountIndices = mPreferences.getString(SharedPreferencesConstants.ACCOUNT_INDICES, null);
            JSONArray jsonArray;

            if (accountIndices == null) {
                jsonArray = new JSONArray();
                jsonArray.put(0, user.getId());
                mAccounts.add(account);
            } else {
                jsonArray = new JSONArray(accountIndices);
                boolean exists = false;
                for (int i = 0; i < jsonArray.length(); i++) {
                    String c = jsonArray.getString(i);
                    if (c.compareTo(userIdAsString) == 0) {
                        exists = true;
                        mAccounts.set(i, account);
                        break;
                    }
                }

                if (!exists) {
                    jsonArray.put(userIdAsString);
                    mAccounts.add(account);
                }
            }

            accountIndices = jsonArray.toString();
            edit.putString(SharedPreferencesConstants.ACCOUNT_INDICES, accountIndices);

            edit.commit();

            setCurrentAccount(user.getId());

        } catch (JSONException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        updateTwitterAccountCount();
        if (TwitterManager.get().getSocialNetType() == oSocialNetType) {
            TwitterManager.get().setOAuthTokenWithSecret(oAuthToken, oAuthSecret, true);
        } else {
            TwitterManager.initModule(oSocialNetType,
                    oSocialNetType == SocialNetConstant.Type.Twitter ? ConsumerKeyConstants.TWITTER_CONSUMER_KEY
                            : ConsumerKeyConstants.APPDOTNET_CONSUMER_KEY,
                    oSocialNetType == SocialNetConstant.Type.Twitter
                            ? ConsumerKeyConstants.TWITTER_CONSUMER_SECRET
                            : ConsumerKeyConstants.APPDOTNET_CONSUMER_SECRET,
                    oAuthToken, oAuthSecret, getCurrentAccountKey(), mConnectionStatusCallbacks);
        }
    }
}

From source file:com.trellmor.berrytube.Poll.java

/**
 * Constructs a <code>Poll</code> from a <code>JSONObject<code>
 * /*from  w  w  w.j av a  2 s  . c o m*/
 * @param poll <code>JSONObject<code> containing the poll data
 * @throws JSONException
 */
public Poll(JSONObject poll) throws JSONException {
    mTitle = poll.getString("title");
    mCreator = poll.getString("title");
    mObscure = poll.getBoolean("obscure");

    JSONArray options = poll.getJSONArray("options");
    for (int i = 0; i < options.length(); i++) {
        mOptions.add(options.getString(i));
    }

    JSONArray votes = poll.getJSONArray("votes");
    for (int i = 0; i < votes.length(); i++) {
        mVotes.add(votes.optInt(i, -1));
    }
}

From source file:br.unicamp.cst.behavior.bn.Behavior.java

/**
 *  Checks if there is a current behavior proposition in working storage that is using the resources this behavior needs
 * @return if there is conflict of resources
 *//*from w  w  w  .  jav  a 2s.co m*/
private boolean resourceConflict() {//TODO must develop this idea further
    boolean resourceConflict = false;
    ArrayList<MemoryObject> allOfType = new ArrayList<MemoryObject>();

    if (ws != null)
        allOfType.addAll(ws.getAllOfType("BEHAVIOR_PROPOSITION"));

    ArrayList<String> usedResources = new ArrayList<String>();

    if (allOfType != null) {

        for (MemoryObject bp : allOfType) {
            try {
                JSONObject jsonBp = new JSONObject(bp.getI());
                //System.out.println("=======> bp.getInfo(): "+bp.getInfo());
                boolean performed = jsonBp.getBoolean("PERFORMED");
                if (!performed) {//otherwise it is not consuming those resources

                    JSONArray resourceList = jsonBp.getJSONArray("RESOURCELIST");

                    for (int i = 0; i < resourceList.length(); i++) {
                        usedResources.add(resourceList.getString(i));
                    }
                }
            } catch (JSONException e) {
                e.printStackTrace();
            }
        }
    }
    //      System.out.println("%%% usedResources: "+usedResources);
    //      System.out.println("%%% getResourceList: "+getResourceList());
    int sizeBefore = usedResources.size();
    usedResources.removeAll(this.getResourceList());
    int sizeLater = usedResources.size();

    if (sizeLater != sizeBefore) {
        resourceConflict = true;
        //         System.out.println("%%%%%%%%%%%%%%%%%%%%% There was a conflict here");
    }

    return resourceConflict;
}