Example usage for org.json JSONArray length

List of usage examples for org.json JSONArray length

Introduction

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

Prototype

public int length() 

Source Link

Document

Get the number of elements in the JSONArray, included nulls.

Usage

From source file:to.networld.fbtosemweb.fb.FacebookAgentHandler.java

public Vector<FacebookLikesEntry> getLikesConcepts() throws IOException, JSONException {
    Vector<FacebookLikesEntry> resultEntries = new Vector<FacebookLikesEntry>();
    JSONObject likesObject = this.getContent(this.FB_LIKES_URL);
    JSONArray likesEntries = likesObject.getJSONArray("data");
    for (int count = 0; count < likesEntries.length(); count++) {
        JSONObject jsonEntry = (JSONObject) likesEntries.get(count);
        FacebookLikesEntry likesEntry = new FacebookLikesEntry(jsonEntry.getString("id"),
                jsonEntry.getString("name"), jsonEntry.getString("category"),
                jsonEntry.getString("created_time"));
        resultEntries.add(likesEntry);/*from w  w  w. j  a v a 2  s .com*/
    }
    return resultEntries;
}

From source file:to.networld.fbtosemweb.fb.FacebookAgentHandler.java

public Vector<FacebookFriendEntry> getFriends() throws IOException, JSONException {
    Vector<FacebookFriendEntry> resultEntries = new Vector<FacebookFriendEntry>();
    JSONObject likesObject = this.getContent(this.FB_FRIENDS_URL);
    JSONArray likesEntries = likesObject.getJSONArray("data");
    for (int count = 0; count < likesEntries.length(); count++) {
        JSONObject jsonEntry = (JSONObject) likesEntries.get(count);
        FacebookFriendEntry friendEntry = new FacebookFriendEntry(jsonEntry.getString("id"),
                jsonEntry.getString("name"));
        resultEntries.add(friendEntry);// w  w  w.j av a  2  s  .  c o  m
    }
    return resultEntries;
}

From source file:to.networld.fbtosemweb.fb.FacebookAgentHandler.java

public Vector<FacebookPage> getOwnedPages() throws IOException, JSONException {
    Vector<FacebookPage> resultEntries = new Vector<FacebookPage>();
    JSONObject pagesObject = this.getContent(this.FB_PAGES_URL);
    JSONArray pagesEntries = pagesObject.getJSONArray("data");
    for (int count = 0; count < pagesEntries.length(); count++) {
        JSONObject jsonEntry = (JSONObject) pagesEntries.get(count);
        FacebookPage pagesEntry = new FacebookPage(jsonEntry);
        resultEntries.add(pagesEntry);//ww  w.j ava  2 s .  com
    }
    return resultEntries;
}

From source file:com.owncloud.android.extensions.ExtensionsListActivity.java

public void done(JSONArray a) {
    LinkedList<HashMap<String, String>> ll = new LinkedList<HashMap<String, String>>();
    for (int i = 0; i < a.length(); ++i) {
        try {// ww w  .  ja  va  2 s.  c om
            ExtensionApplicationEntry ela = new ExtensionApplicationEntry(((JSONObject) a.get(i)));
            HashMap<String, String> ss = new HashMap<String, String>();
            ss.put("NAME", ela.getName());
            ss.put("DESC", ela.getDescription());
            ll.add(ss);
        } catch (JSONException e) {
            e.printStackTrace();
        }
    }
    setListAdapter(new SimpleAdapter(this, ll, R.layout.simple_list_item_2, new String[] { "NAME", "DESC" },
            new int[] { android.R.id.text1, android.R.id.text2 }));

}

From source file:org.eclipse.orion.server.tests.servlets.git.GitConfigTest.java

@Test
public void testGetListOfConfigEntries() throws Exception {
    URI workspaceLocation = createWorkspace(getMethodName());
    IPath[] clonePaths = createTestProjects(workspaceLocation);

    for (IPath clonePath : clonePaths) {
        // clone a  repo
        String contentLocation = clone(clonePath).getString(ProtocolConstants.KEY_CONTENT_LOCATION);

        // get project metadata
        WebRequest request = getGetRequest(contentLocation);
        WebResponse response = webConversation.getResponse(request);
        assertEquals(HttpURLConnection.HTTP_OK, response.getResponseCode());
        JSONObject project = new JSONObject(response.getText());
        JSONObject gitSection = project.getJSONObject(GitConstants.KEY_GIT);
        String gitConfigUri = gitSection.getString(GitConstants.KEY_CONFIG);

        JSONObject configResponse = listConfigEntries(gitConfigUri);
        JSONArray configEntries = configResponse.getJSONArray(ProtocolConstants.KEY_CHILDREN);

        for (int i = 0; i < configEntries.length(); i++) {
            JSONObject configEntry = configEntries.getJSONObject(i);
            assertNotNull(configEntry.optString(GitConstants.KEY_CONFIG_ENTRY_KEY, null));
            assertNotNull(configEntry.optString(GitConstants.KEY_CONFIG_ENTRY_VALUE, null));
            assertConfigUri(configEntry.getString(ProtocolConstants.KEY_LOCATION));
            assertCloneUri(configEntry.getString(GitConstants.KEY_CLONE));
            assertEquals(ConfigOption.TYPE, configEntry.getString(ProtocolConstants.KEY_TYPE));
        }//from  ww w .j  av  a2s .c  o  m
    }
}

From source file:org.eclipse.orion.server.tests.servlets.git.GitConfigTest.java

@Test
public void testAddConfigEntry() throws Exception {
    URI workspaceLocation = createWorkspace(getMethodName());
    IPath[] clonePaths = createTestProjects(workspaceLocation);

    for (IPath clonePath : clonePaths) {
        // clone a  repo
        String contentLocation = clone(clonePath).getString(ProtocolConstants.KEY_CONTENT_LOCATION);

        // get project metadata
        WebRequest request = getGetRequest(contentLocation);
        WebResponse response = webConversation.getResponse(request);
        assertEquals(HttpURLConnection.HTTP_OK, response.getResponseCode());
        JSONObject project = new JSONObject(response.getText());
        JSONObject gitSection = project.getJSONObject(GitConstants.KEY_GIT);
        String gitConfigUri = gitSection.getString(GitConstants.KEY_CONFIG);

        JSONObject configResponse = listConfigEntries(gitConfigUri);
        JSONArray configEntries = configResponse.getJSONArray(ProtocolConstants.KEY_CHILDREN);
        // initial number of config entries
        int initialConfigEntriesCount = configEntries.length();

        // set some dummy value
        final String ENTRY_KEY = "a.b.c";
        final String ENTRY_VALUE = "v";

        request = getPostGitConfigRequest(gitConfigUri, ENTRY_KEY, ENTRY_VALUE);
        response = webConversation.getResponse(request);
        assertEquals(HttpURLConnection.HTTP_CREATED, response.getResponseCode());
        configResponse = new JSONObject(response.getText());
        String entryLocation = configResponse.getString(ProtocolConstants.KEY_LOCATION);
        assertConfigUri(entryLocation);//  www.java2s  .  c o  m

        // get list of config entries again
        configResponse = listConfigEntries(gitConfigUri);
        configEntries = configResponse.getJSONArray(ProtocolConstants.KEY_CHILDREN);
        assertEquals(initialConfigEntriesCount + 1, configEntries.length());

        entryLocation = null;
        for (int i = 0; i < configEntries.length(); i++) {
            JSONObject configEntry = configEntries.getJSONObject(i);
            if (ENTRY_KEY.equals(configEntry.getString(GitConstants.KEY_CONFIG_ENTRY_KEY))) {
                assertConfigOption(configEntry, ENTRY_KEY, ENTRY_VALUE);
                break;
            }
        }

        // double check
        org.eclipse.jgit.lib.Config config = getRepositoryForContentLocation(contentLocation).getConfig();
        assertEquals(ENTRY_VALUE, config.getString("a", "b", "c"));
    }
}

From source file:fr.pasteque.pos.customers.DataLogicCustomers.java

/** Load customers list from server */
private List<CustomerInfoExt> loadCustomers() throws BasicException {
    try {/* w w w  . ja  va  2  s . c  o m*/
        ServerLoader loader = new ServerLoader();
        ServerLoader.Response r = loader.read("CustomersAPI", "getAll");
        if (r.getStatus().equals(ServerLoader.Response.STATUS_OK)) {
            List<CustomerInfoExt> data = new ArrayList<CustomerInfoExt>();
            JSONArray a = r.getArrayContent();
            for (int i = 0; i < a.length(); i++) {
                JSONObject o = a.getJSONObject(i);
                CustomerInfoExt customer = new CustomerInfoExt(o);
                data.add(customer);
            }
            return data;
        }
    } catch (Exception e) {
        throw new BasicException(e);
    }
    return null;
}

From source file:fr.pasteque.pos.customers.DataLogicCustomers.java

private List<String> loadTopCustomers() throws BasicException {
    try {/*from w ww. j a va 2s .c o  m*/
        ServerLoader loader = new ServerLoader();
        ServerLoader.Response r = loader.read("CustomersAPI", "getTop");
        if (r.getStatus().equals(ServerLoader.Response.STATUS_OK)) {
            List<String> data = new ArrayList<String>();
            JSONArray a = r.getArrayContent();
            for (int i = 0; i < a.length(); i++) {
                data.add(a.getString(i));
            }
            return data;
        }
    } catch (Exception e) {
        throw new BasicException(e);
    }
    return null;
}

From source file:fr.pasteque.pos.customers.DataLogicCustomers.java

private static void loadDiscountProfiles() throws BasicException {
    try {//from ww w  .j  ava2  s  .  com
        ServerLoader loader = new ServerLoader();
        ServerLoader.Response r = loader.read("DiscountProfilesAPI", "getAll");
        if (r.getStatus().equals(ServerLoader.Response.STATUS_OK)) {
            DataLogicCustomers.discProfileCache = new ArrayList<DiscountProfile>();
            JSONArray a = r.getArrayContent();
            for (int i = 0; i < a.length(); i++) {
                JSONObject o = a.getJSONObject(i);
                DiscountProfile prof = new DiscountProfile(o);
                DataLogicCustomers.discProfileCache.add(prof);
            }
        }
    } catch (Exception e) {
        throw new BasicException(e);
    }
}

From source file:fr.cobaltians.cobalt.fragments.CobaltFragment.java

/******************************************************************************************************************
 * ALERT DIALOG// w w w  .jav a  2  s  .co m
 *****************************************************************************************************************/

private void showAlertDialog(JSONObject data, final String callback) {
    try {
        String title = data.optString(Cobalt.kJSAlertTitle);
        String message = data.optString(Cobalt.kJSMessage);
        boolean cancelable = data.optBoolean(Cobalt.kJSAlertCancelable, false);
        JSONArray buttons = data.has(Cobalt.kJSAlertButtons) ? data.getJSONArray(Cobalt.kJSAlertButtons)
                : new JSONArray();

        AlertDialog alertDialog = new AlertDialog.Builder(mContext).setTitle(title).setMessage(message)
                .create();
        alertDialog.setCancelable(cancelable);

        if (buttons.length() == 0) {
            alertDialog.setButton(DialogInterface.BUTTON_NEGATIVE, "OK", new DialogInterface.OnClickListener() {

                @Override
                public void onClick(DialogInterface dialog, int which) {
                    if (callback != null) {
                        try {
                            JSONObject data = new JSONObject();
                            data.put(Cobalt.kJSAlertButtonIndex, 0);
                            sendCallback(callback, data);
                        } catch (JSONException exception) {
                            if (Cobalt.DEBUG)
                                Log.e(Cobalt.TAG, TAG + ".AlertDialog - onClick: JSONException");
                            exception.printStackTrace();
                        }
                    }
                }
            });
        } else {
            int buttonsLength = Math.min(buttons.length(), 3);
            for (int i = 0; i < buttonsLength; i++) {
                int buttonId;

                switch (i) {
                case 0:
                default:
                    buttonId = DialogInterface.BUTTON_NEGATIVE;
                    break;
                case 1:
                    buttonId = DialogInterface.BUTTON_NEUTRAL;
                    break;
                case 2:
                    buttonId = DialogInterface.BUTTON_POSITIVE;
                    break;
                }

                alertDialog.setButton(buttonId, buttons.getString(i), new DialogInterface.OnClickListener() {

                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        if (callback != null) {
                            int buttonIndex;
                            switch (which) {
                            case DialogInterface.BUTTON_NEGATIVE:
                            default:
                                buttonIndex = 0;
                                break;
                            case DialogInterface.BUTTON_NEUTRAL:
                                buttonIndex = 1;
                                break;
                            case DialogInterface.BUTTON_POSITIVE:
                                buttonIndex = 2;
                                break;
                            }

                            try {
                                JSONObject data = new JSONObject();
                                data.put(Cobalt.kJSAlertButtonIndex, buttonIndex);
                                sendCallback(callback, data);
                            } catch (JSONException exception) {
                                if (Cobalt.DEBUG)
                                    Log.e(Cobalt.TAG, TAG + ".AlertDialog - onClick: JSONException");
                                exception.printStackTrace();
                            }
                        }
                    }
                });
            }
        }

        alertDialog.show();
    } catch (JSONException exception) {
        if (Cobalt.DEBUG)
            Log.e(Cobalt.TAG, TAG + " - showAlertDialog: JSONException");
        exception.printStackTrace();
    }
}