Example usage for org.json JSONObject optJSONObject

List of usage examples for org.json JSONObject optJSONObject

Introduction

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

Prototype

public JSONObject optJSONObject(String key) 

Source Link

Document

Get an optional JSONObject associated with a key.

Usage

From source file:gov.sfmta.sfpark.DetailViewActivity.java

private void parseHours() throws JSONException {
    Log.v(TAG, "parsing hours");

    beg = null;//w w w  .  j  a v  a  2 s.co m
    end = null;
    from = null;
    to = null;

    JSONObject ophrs = annotation.allGarageData.optJSONObject("OPHRS");

    if (ophrs == null) {
        return;
    }

    listLayout.addView(headerText(getString(R.string.hours)));

    JSONArray opsArray = ophrs.optJSONArray("OPS");

    // its not an array, just one object
    if (opsArray == null) {
        JSONObject opsObject = ophrs.optJSONObject("OPS");

        // want null if fail not 'empty string' ?
        beg = opsObject.optString("BEG", null);
        end = opsObject.optString("END", null);
        from = opsObject.optString("FROM", null);
        to = opsObject.optString("TO", null);

        if (end != null) {
            end = "- " + end;
        } else {
            end = "";
        }

        if (to != null) {
            to = fixDay(to);
            to = "- " + to;
        } else {
            to = "";
        }

        if (beg == null)
            beg = "";
        if (from == null)
            from = "";

        row = fixDay(from) + " " + to + ": " + beg + " " + end;

        listLayout.addView(normalText(row));

        return;
    }

    int hoursRows = opsArray.length();
    for (int i = 0; i < hoursRows; i++) {
        JSONObject opsObject = opsArray.getJSONObject(i);

        // want null if fail not 'empty string' ?
        beg = opsObject.optString("BEG", null);
        end = opsObject.optString("END", null);
        from = opsObject.optString("FROM", null);
        to = opsObject.optString("TO", null);

        if (to != null) {
            row = fixDay(from) + " - " + fixDay(to) + ": " + beg + " - " + end;
        } else {
            row = fixDay(from) + ": " + beg + " - " + end;
        }

        listLayout.addView(normalText(row));
        hr();
    }

}

From source file:gov.sfmta.sfpark.DetailViewActivity.java

private void parseRates() throws JSONException {
    Log.v(TAG, "parsing rates");

    beg = null;// w  w  w .  j a  v  a  2s . com
    end = null;
    from = null;
    to = null;

    JSONObject rates = annotation.allGarageData.optJSONObject("RATES");

    displayedRates = false;

    if (rates == null) {
        return;
    }

    JSONArray rsArray = rates.optJSONArray("RS");

    if (rsArray == null) { //its not an array, just one object
        JSONObject rsObject = rates.optJSONObject("RS");

        // want null if fail not 'empty string' ?
        beg = rsObject.optString("BEG", null);
        end = rsObject.optString("END", null);
        rate = rsObject.optString("RATE", null);
        rq = rsObject.optString("RQ", null);

        float phr = Float.parseFloat(rate);

        if (beg != null) {
            listLayout.addView(headerText(getString(R.string.rates)));
            displayedRates = true;

            row = beg + " - " + end;

            if (phr == 0) {
                row2 = rq;
            } else {
                row2 = String.format("$%.2f hr", phr);
            }

            row = row + "  " + row2;

            if (annotation.inThisBucketBegin(beg, end)) {
                listLayout.addView(highlightText(row));
            } else {
                listLayout.addView(normalText(row));
            }
        }
        return;
    }

    int rsc = rsArray.length();

    for (int i = 0; i < rsc; i++) {
        JSONObject rsObject = rsArray.getJSONObject(i);

        // want null if fail not 'empty string' ?
        beg = rsObject.optString("BEG", null);
        end = rsObject.optString("END", null);
        rate = rsObject.optString("RATE", null);
        rq = rsObject.optString("RQ", null);

        float phr = Float.parseFloat(rate);

        if (beg != null) {
            if (!displayedRates) {
                listLayout.addView(headerText(getString(R.string.rates)));
                displayedRates = true;
            }

            row = beg + " - " + end;

            if (phr == 0) {
                row2 = rq;
            } else {
                row2 = String.format("$%.2f hr", phr);
            }

            row = row + "  " + row2;

            if (annotation.inThisBucketBegin(beg, end)) {
                listLayout.addView(highlightText(row));
            } else {
                listLayout.addView(normalText(row));
            }

            hr();
        }
    }
}

From source file:de.luhmer.owncloudnewsreader.reader.GoogleReaderApi.GoogleReaderMethods.java

@SuppressWarnings("unused")
public static ArrayList<RssFile> getFeeds(String _USERNAME, String _PASSWORD, String _TAG_LABEL) {
    Log.d("mygr", "METHOD: getUnreadFeeds()");

    ArrayList<RssFile> items = new ArrayList<RssFile>();

    String returnString = null;/*from w  w w . ja va 2s.c o m*/

    /*
    String _TAG_LABEL = null;
    try {
       //_TAG_LABEL = "stream/contents/user/" + AuthenticationManager.getGoogleUserID(_USERNAME, _PASSWORD) + "/state/com.google/reading-list?n=1000&r=n&xt=user/-/state/com.google/read";
       _TAG_LABEL = "stream/contents/user/-/state/com.google/reading-list?n=1000&r=n&xt=user/-/state/com.google/read";
    }catch(Exception e){
       e.printStackTrace();
    }*/

    try {

        HttpClient client = new DefaultHttpClient();
        HttpGet request = new HttpGet(GoogleReaderConstants._API_URL + _TAG_LABEL);
        request.addHeader("Authorization", GoogleReaderConstants._AUTHPARAMS
                + AuthenticationManager.getGoogleAuthKey(_USERNAME, _PASSWORD));

        HttpResponse response = client.execute(request);

        returnString = HttpHelper.request(response);

        try {
            JSONObject jObj = new JSONObject(returnString);
            JSONArray jItems = (JSONArray) jObj.get("items");
            for (int i = 0; i < jItems.length(); i++) {
                JSONObject jItem = jItems.getJSONObject(i);

                try {
                    String streamID = jItem.optJSONObject("origin").optString("streamId");
                    String feedTitel = jItem.getString("title");
                    String feedID = jItem.optString("id");
                    String content = "";
                    String link = "";
                    String timestamp = jItem.optString("published");

                    JSONObject jSummary = jItem.optJSONObject("summary");
                    JSONObject jContent = jItem.optJSONObject("content");

                    //JSONArray jCategories

                    if (jSummary != null)
                        content = (String) jItem.getJSONObject("summary").get("content");

                    if (jContent != null)
                        content = (String) jItem.getJSONObject("content").get("content");

                    //if(jItem.has("origin"));
                    //   link = (String) jItem.getJSONObject("origin").get("htmlUrl");

                    if (jItem.has("alternate"))
                        ;
                    link = (String) jItem.optJSONArray("alternate").getJSONObject(0).getString("href");

                    JSONArray jCategories = jItem.optJSONArray("categories");
                    List<String> categories = new ArrayList<String>();
                    if (jCategories != null) {
                        for (int t = 0; t < jCategories.length(); t++)
                            categories.add((String) jCategories.get(t));
                    }

                    Boolean starred = false;
                    Boolean read = false;

                    if (_TAG_LABEL.equals(Constants._TAG_LABEL_STARRED)) {
                        starred = true;
                        read = true;
                    }

                    content = content.replaceAll("<img[^>]*feedsportal.com.*>", "");
                    content = content
                            .replaceAll("<img[^>]*statisches.auslieferung.commindo-media-ressourcen.de.*>", "");
                    content = content.replaceAll("<img[^>]*auslieferung.commindo-media-ressourcen.de.*>", "");
                    content = content.replaceAll("<img[^>]*rss.buysellads.com.*>", "");

                    //content = (String) jItem.getJSONObject("content").get("content");
                    /*
                    RssFile sItem = new  RssFile(0, feedTitel, link, content, read, 0, feedID, categories, streamID, new Date(Long.parseLong(timestamp) * 1000), starred, null, null);//TODO implement this here again
                    items.add(sItem);*/
                } catch (Exception ex) {
                    ex.printStackTrace();
                }
            }
            //Log.d("HI", jObj.toString());
        } catch (JSONException e) {
            e.printStackTrace();
        }

        /*
        Pattern pattern = Pattern.compile("\"alternate\":\\[\\{\"href\":\"(.*?)\",");
        Matcher matcher = pattern.matcher(returnString);
                
        ArrayList<String> resultList = new ArrayList<String>();
                
        while (matcher.find())
           resultList.add(matcher.group(1));
                
        String[] ret = new String[resultList.size()];
        resultList.toArray(ret);*/
        //return ret;

    } catch (IOException e) {
        e.printStackTrace();
        return null;
    }
    return items;
}

From source file:com.danlvse.weebo.model.Comment.java

public static Comment parse(JSONObject jsonObject) {
    if (null == jsonObject) {
        return null;
    }/*from  w w w. ja  va2s . com*/

    Comment comment = new Comment();
    comment.created_at = jsonObject.optString("created_at");
    comment.id = jsonObject.optString("id");
    comment.text = jsonObject.optString("text");
    comment.source = jsonObject.optString("source");
    comment.source = getSource(jsonObject.optString("source"));
    comment.user = User.parse(jsonObject.optJSONObject("user"));
    comment.mid = jsonObject.optString("mid");
    comment.idstr = jsonObject.optString("idstr");
    comment.status = Feed.parse(jsonObject.optJSONObject("status"));
    comment.reply_comment = Comment.parse(jsonObject.optJSONObject("reply_comment"));

    return comment;
}

From source file:com.example.protocol.COMMENT.java

public void fromJson(JSONObject jsonObject) throws JSONException {
    if (null == jsonObject) {
        return;/*from w ww .j a v  a2  s .  c om*/
    }
    this.id = jsonObject.optInt("id");
    this.likes_count = jsonObject.optInt("likes_count");
    this.body = jsonObject.optString("body");
    this.created_at = jsonObject.optString("created_at");
    PLAYER player = new PLAYER();
    player.fromJson(jsonObject.optJSONObject("player"));
    this.player = player;
    return;
}

From source file:com.facebook.stream.StreamRenderer.java

/**
 * If it's a wall post on a friend's fall, renders
 * the recipient's name preceded by a '>'.
 * /* ww w . j a va  2s. co m*/
 * @param post
 * @throws JSONException
 */
private void renderTo(JSONObject post) throws JSONException {
    JSONObject to = post.optJSONObject("to");
    if (to != null) {
        JSONObject toData = to.getJSONArray("data").getJSONObject(0);
        String toName = toData.getString("name");
        String toId = toData.getString("id");
        append(" > ");
        renderProfileLink(toId, toName);
    }
}

From source file:com.facebook.stream.StreamRenderer.java

/**
 * Renders the post's comments./*from   w  ww .  j av a  2 s.  c o  m*/
 * 
 * @param post
 * @throws JSONException
 */
private void renderComments(JSONObject post) throws JSONException {
    append("<div class=\"comments\" id=\"comments" + post.optString("id") + "\">");
    JSONObject comments = post.optJSONObject("comments");
    if (comments != null) {
        JSONArray data = comments.optJSONArray("data");
        if (data != null) {
            for (int j = 0; j < data.length(); j++) {
                JSONObject comment = data.getJSONObject(j);
                renderComment(comment);
            }
        }
    }
    append("</div>");
}

From source file:com.facebook.stream.StreamRenderer.java

/**
 * Renders an individual comment.//from w ww.  j  ava 2s.co m
 * 
 * @param comment
 */
private void renderComment(JSONObject comment) {
    JSONObject from = comment.optJSONObject("from");
    if (from == null) {
        Log.w("StreamRenderer", "Comment missing from field: " + comment.toString());
    } else {
        String authorId = from.optString("id");
        String authorName = from.optString("name");
        renderAuthor(authorId, authorName);
    }
    String message = comment.optString("message");
    append("<div class=\"comment\">");
    String[] chunks = { "&nbsp;", message, "</div>" };
    append(chunks);
}

From source file:com.zotoh.core.util.JSONUte.java

/**
 * @param obj/*  w  w w  .j  av  a2s.  c o m*/
 * @param key
 * @return
 */
public static JSONObject getObject(JSONObject obj, String key) {
    return obj == null ? null : obj.optJSONObject(key);
}

From source file:com.zsxj.pda.ui.client.LoginActivity.java

@Override
public void onReponse(Object id, InputStream data) throws IOException {
    if (null != mUpdateDataId && mUpdateDataId.toString().equals(id.toString())) {

        try {/* www  .  j av a 2s. c o m*/
            String rtnData = new String(Util.readDataFromIS(data));
            if (null == rtnData || rtnData.equals("")) {
                sendCheckUpdateRequest();
                return;
            }

            JSONObject result = new JSONObject(rtnData);
            // Message return 
            Bundle bundle = new Bundle();
            bundle.putInt("update_type", result.optJSONObject("init_response").optInt("update_type"));
            bundle.putInt("update_method", result.optJSONObject("init_response").optInt("update_method"));
            bundle.putString("update_url", result.optJSONObject("init_response").optString("update_url"));
            bundle.putString("update_ver", result.optJSONObject("init_response").optString("update_ver"));
            bundle.putLong("update_size", result.optJSONObject("init_response").optLong("update_size"));
            bundle.putString("update_msg", result.optJSONObject("init_response").optString("update_msg"));
            Message message = new Message();
            message.what = HandlerCases.UPDATE_CHECK;
            message.setData(bundle);
            handler.sendMessage(message);
        } catch (Exception e) {
        }
    }
}