Example usage for org.json JSONObject get

List of usage examples for org.json JSONObject get

Introduction

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

Prototype

public Object get(String key) throws JSONException 

Source Link

Document

Get the value object associated with a key.

Usage

From source file:util.Control.java

public Control(boolean debug) {
    //Thread affinity class is "Legaly CopyPasted" from a guy that I 
    //mention on my github on the affinity project
    threadAffinity = new ThreadAffinity(this);

    this.debug = debug;
    String jsonReply = "";

    if (threadAffinity.cores().length == 4) {
        //If quadcore etc
        encryptionCore = threadAffinity.cores()[0];
        HTTPCore = threadAffinity.cores()[1];
        sensingCore = threadAffinity.cores()[2];
        cronCore = threadAffinity.cores()[3];
    } else if (threadAffinity.cores().length == 2) {
        encryptionCore = threadAffinity.cores()[0];
        HTTPCore = threadAffinity.cores()[1];
        sensingCore = threadAffinity.cores()[1];
        cronCore = threadAffinity.cores()[0];
    } else if (threadAffinity.cores().length == 1) {
        encryptionCore = threadAffinity.cores()[0];
        HTTPCore = threadAffinity.cores()[0];
        sensingCore = threadAffinity.cores()[0];
        cronCore = threadAffinity.cores()[0];
    } else {//from ww  w.  j  a  va  2  s .co m
        //if32core fuck off, use only one core
        encryptionCore = threadAffinity.cores()[0];
        HTTPCore = threadAffinity.cores()[0];
        sensingCore = threadAffinity.cores()[0];
        cronCore = threadAffinity.cores()[0];
    }

    if (debug) {
        System.out.println("Available cores: " + threadAffinity.cores().length);
        System.out.println("encryptionCore: " + encryptionCore);
        System.out.println("HTTPCore: " + HTTPCore);
        System.out.println("sensingCore: " + sensingCore);
        System.out.println("cronCore: " + cronCore);
    }
    encryptionCore.setC(this);
    HTTPCore.setC(this);
    sensingCore.setC(this);
    cronCore.setC(this);

    try {
        //So this apparently gets your LAN address, provided there is one
        InetAddress addr = getFirstNonLoopbackAddress(true, false);
        String ip = addr.getHostAddress();

        //this could have been a nonlocal adress, but I'm such a bad person
        String registryUnitIP = "127.0.0.1";

        messages = new Messaging(this);
        jsonReply = HTTPRequest.sendPost("http://" + registryUnitIP, 8383, URLEncoder.encode("ip=" + ip
                + "&port=8181&services={\"services\":[{\"uri\" : \"/sensors\", \"description\" : \"returns a list of sensors available\"}]}"),
                "/register");
        //registers itself to the registry unit

        if (debug) {
            System.out.println("reply is: " + jsonReply);
        }
        JSONObject obj;

        obj = new JSONObject(jsonReply);

        if (!obj.get("result").equals("success")) {
            //parsing the json object token by token
            if (debug) {
                System.out.println("jsonReply failed " + jsonReply);
            }
        } else {
            uid = obj.getString("uid");
            if (debug) {
                System.out.println("myUID is " + uid);
            }
        }

    } catch (Exception e) {
        if (debug) {
            System.out.println(jsonReply);
        }
        e.printStackTrace();
    }
    motesList = new ArrayList<MicazMote>();
    dropDaemon = createDropDaemon();
    dropDaemon.start();
    populate = constructPollDaemon();
    populate.start();
}

From source file:org.jboss.aerogear.cordova.push.PushPlugin.java

private JSONObject parseConfig(JSONArray data) throws JSONException {
    JSONObject pushConfig = data.getJSONObject(0);
    if (!pushConfig.isNull("android")) {
        final JSONObject android = pushConfig.getJSONObject("android");
        for (Iterator iterator = android.keys(); iterator.hasNext();) {
            String key = (String) iterator.next();
            pushConfig.put(key, android.get(key));
        }//from w ww.  j a v  a2s .c om

        pushConfig.remove("android");
    }
    return pushConfig;
}

From source file:networkedassets.PhilipsHue.java

private void fetchLightData() throws IOException {
    allLights.clear();//from  w ww .j a va  2 s .  com
    JSONObject lights = Useful.readJsonFromUrl(hueIp + "api/newdeveloper/lights");
    Iterator<String> keys = lights.keys();

    while (keys.hasNext()) {
        String key = keys.next();
        JSONObject onelight = (JSONObject) lights.get(key);

        PhilipsLight newlight = new PhilipsLight();
        newlight.modelid = onelight.getString("modelid");
        newlight.name = onelight.getString("name");
        newlight.type = onelight.getString("type");
        newlight.id = Integer.parseInt(key);
        allLights.add(newlight);

        System.out.println(key + " " + newlight.type);

    }
}

From source file:org.wso2.carbon.connector.integration.test.github.GithubConnectorIntegrationTest.java

/**
 * Positive test case for listRepositoryIssues method with mandatory parameters.
 */// ww w .  ja v  a 2  s.c  om
@Test(priority = 1, dependsOnMethods = {
        "testSearchIssuesWithNegativeCase" }, description = "github {listRepositoryIssues} integration test with mandatory parameters.")
public void testListRepositoryIssuesWithMandatoryParameters() throws IOException, JSONException {
    esbRequestHeadersMap.put("Action", "urn:listRepositoryIssues");
    String apiEndPoint = connectorProperties.getProperty("githubApiUrl") + "/repos/"
            + connectorProperties.getProperty("owner") + "/" + connectorProperties.getProperty("repo")
            + "/issues";
    RestResponse<JSONObject> esbRestResponse = sendJsonRestRequest(proxyUrl, "POST", esbRequestHeadersMap,
            "esb_listRepositoryIssues_mandatory.json");
    RestResponse<JSONObject> apiRestResponse = sendJsonRestRequest(apiEndPoint, "GET", apiRequestHeadersMap);
    JSONArray esbArray = new JSONArray(esbRestResponse.getBody().getString("output"));
    JSONArray apiArray = new JSONArray(apiRestResponse.getBody().getString("output"));
    Assert.assertEquals(esbArray.length(), apiArray.length());
    if (esbArray.length() > 0 && apiArray.length() > 0) {
        JSONObject esbFirstElement = esbArray.getJSONObject(0);
        JSONObject apiFirstElement = apiArray.getJSONObject(0);
        Assert.assertEquals(esbFirstElement.get("number"), apiFirstElement.get("number"));
    }
}

From source file:org.wso2.carbon.connector.integration.test.github.GithubConnectorIntegrationTest.java

/**
 * Positive test case for listRepositoryIssues method with optional parameters.
 *///from  w w w .  j  a  va  2s  . c  o m
@Test(priority = 1, dependsOnMethods = {
        "testListRepositoryIssuesWithMandatoryParameters" }, description = "github {listRepositoryIssues} integration test with optional parameters.")
public void testListRepositoryIssuesWithOptionalParameters() throws IOException, JSONException {
    esbRequestHeadersMap.put("Action", "urn:listRepositoryIssues");
    String apiEndPoint = connectorProperties.getProperty("githubApiUrl") + "/repos/"
            + connectorProperties.getProperty("owner") + "/" + connectorProperties.getProperty("repo")
            + "/issues?milestone=*&state=open&assignee=" + connectorProperties.getProperty("owner")
            + "&creator=" + connectorProperties.getProperty("owner") + "&sort=created&direction=asc";
    RestResponse<JSONObject> esbRestResponse = sendJsonRestRequest(proxyUrl, "POST", esbRequestHeadersMap,
            "esb_listRepositoryIssues_optional.json");
    RestResponse<JSONObject> apiRestResponse = sendJsonRestRequest(apiEndPoint, "GET", apiRequestHeadersMap);
    JSONArray esbArray = new JSONArray(esbRestResponse.getBody().getString("output"));
    JSONArray apiArray = new JSONArray(apiRestResponse.getBody().getString("output"));
    Assert.assertEquals(esbArray.length(), apiArray.length());
    if (esbArray.length() > 0 && apiArray.length() > 0) {
        JSONObject esbFirstElement = esbArray.getJSONObject(0);
        JSONObject apiFirstElement = apiArray.getJSONObject(0);
        Assert.assertEquals(esbFirstElement.get("number"), apiFirstElement.get("number"));
    }
}

From source file:org.wso2.carbon.connector.integration.test.github.GithubConnectorIntegrationTest.java

/**
 * Positive test case for listIssueComments method with mandatory parameters.
 *//*  w  w  w  .  j  ava 2  s  . c o m*/
@Test(priority = 1, dependsOnMethods = {
        "testCreateIssueCommentWithNegativeCase" }, description = "github {listIssueComments} integration test with mandatory parameters.")
public void testListIssueCommentsWithMandatoryParameters()
        throws IOException, JSONException, InterruptedException {
    esbRequestHeadersMap.put("Action", "urn:listIssueComments");
    Thread.sleep(timeOut);
    String apiEndPoint = connectorProperties.getProperty("githubApiUrl") + "/repos/"
            + connectorProperties.getProperty("owner") + "/" + connectorProperties.getProperty("repo")
            + "/issues/" + connectorProperties.getProperty("issueNumber") + "/comments";
    RestResponse<JSONObject> esbRestResponse = sendJsonRestRequest(proxyUrl, "POST", esbRequestHeadersMap,
            "esb_listIssueComments_mandatory.json");
    RestResponse<JSONObject> apiRestResponse = sendJsonRestRequest(apiEndPoint, "GET", apiRequestHeadersMap);
    JSONArray esbArray = new JSONArray(esbRestResponse.getBody().getString("output"));
    JSONArray apiArray = new JSONArray(apiRestResponse.getBody().getString("output"));
    Assert.assertEquals(esbArray.length(), apiArray.length());
    if (esbArray.length() > 0 && apiArray.length() > 0) {
        JSONObject esbFirstElement = esbArray.getJSONObject(0);
        JSONObject apiFirstElement = apiArray.getJSONObject(0);
        Assert.assertEquals(esbFirstElement.get("id"), apiFirstElement.get("id"));
    }
}

From source file:org.wso2.carbon.connector.integration.test.github.GithubConnectorIntegrationTest.java

/**
 * Positive test case for listPullRequests method with mandatory parameters
 *//*  www.  j a va 2 s.c o  m*/
@Test(priority = 1, dependsOnMethods = {
        "testGetPullRequestNegativeCase" }, description = "github {listPullRequests} integration test with mandatory parameters.")
public void listPullRequestsWithMandatoryParameters() throws IOException, JSONException {
    esbRequestHeadersMap.put("Action", "urn:listPullRequests");
    String apiEndPoint = connectorProperties.getProperty("githubApiUrl") + "/repos/"
            + connectorProperties.getProperty("owner") + "/" + connectorProperties.getProperty("repo")
            + "/pulls";
    RestResponse<JSONObject> apiRestResponse = sendJsonRestRequest(apiEndPoint, "GET", apiRequestHeadersMap);
    RestResponse<JSONObject> esbRestResponse = sendJsonRestRequest(proxyUrl, "POST", esbRequestHeadersMap,
            "esb_listPullRequests_mandatory.json");
    JSONArray esbResponseJsonArray = new JSONArray(esbRestResponse.getBody().get("output").toString());
    JSONArray apiResponseJsonArray = new JSONArray(apiRestResponse.getBody().get("output").toString());
    Assert.assertEquals(esbResponseJsonArray.length(), apiResponseJsonArray.length());
    if (esbResponseJsonArray.length() > 0 && apiResponseJsonArray.length() > 0) {
        JSONObject esbFirstElement = esbResponseJsonArray.getJSONObject(0);
        JSONObject apiFirstElement = apiResponseJsonArray.getJSONObject(0);
        Assert.assertEquals(esbFirstElement.get("number"), apiFirstElement.get("number"));
    }
}

From source file:org.wso2.carbon.connector.integration.test.github.GithubConnectorIntegrationTest.java

/**
 * Positive test case for listPullRequests method with optional parameters
 *//*from   w ww.j  a  va2  s .co  m*/
@Test(priority = 1, dependsOnMethods = {
        "listPullRequestsWithMandatoryParameters" }, description = "github {listPullRequests} integration test with optional parameters.")
public void listPullRequestsWithOptionalParameters() throws IOException, JSONException {
    esbRequestHeadersMap.put("Action", "urn:listPullRequests");
    String apiEndPoint = connectorProperties.getProperty("githubApiUrl") + "/repos/"
            + connectorProperties.getProperty("owner") + "/" + connectorProperties.getProperty("repo")
            + "/pulls?state=open";
    RestResponse<JSONObject> apiRestResponse = sendJsonRestRequest(apiEndPoint, "GET", apiRequestHeadersMap);
    RestResponse<JSONObject> esbRestResponse = sendJsonRestRequest(proxyUrl, "POST", esbRequestHeadersMap,
            "esb_listPullRequests_optional.json");
    JSONArray esbResponseJsonArray = new JSONArray(esbRestResponse.getBody().get("output").toString());
    JSONArray apiResponseJsonArray = new JSONArray(apiRestResponse.getBody().get("output").toString());
    Assert.assertEquals(esbResponseJsonArray.length(), apiResponseJsonArray.length());
    if (esbResponseJsonArray.length() > 0 && apiResponseJsonArray.length() > 0) {
        JSONObject esbFirstElement = esbResponseJsonArray.getJSONObject(0);
        JSONObject apiFirstElement = apiResponseJsonArray.getJSONObject(0);
        Assert.assertEquals(esbFirstElement.get("number"), apiFirstElement.get("number"));
    }
}

From source file:org.wso2.carbon.connector.integration.test.github.GithubConnectorIntegrationTest.java

/**
 * Positive test case for listPullRequestCommits method with mandatory parameters
 *///from ww  w  . j a  va 2s .c  om
@Test(priority = 1, dependsOnMethods = {
        "listPullRequestsNegativeCase" }, description = "github {listPullRequestsFiles} integration test case with mandatory parameters.")
public void listPullRequestsCommitsWithMandatoryParameters() throws IOException, JSONException {
    esbRequestHeadersMap.put("Action", "urn:listPullRequestCommits");
    String apiEndPoint = connectorProperties.getProperty("githubApiUrl") + "/repos/"
            + connectorProperties.getProperty("owner") + "/" + connectorProperties.getProperty("repo")
            + "/pulls/" + connectorProperties.getProperty("pullRequestNumber") + "/commits";
    RestResponse<JSONObject> esbRestResponse = sendJsonRestRequest(proxyUrl, "POST", esbRequestHeadersMap,
            "esb_listPullRequestCommits_mandatory.json");
    RestResponse<JSONObject> apiRestResponse = sendJsonRestRequest(apiEndPoint, "GET", apiRequestHeadersMap);
    JSONArray esbResponseJsonArray = new JSONArray(esbRestResponse.getBody().get("output").toString());
    JSONArray apiResponseJsonArray = new JSONArray(apiRestResponse.getBody().get("output").toString());
    Assert.assertEquals(esbResponseJsonArray.length(), apiResponseJsonArray.length());
    if (esbResponseJsonArray.length() > 0 && apiResponseJsonArray.length() > 0) {
        JSONObject esbFirstElement = esbResponseJsonArray.getJSONObject(0);
        JSONObject apiFirstElement = apiResponseJsonArray.getJSONObject(0);
        Assert.assertEquals(esbFirstElement.get("sha"), apiFirstElement.get("sha"));
    }
}

From source file:org.wso2.carbon.connector.integration.test.github.GithubConnectorIntegrationTest.java

/**
 * Positive test case for listForks method with mandatory parameters.
 */// w w w. j a  v a  2s  . c o m
@Test(priority = 1, dependsOnMethods = {
        "testGetTagWithNegativeCase" }, description = "github {listForks} integration test with mandatory parameters.")
public void testListForksWithMandatoryParameters() throws IOException, JSONException {
    esbRequestHeadersMap.put("Action", "urn:listForks");
    String apiEndPoint = connectorProperties.getProperty("githubApiUrl") + "/repos/"
            + connectorProperties.getProperty("owner") + "/" + connectorProperties.getProperty("repo")
            + "/forks";
    RestResponse<JSONObject> esbRestResponse = sendJsonRestRequest(proxyUrl, "POST", esbRequestHeadersMap,
            "esb_listForks_mandatory.json");
    RestResponse<JSONObject> apiRestResponse = sendJsonRestRequest(apiEndPoint, "GET", apiRequestHeadersMap);
    JSONArray esbResponseJsonArray = new JSONArray(esbRestResponse.getBody().get("output").toString());
    JSONArray apiResponseJsonArray = new JSONArray(apiRestResponse.getBody().get("output").toString());
    Assert.assertEquals(esbResponseJsonArray.length(), apiResponseJsonArray.length());
    if (esbResponseJsonArray.length() > 0 && apiResponseJsonArray.length() > 0) {
        JSONObject esbFirstElement = esbResponseJsonArray.getJSONObject(0);
        JSONObject apiFirstElement = apiResponseJsonArray.getJSONObject(0);
        Assert.assertEquals(esbFirstElement.get("id"), apiFirstElement.get("id"));
    }
}