Example usage for org.json JSONObject getJSONObject

List of usage examples for org.json JSONObject getJSONObject

Introduction

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

Prototype

public JSONObject getJSONObject(String key) throws JSONException 

Source Link

Document

Get the JSONObject value associated with a key.

Usage

From source file:org.jandroid2cloud.connection.ChannelHandler.java

@Override
public void message(String rawMsg) {
    logger.debug("Received message from server:" + rawMsg);

    try {//from   ww w . j av a  2s.  c  o m
        JSONObject jsonMessage = new JSONObject(new JSONTokener(rawMsg));
        JSONObject links = (JSONObject) jsonMessage.opt("links");
        if (links == null) {
            JSONObject link = jsonMessage.optJSONObject("link");
            handleLink(link);
        } else {
            Iterator it = links.keys();
            while (it.hasNext()) {
                String s = (String) it.next();
                if (s != null && !s.isEmpty()) {
                    JSONObject o = links.getJSONObject(s);
                    handleLink(o);
                }
            }
        }

        Map<String, String> params = new HashMap<String, String>();
        params.put("links", rawMsg);
        String response = oauth.makeRequest("http://" + config.getHost() + "/markread", Verb.POST, params);
        logger.debug("Marked message as read");
    } catch (JSONException e) {
        e.printStackTrace();
    } catch (NetworkException e) {
        logger.error(NotificationAppender.MARKER, "Could not mark links as read.\n"
                + "You will not receive more links until that is done.\n" + "See log for details", e);
    }
}

From source file:org.jandroid2cloud.connection.ChannelHandler.java

private void handleLink(JSONObject link) throws JSONException {
    if (link.has("link")) {
        link = link.getJSONObject("link");
    }//from   w  ww.  j  a v a 2  s  .c o m
    String url = link.getString("url");
    if (url.equals(waitingFor)) {
        received = true;
        waitingForSema.release(1);
    } else {
        logger.info(NotificationAppender.MARKER, "new link " + url + " received.");
        LinkQueue.INSTANCE.push(url);
    }
}

From source file:com.clearcenter.mobile_demo.mdStatusActivity.java

public List<JSONObject> sortedSamplesList(String data) {
    SortedMap<String, JSONObject> map = new TreeMap<String, JSONObject>();

    try {/*from  w  w w  . j  a  va2 s.  co m*/
        int version = 0;
        JSONObject json_data = new JSONObject(data);
        if (json_data.has("version"))
            version = json_data.getInt("version");
        if (version < 1 && json_data.has("time")) {
            String key = json_data.getString("time");
            map.put(key, json_data);
        } else if (version >= 1 && json_data.has("samples") && !json_data.isNull("samples")) {
            json_data = json_data.getJSONObject("samples");
            Iterator i = json_data.keys();
            while (i.hasNext()) {
                String key = i.next().toString();
                JSONObject sample = json_data.getJSONObject(key);
                sample.put("time", key);
                map.put(key, sample);
            }
        }
    } catch (JSONException e) {
        Log.e(TAG, "JSONException", e);
    }

    return new LinkedList<JSONObject>(map.values());
}

From source file:com.microsoft.live.unittest.PostTest.java

@Override
protected void checkValidResponseBody(LiveOperation operation) throws JSONException {
    JSONObject result = operation.getResult();
    String id = result.getString(JsonKeys.ID);
    Object description = result.get(JsonKeys.DESCRIPTION);
    String name = result.getString(JsonKeys.NAME);
    String permissions = result.getString(JsonKeys.PERMISSIONS);
    boolean isDefault = result.getBoolean(JsonKeys.IS_DEFAULT);

    JSONObject from = result.getJSONObject(JsonKeys.FROM);
    String fromId = from.getString(JsonKeys.ID);
    String fromName = from.getString(JsonKeys.NAME);

    Object subscriptionLocation = result.get(JsonKeys.SUBSCRIPTION_LOCATION);
    String createdTime = result.getString(JsonKeys.CREATED_TIME);
    String updatedTime = result.getString(JsonKeys.UPDATED_TIME);

    assertEquals("calendar_id", id);
    assertEquals(JSONObject.NULL, description);
    assertEquals("name", name);
    assertEquals("owner", permissions);
    assertEquals(false, isDefault);// ww w. ja  v a2s.c om
    assertEquals("from_id", fromId);
    assertEquals("from_name", fromName);
    assertEquals(JSONObject.NULL, subscriptionLocation);
    assertEquals("2011-12-10T02:48:33+0000", createdTime);
    assertEquals("2011-12-10T02:48:33+0000", updatedTime);
}

From source file:es.deustotech.piramide.utils.net.JSONParser.java

public static Vector<Point> parseFromUrl(HttpEntity httpEntity) {
    if (httpEntity != null) {
        InputStream inStream;//from  w w w  .ja v a 2s  . c om
        try {
            inStream = httpEntity.getContent();
            result = convertStreamToString(inStream);
            points = new Vector<Point>();
            final JSONObject jsonObj = new JSONObject(result);

            final JSONObject result = jsonObj.getJSONObject(Constants.JSON_RESPONSE_DATA);

            //show 8 results
            for (int i = 0; i < 8; i++) {
                final JSONObject resultAux = (JSONObject) result.getJSONArray(Constants.JSON_RESULTS).get(i);
                //               final JSONObject phoneAux    = (JSONObject) resultAux.getJSONArray(Constants.JSON_PHONE_NUMBERS).get(0);

                final Point point = new Point(resultAux.getString(Constants.JSON_TITLE),
                        resultAux.getString(Constants.JSON_STREET), resultAux.getString(Constants.JSON_LAT),
                        resultAux.getString(Constants.JSON_LNG)

                /*new PhoneNumbers(phoneAux.getString(Constants.JSON_TYPE), 
                phoneAux.getString(Constants.JSON_NUMBER)), 
                resultAux.getString(Constants.JSON_CITY), 
                resultAux.getString(Constants.JSON_TITLE), 
                resultAux.getString(Constants.JSON_REGION), 
                resultAux.getString(Constants.JSON_TITLE_NO_FORMATTING), 
                resultAux.getString(Constants.JSON_STREET), 
                resultAux.getJSONArray(Constants.JSON_ADDRESS_LINES).getString(0), 
                resultAux.getString(Constants.JSON_COUNTRY), 
                resultAux.getString(Constants.JSON_LAT), 
                resultAux.getString(Constants.JSON_LNG),
                resultAux.getString(Constants.JSON_TO_HERE), 
                resultAux.getString(Constants.JSON_FROM_HERE)*/);

                points.add(point);
            }

            //            final JSONObject phoneAux    = (JSONObject) result.getJSONArray(Constants.JSON_PHONE_NUMBERS).get(0);

            /*
            Log.i(Constants.TAG, "<jsonobject>\n" + jsonObj.toString() + "\n</jsonobject>");
                    
            final JSONArray nameArray    = jsonObj.names();
            final JSONArray valArray    = jsonObj.toJSONArray(nameArray);
                    
            for(int i=0; i<valArray.length(); i++) {
               Log.e(Constants.TAG, "<jsonname" + i + ">\n" + nameArray.getString(i) + "\n</jsonname" + i + ">\n" 
             + "<jsonvalue" + i + ">\n" + valArray.getString(i) + "\n</jsonvalue" + i + ">");
            }
            jsonObj.put(Constants.SAMPLE_KEY, Constants.SAMPLE_VALUE);
                    
            Log.i(Constants.TAG, "<jsonobject>\n" + jsonObj.toString() + "\n</jsonobject>");
             */

            inStream.close();
        } catch (IllegalStateException ise) {
            Log.d(Constants.TAG, ise.getMessage());
        } catch (IOException ioe) {
            Log.d(Constants.TAG, ioe.getMessage());
        } catch (JSONException je) {
            Log.d(Constants.TAG, je.getMessage());
        }
    } // If the response does not enclose an entity, there is no need
      // to worry about connection release
    return points;
}

From source file:com.roiland.crm.sm.core.service.impl.ContacterAPIImpl.java

@Override
public Contacter createContacter(String userID, String dealerOrgID, Contacter contacter)
        throws ResponseException {

    // ?//w  w  w .  ja v a  2 s .  c  om
    Contacter returnContacter = null;
    try {
        if (userID == null || dealerOrgID == null) {
            throw new ResponseException("userID or dealerOrgID is null.");
        }
        JSONObject params = new JSONObject();
        params.put("userID", userID);
        params.put("dealerOrgID", dealerOrgID);
        params.put("projectID", contacter.getProjectID());
        params.put("customerID", contacter.getCustomerID());
        params.put("contName", contacter.getContName());
        params.put("contMobile", contacter.getContMobile());
        params.put("contOtherPhone", contacter.getContOtherPhone());
        params.put("isPrimContanter", contacter.getIsPrimContanter());
        params.put("contGenderCode", contacter.getContGenderCode());
        params.put("contBirthday", contacter.getContBirthday());
        params.put("idNumber", contacter.getIdNumber());
        params.put("ageScopeCode", contacter.getAgeScopeCode());
        params.put("contTypeCode", contacter.getContTypeCode());
        params.put("contRelationCode", contacter.getContRelationCode());
        if (contacter.getLicenseValid() == 0) {
            params.put("licenseValid", null);
        } else
            params.put("licenseValid", contacter.getLicenseValid());

        RLHttpResponse response = getHttpClient()
                .executePostJSON(getURLAddress(URLContact.METHOD_CREATE_CONTACTER), params, null);

        if (response.isSuccess()) {

            returnContacter = contacter;
            JSONObject result = new JSONObject(getSimpleString(response));
            String node = null;
            String error = null;
            JSONArray nodeArray = result.names();
            if (nodeArray != null) {
                for (int i = 0; i < nodeArray.length(); i++) {
                    node = nodeArray.get(i).toString();
                    if (node.equalsIgnoreCase("success")) {

                        Boolean success = Boolean.parseBoolean(result.getString("success"));
                        if (success) {
                            // ?ID
                            returnContacter.setContacterID(result.getString("contacterID"));
                        }
                    } else if (node.equalsIgnoreCase("validate_error")) {
                        error = parsingValidation(result.getJSONObject(node));
                        throw new ResponseException(error);
                    }
                }
            }

            return returnContacter;
        }
        throw new ResponseException();
    } catch (IOException e) {
        Log.e(tag, "Connection network error.", e);
        throw new ResponseException(e);
    } catch (JSONException e) {
        Log.e(tag, "Parsing data error.", e);
        throw new ResponseException(e);
    }
}

From source file:com.roiland.crm.sm.core.service.impl.ContacterAPIImpl.java

@Override
public Contacter updateContacter(String userID, String dealerOrgID, Contacter contacter)
        throws ResponseException {
    // ?/*from   w  ww. j av a2 s.  c o m*/
    Contacter returnContacter = null;
    try {
        if (userID == null || dealerOrgID == null) {
            throw new ResponseException("userID or dealerOrgID is null.");
        }
        JSONObject params = new JSONObject();
        params.put("userID", userID);
        params.put("dealerOrgID", dealerOrgID);
        params.put("projectID", contacter.getProjectID());
        params.put("customerID", contacter.getCustomerID());
        params.put("contacterID", contacter.getContacterID());
        params.put("contName", contacter.getContName());
        params.put("contMobile", contacter.getContMobile());
        params.put("contOtherPhone", contacter.getContOtherPhone());
        params.put("isPrimContanter", Boolean.parseBoolean(contacter.getIsPrimContanter()));
        params.put("contGenderCode", contacter.getContGenderCode());
        params.put("contBirthday", contacter.getContBirthday());
        params.put("idNumber", contacter.getIdNumber());
        params.put("ageScopeCode", contacter.getAgeScopeCode());
        params.put("contTypeCode", contacter.getContTypeCode());
        params.put("contRelationCode", contacter.getContRelationCode());
        if (contacter.getLicenseValid() == 0)
            params.put("licenseValid", null);
        else
            params.put("licenseValid", contacter.getLicenseValid());

        RLHttpResponse response = getHttpClient()
                .executePostJSON(getURLAddress(URLContact.METHOD_UPDATE_CONTACTER), params, null);

        if (response.isSuccess()) {
            returnContacter = contacter;
            JSONObject result = new JSONObject(getSimpleString(response));
            // ??
            String node = null;
            String error = null;
            JSONArray nodeArray = result.names();
            if (nodeArray != null) {
                for (int i = 0; i < nodeArray.length(); i++) {
                    node = nodeArray.get(i).toString();
                    if (node.equalsIgnoreCase("success")) {

                        Boolean success = Boolean.parseBoolean(result.getString("success"));
                        //                            if (success) {
                        //                             // ?ID
                        //                                returnContacter.setContacterID(result.getString("contacterID"));
                        //                            }
                    } else if (node.equalsIgnoreCase("validate_error")) {
                        error = parsingValidation(result.getJSONObject(node));
                        throw new ResponseException(error);
                    }
                }
            }
            return returnContacter;
        }
        throw new ResponseException();
    } catch (IOException e) {
        Log.e(tag, "Connection network error.", e);
        throw new ResponseException(e);
    } catch (JSONException e) {
        Log.e(tag, "Parsing data error.", e);
        throw new ResponseException(e.getMessage());
    }
}

From source file:org.melato.bus.otp.OTPParser.java

static Stop getStop(JSONObject leg, String endPoint) throws JSONException {
    JSONObject jsonStop = leg.getJSONObject(endPoint);
    Stop stop = new Stop();
    stop.name = jsonStop.getString("name");
    stop.stopCode = jsonStop.getString("stopCode");
    JSONObject stopId = jsonStop.getJSONObject("stopId");
    stop.id = stopId.getString("id");
    stop.agencyId = stopId.getString("agencyId");
    return stop;/*www .  ja va 2 s .  c o m*/
}

From source file:org.melato.bus.otp.OTPParser.java

static JSONObject getObject(JSONObject json, String key) throws JSONException {
    if (json.has(key) && !json.isNull(key)) {
        return json.getJSONObject(key);
    }//from   w  w  w.jav  a 2s  .  c om
    return null;
}

From source file:de.damdi.fitness.db.parser.ExerciseTagJSONParser.java

/**
 * Parses the JSON-String to a list of {@link ExerciseTag}s.
 * //from  ww  w. ja  va  2s .com
  *   
  * @param jsonString The String to parse.
  * 
  * @return A list of {@link ExerciseTag}s, null if an error occurs.
  * 
 */
@Override
public List<ExerciseTag> parse(String jsonString) {
    List<ExerciseTag> exerciseTagList = new ArrayList<ExerciseTag>();

    JSONArray jsonArray;
    try {
        jsonArray = new JSONArray(jsonString);

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

            ExerciseTag exerciseTag = null;

            for (String locale : TAG_LOCALES) {
                if (SportsEquipmentObject.has(locale)) {
                    JSONObject languageObject = SportsEquipmentObject.getJSONObject(locale);

                    // name
                    String name = languageObject.getString(TAG_NAME);
                    List<String> nameList = new ArrayList<String>();
                    nameList.add(name);

                    String description = null;

                    // description   
                    if (languageObject.has(TAG_DESCRIPTION)) {
                        //JSONObject descriptionJSONObject = languageObject.getJSONObject(TAG_DESCRIPTION);

                        // description
                        description = languageObject.getString(TAG_DESCRIPTION);

                    }

                    if (exerciseTag == null) {
                        exerciseTag = new ExerciseTag(new Locale(locale), nameList, description);
                    } else {
                        exerciseTag.addNames(new Locale(locale), nameList, description);
                    }
                }
            }

            // Log.d(TAG, "Finished parsing ExerciseTag: \n" + exerciseTag.toDebugString());
            exerciseTagList.add(exerciseTag);

        }

    } catch (JSONException e) {
        Log.e(TAG, "Error during parsing JSON File.", e);
        return null;
    }

    if (exerciseTagList.isEmpty())
        throw new AssertionError("JSON parsing failed: no ExerciseTag parsed.");
    return exerciseTagList;
}