Example usage for org.json JSONObject getJSONArray

List of usage examples for org.json JSONObject getJSONArray

Introduction

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

Prototype

public JSONArray getJSONArray(String key) throws JSONException 

Source Link

Document

Get the JSONArray value associated with a key.

Usage

From source file:com.whizzosoftware.hobson.dto.data.DataStreamDTOTest.java

@Test
public void testToJSON() {
    ExpansionFields ef = new ExpansionFields(JSONAttributes.DATA);
    DataStreamDTO dto = createDTO(new ManagerDTOBuildContext.Builder().idProvider(new ContextPathIdProvider())
            .expansionFields(ef).build(), true);
    JSONObject json = dto.toJSON();
    assertEquals("dataStreams:ds1", json.getString("@id"));
    assertEquals("My DS", json.getString("name"));
    JSONArray fa = json.getJSONArray("fields");
    assertEquals(1, fa.length());//from  ww  w.  j a v  a  2 s.c om
    JSONObject f = fa.getJSONObject(0);
    assertEquals("dataStreams:ds1:fields:field1", f.getString("@id"));
    assertEquals("test", f.getString("name"));
    assertNotNull(f.getJSONObject("variable"));
    assertEquals("hubs:local:variables:plugin1:device1:var1", f.getJSONObject("variable").getString("@id"));
    fa = json.getJSONArray("tags");
    assertTrue("tag1".equals(json.getJSONArray("tags").getString(0))
            || "tag2".equals(json.getJSONArray("tags").getString(0)));
    assertTrue("tag1".equals(json.getJSONArray("tags").getString(1))
            || "tag2".equals(json.getJSONArray("tags").getString(1)));
    assertEquals(2, fa.length());
}

From source file:info.rmapproject.webapp.auth.GoogleOAuthProvider.java

public OAuthProviderAccount loadOAuthProviderAccount(Token accessToken, OAuthProviderName provider) {

    OAuthService service = this.getService();

    // getting user profile
    OAuthRequest oauthRequest = new OAuthRequest(Verb.GET, config.getProfileUrl(), service);

    service.signRequest(accessToken, oauthRequest);
    Response oauthResponse = oauthRequest.send();
    String jsonString = oauthResponse.getBody();
    JSONObject root = new JSONObject(jsonString);
    JSONArray emailArray = root.getJSONArray(GOOGLE_JSON_EMAILLIST_PROPERTY);
    JSONObject firstEmail = emailArray.getJSONObject(0);

    String accountId = root.getString(GOOGLE_JSON_ACCOUNTID_PROPERTY);
    String displayName = root.getString(GOOGLE_JSON_DISPLAYNAME_PROPERTY);
    String publicId = firstEmail.getString(GOOGLE_JSON_EMAIL_PROPERTY);
    String profilePath = "";
    if (root.has(GOOGLE_JSON_PROFILEPATH_PROPERTY)) {
        profilePath = root.getString(GOOGLE_JSON_PROFILEPATH_PROPERTY);
    }/*w  w  w  .  j a va 2  s . co m*/

    OAuthProviderAccount profile = new OAuthProviderAccount(accessToken, provider, displayName, accountId,
            publicId, profilePath);

    //logger.info("Google profile=" + jsonString);
    //logger.info("Google token=" + accessToken.getRawResponse());

    return profile;
}

From source file:com.hichinaschool.flashcards.anki.servicelayer.NoteService.java

/**
 * Creates an empty Note from given Model
 * //w  w w  .ja  v a  2s. c  om
 * @param model the model in JSOBObject format
 * @return a new note instance
 */
public static MultimediaEditableNote createEmptyNote(JSONObject model) {
    try {
        JSONArray fieldsArray = model.getJSONArray("flds");
        int numOfFields = fieldsArray.length();
        if (numOfFields > 0) {
            MultimediaEditableNote note = new MultimediaEditableNote();
            note.setNumFields(numOfFields);

            for (int i = 0; i < numOfFields; i++) {
                JSONObject fieldObject = fieldsArray.getJSONObject(i);
                TextField uiTextField = new TextField();
                uiTextField.setName(fieldObject.getString("name"));
                uiTextField.setText(fieldObject.getString("name"));
                note.setField(i, uiTextField);
            }
            note.setModelId(model.getLong("id"));
            return note;
        }
    } catch (JSONException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    return null;
}

From source file:com.tassadar.multirommgr.installfragment.InstallCard.java

private int getDefaultKernel() {
    int res = 0;/*from ww  w . j  a va  2 s  .  c o  m*/
    Iterator<Map.Entry<String, Manifest.InstallationFile>> itr = m_manifest.getKernels().entrySet().iterator();
    for (int i = 0; itr.hasNext(); ++i) {
        JSONObject extra = itr.next().getValue().extra;
        if (extra == null)
            continue;
        try {
            if (extra.has("display") && Build.DISPLAY.indexOf(extra.getString("display")) == -1)
                continue;

            if (extra.has("releases")) {
                JSONArray r = extra.getJSONArray("releases");
                boolean found = false;
                for (int x = 0; x < r.length(); ++x) {
                    if (r.getString(x).equals(Build.VERSION.RELEASE)) {
                        found = true;
                        break;
                    }
                }
                if (!found)
                    continue;
            }
            res = i;
        } catch (JSONException e) {
            e.printStackTrace();
        }
    }
    return res;
}

From source file:com.github.cambierr.lorawanpacket.semtech.PullResp.java

public PullResp(byte[] _randoms, ByteBuffer _raw) throws MalformedPacketException {
    super(_randoms, PacketType.PULL_RESP);
    _raw.order(ByteOrder.LITTLE_ENDIAN);

    if (_raw.remaining() < 1) {
        throw new MalformedPacketException("too short");
    }//from   ww w .ja va2 s  . c o m

    byte[] json = new byte[_raw.remaining()];
    _raw.get(json);
    JSONObject jo;

    try {
        jo = new JSONObject(new String(json));
    } catch (JSONException ex) {
        throw new MalformedPacketException("malformed json");
    }

    txpks = new ArrayList<>();

    if (!jo.has("txpk")) {
        throw new MalformedPacketException("missing json (txpk)");
    }

    if (!jo.get("txpk").getClass().equals(JSONArray.class)) {
        throw new MalformedPacketException("malformed json (txpk)");
    }
    JSONArray rxpk = jo.getJSONArray("txpk");

    for (int i = 0; i < rxpk.length(); i++) {
        txpks.add(new Txpk(rxpk.getJSONObject(i)));
    }
}

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

@Test
public void testEmptyStashList() throws Exception {
    String projectName = getMethodName();
    createWorkspace(SimpleMetaStore.DEFAULT_WORKSPACE_NAME);
    JSONObject project = createProjectOrLink(workspaceLocation, projectName, gitDir.toString());

    String stashLocation = getStashLocation(project);
    ServerStatus status = getStashList(stashLocation);
    assertTrue(status.isOK());//from  w w  w  .j  a v  a2 s .c  om

    JSONObject stash = status.getJsonData();
    assertTrue(stash.has(ProtocolConstants.KEY_CHILDREN));
    assertEquals(0, stash.getJSONArray(ProtocolConstants.KEY_CHILDREN).length());
}

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

@Test
public void testStashListPagination() throws Exception {
    String projectName = getMethodName();
    createWorkspace(SimpleMetaStore.DEFAULT_WORKSPACE_NAME);
    JSONObject project = createProjectOrLink(workspaceLocation, projectName, gitDir.toString());
    String stashLocation = getStashLocation(project);

    int CHANGES = 12;
    int pageSize = 5;
    int k = CHANGES;

    while (k-- > 0) {
        JSONObject testTxt = getChild(project, "test.txt"); //$NON-NLS-1$
        modifyFile(testTxt, "change to stash " + String.valueOf(k)); //$NON-NLS-1$

        ServerStatus status = createStash(stashLocation);
        assertTrue(status.isOK());//from   w  ww .  ja  v  a 2 s .  c  om
    }

    ServerStatus status = getStashList(stashLocation, 1, pageSize);
    assertTrue(status.isOK());

    /* first page */
    JSONObject stash = status.getJsonData();
    assertTrue(stash.has(ProtocolConstants.KEY_CHILDREN));
    assertEquals(pageSize, stash.getJSONArray(ProtocolConstants.KEY_CHILDREN).length());
    assertTrue(stash.has(ProtocolConstants.KEY_NEXT_LOCATION));
    assertFalse(stash.has(ProtocolConstants.KEY_PREVIOUS_LOCATION));

    status = getStashList(stash.getString(ProtocolConstants.KEY_NEXT_LOCATION));
    assertTrue(status.isOK());

    /* second page */
    stash = status.getJsonData();
    assertTrue(stash.has(ProtocolConstants.KEY_CHILDREN));
    assertEquals(pageSize, stash.getJSONArray(ProtocolConstants.KEY_CHILDREN).length());
    assertTrue(stash.has(ProtocolConstants.KEY_NEXT_LOCATION));
    assertTrue(stash.has(ProtocolConstants.KEY_PREVIOUS_LOCATION));

    status = getStashList(stash.getString(ProtocolConstants.KEY_NEXT_LOCATION));
    assertTrue(status.isOK());

    /* third page */
    stash = status.getJsonData();
    assertTrue(stash.has(ProtocolConstants.KEY_CHILDREN));
    assertEquals((CHANGES - 2 * pageSize), stash.getJSONArray(ProtocolConstants.KEY_CHILDREN).length());
    assertFalse(stash.has(ProtocolConstants.KEY_NEXT_LOCATION));
    assertTrue(stash.has(ProtocolConstants.KEY_PREVIOUS_LOCATION));
}

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

@Test
public void testStashCreateWithUntracked() throws Exception {
    String projectName = getMethodName();
    createWorkspace(SimpleMetaStore.DEFAULT_WORKSPACE_NAME);
    JSONObject project = createProjectOrLink(workspaceLocation, projectName, gitDir.toString());

    JSONObject testTxt = getChild(project, "test.txt"); //$NON-NLS-1$
    modifyFile(testTxt, "change to stash"); //$NON-NLS-1$

    String stashLocation = getStashLocation(project);
    ServerStatus status = getStashList(stashLocation);
    assertTrue(status.isOK());//from   w w w . j  a  v  a2 s  .c  o m

    JSONObject stash = status.getJsonData();
    assertTrue(stash.has(ProtocolConstants.KEY_CHILDREN));
    assertEquals(0, stash.getJSONArray(ProtocolConstants.KEY_CHILDREN).length());

    status = createStash(stashLocation);
    assertTrue(status.isOK());

    status = getStashList(stashLocation);
    assertTrue(status.isOK());

    stash = status.getJsonData();
    assertTrue(stash.has(ProtocolConstants.KEY_CHILDREN));
    assertEquals(1, stash.getJSONArray(ProtocolConstants.KEY_CHILDREN).length());
}

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

@Test
public void testStashCreateWithUntrackedAndIndex() throws Exception {
    String projectName = getMethodName();
    createWorkspace(SimpleMetaStore.DEFAULT_WORKSPACE_NAME);
    JSONObject project = createProjectOrLink(workspaceLocation, projectName, gitDir.toString());

    JSONObject testTxt = getChild(project, "test.txt"); //$NON-NLS-1$
    modifyFile(testTxt, "change to stash"); //$NON-NLS-1$

    /* stage folder.txt */
    JSONObject folder1 = getChild(project, "folder"); //$NON-NLS-1$
    JSONObject folderTxt = getChild(folder1, "folder.txt"); //$NON-NLS-1$
    modifyFile(folderTxt, "change to stash"); //$NON-NLS-1$
    addFile(folderTxt);/*from  w w w  . ja va 2  s.com*/

    String stashLocation = getStashLocation(project);
    ServerStatus status = getStashList(stashLocation);
    assertTrue(status.isOK());

    JSONObject stash = status.getJsonData();
    assertTrue(stash.has(ProtocolConstants.KEY_CHILDREN));
    assertEquals(0, stash.getJSONArray(ProtocolConstants.KEY_CHILDREN).length());

    status = createStash(stashLocation);
    assertTrue(status.isOK());

    status = getStashList(stashLocation);
    assertTrue(status.isOK());

    stash = status.getJsonData();
    assertTrue(stash.has(ProtocolConstants.KEY_CHILDREN));
    assertEquals(1, stash.getJSONArray(ProtocolConstants.KEY_CHILDREN).length());

    JSONObject stashChange = stash.getJSONArray(ProtocolConstants.KEY_CHILDREN).getJSONObject(0);
    assertTrue(stashChange.has(GitConstants.KEY_COMMIT_DIFFS));
    assertEquals(2, stashChange.getJSONArray(GitConstants.KEY_COMMIT_DIFFS).length());
}

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

@Test
public void testStashApply() throws Exception {
    String projectName = getMethodName();
    createWorkspace(SimpleMetaStore.DEFAULT_WORKSPACE_NAME);
    JSONObject project = createProjectOrLink(workspaceLocation, projectName, gitDir.toString());

    JSONObject testTxt = getChild(project, "test.txt"); //$NON-NLS-1$
    modifyFile(testTxt, "change to stash"); //$NON-NLS-1$
    String beforeStash = getFileContent(testTxt);

    String stashLocation = getStashLocation(project);
    ServerStatus status = createStash(stashLocation);
    assertTrue(status.isOK());/*from  ww  w  .  j  a  va2 s  .co  m*/

    String afterStash = getFileContent(testTxt);
    assertFalse(beforeStash.equals(afterStash));

    status = getStashList(stashLocation);
    assertTrue(status.isOK());

    JSONObject stash = status.getJsonData();
    assertTrue(stash.has(ProtocolConstants.KEY_CHILDREN));
    assertEquals(1, stash.getJSONArray(ProtocolConstants.KEY_CHILDREN).length());

    JSONObject change = stash.getJSONArray(ProtocolConstants.KEY_CHILDREN).getJSONObject(0);
    assertTrue(change.has(GitConstants.KEY_STASH_APPLY_LOCATION));

    status = applyStash(change.getString(GitConstants.KEY_STASH_APPLY_LOCATION));
    assertTrue(status.isOK());

    status = getStashList(stashLocation);
    assertTrue(status.isOK());

    stash = status.getJsonData();
    assertTrue(stash.has(ProtocolConstants.KEY_CHILDREN));
    assertEquals(1, stash.getJSONArray(ProtocolConstants.KEY_CHILDREN).length());

    String afterApply = getFileContent(testTxt);
    assertTrue(beforeStash.equals(afterApply));
}