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:org.akop.ararat.io.WSJFormatter.java

@Override
public void read(Crossword.Builder builder, InputStream inputStream) throws IOException {
    InputStreamReader reader = new InputStreamReader(inputStream, mEncoding);

    StringBuilder sb = new StringBuilder();
    int nread;/*from w  w  w . ja v a  2s. co m*/
    char[] buffer = new char[4000];
    while ((nread = reader.read(buffer, 0, buffer.length)) > -1) {
        sb.append(buffer, 0, nread);
    }

    JSONObject obj;
    try {
        obj = new JSONObject(sb.toString());
    } catch (JSONException e) {
        throw new FormatException("Error parsing JSON object", e);
    }

    JSONObject dataObj = obj.optJSONObject("data");
    if (dataObj == null) {
        throw new FormatException("Missing 'data'");
    }

    JSONObject copyObj = dataObj.optJSONObject("copy");
    if (copyObj == null) {
        throw new FormatException("Missing 'data.copy'");
    }

    JSONObject gridObj = copyObj.optJSONObject("gridsize");
    if (gridObj == null) {
        throw new FormatException("Missing 'data.copy.gridsize'");
    }

    builder.setTitle(copyObj.optString("title"));
    builder.setDescription(copyObj.optString("description"));
    builder.setCopyright(copyObj.optString("publisher"));
    builder.setAuthor(copyObj.optString("byline"));

    String pubString = copyObj.optString("date-publish");
    try {
        builder.setDate(PUBLISH_DATE_FORMAT.parse(pubString).getTime());
    } catch (ParseException e) {
        throw new FormatException("Can't parse '" + pubString + "' as publish date");
    }

    int width = gridObj.optInt("cols");
    int height = gridObj.optInt("rows");

    builder.setWidth(width);
    builder.setHeight(height);

    readClues(builder, copyObj, Grid.parseJSON(dataObj.optJSONArray("grid"), width, height));
}

From source file:com.appsimobile.appsii.module.weather.ImageDownloadHelper.java

public static void getEligiblePhotosFromResponse(@Nullable JSONObject jsonObject, List<PhotoInfo> result,
        int minDimension) {
    result.clear();/* ww w . j  a  v  a 2  s.  co m*/

    if (jsonObject == null)
        return;

    JSONObject photos = jsonObject.optJSONObject("photos");
    if (photos == null)
        return;

    JSONArray photoArr = photos.optJSONArray("photo");
    if (photoArr == null)
        return;

    int N = photoArr.length();
    for (int i = 0; i < N; i++) {
        JSONObject object = photoArr.optJSONObject(i);
        if (object == null)
            continue;
        String id = object.optString("id");
        if (TextUtils.isEmpty(id))
            continue;
        String urlH = urlFromImageObject(object, "url_h", "width_h", "height_h", minDimension - 100);
        String urlO = urlFromImageObject(object, "url_o", "width_o", "height_o", minDimension - 100);

        if (urlH != null) {
            result.add(new PhotoInfo(id, urlH));
        } else if (urlO != null) {
            result.add(new PhotoInfo(id, urlO));
        }
    }

}

From source file:org.catnut.metadata.User.java

@Override
public ContentValues convert(JSONObject json) {
    ContentValues user = new ContentValues();
    user.put(BaseColumns._ID, json.optLong(Constants.ID));
    user.put(screen_name, json.optString(screen_name));
    user.put(name, json.optString(name));
    user.put(province, json.optInt(province));
    user.put(city, json.optInt(city));/*from   w  w  w  . j a v a 2 s .  c o m*/
    user.put(location, json.optString(location));
    user.put(description, json.optString(description));
    user.put(url, json.optString(url));
    user.put(profile_image_url, json.optString(profile_image_url));
    user.put(cover_image, json.optString(cover_image));
    user.put(cover_image_phone, json.optString(cover_image_phone));
    user.put(profile_url, json.optString(profile_url));
    user.put(domain, json.optString(domain));
    user.put(weihao, json.optString(weihao));
    user.put(gender, json.optString(gender));
    user.put(followers_count, json.optInt(followers_count));
    user.put(friends_count, json.optInt(friends_count));
    user.put(statuses_count, json.optInt(statuses_count));
    user.put(favourites_count, json.optInt(favourites_count));
    user.put(created_at, json.optString(created_at));
    user.put(following, json.optBoolean(following));
    user.put(allow_all_act_msg, json.optBoolean(allow_all_act_msg));
    user.put(geo_enabled, json.optBoolean(geo_enabled));
    user.put(verified, json.optBoolean(verified));
    user.put(verified_type, json.optInt(verified_type));
    user.put(remark, json.optString(remark));
    //      user.put(ptype, json.optInt(ptype));
    user.put(allow_all_comment, json.optBoolean(allow_all_comment));
    user.put(avatar_large, json.optString(avatar_large));
    user.put(avatar_hd, json.optString(avatar_hd));
    user.put(verified_reason, json.optString(verified_reason));
    user.put(follow_me, json.optBoolean(follow_me));
    user.put(online_status, json.optInt(online_status));
    user.put(bi_followers_count, json.optInt(bi_followers_count));
    user.put(lang, json.optString(lang));
    //      user.put(star, json.optString(star));
    //      user.put(mbtype, json.optInt(mbtype));
    //      user.put(mbrank, json.optInt(mbrank));
    //      user.put(block_word, json.optInt(block_word));
    // ?id
    if (json.has(SINGLE)) {
        user.put(status_id, json.optJSONObject(Status.SINGLE).optLong(Constants.ID));
    }
    return user;
}

From source file:br.com.indigo.android.facebook.SocialFacebook.java

private void handleFeedResponse(JSONObject jsonResponse, FeedListener listener) {
    FbSimplePost post;//w ww.ja  v a 2  s.  co m
    ArrayList<FbSimplePost> posts = new ArrayList<FbSimplePost>();

    try {
        JSONArray objs = jsonResponse.getJSONArray("data");

        for (int i = 0; i < objs.length(); i++) {
            JSONObject obj = objs.getJSONObject(i);

            post = new FbSimplePost();
            post.setId(obj.getString("id"));

            JSONObject fromJson = obj.optJSONObject("from");
            if (fromJson != null) {
                FbSimpleUser from = new FbSimpleUser();
                from.setId(fromJson.getString("id"));
                from.setName(fromJson.getString("name"));

                post.setFrom(from);
            }

            post.setMessage(obj.optString("message"));
            post.setPicture(obj.optString("picture"));
            post.setLink(obj.optString("link"));
            post.setName(obj.optString("name"));
            post.setCaption(obj.optString("caption"));
            post.setDescription(obj.optString("description"));
            post.setSource(obj.optString("source"));
            post.setType(obj.optString("type"));

            post.setCreatedTime(new Date(obj.getLong("created_time")));
            post.setUpdatedTime(new Date(obj.getLong("updated_time")));

            JSONObject comments = obj.optJSONObject("comments");
            if (comments != null) {
                post.setNumberOfComments(comments.getInt("count"));
            }

            JSONObject likes = obj.optJSONObject("likes");
            if (likes != null) {
                post.setNumberOfLikes(likes.getInt("count"));
            }

            posts.add(post);
        }

        String nextPage = null;
        JSONObject paging = jsonResponse.optJSONObject("paging");
        if (paging != null) {
            nextPage = paging.optString("next");
        }

        listener.onComplete(posts, nextPage);

    } catch (JSONException e) {
        Util.logd(TAG, "Could not parse Json response", e);
        listener.onFail(e);
    }
}

From source file:br.com.indigo.android.facebook.SocialFacebook.java

private void handleUsersResponse(JSONObject jsonResponse, UsersListener listener) {

    FbSimpleUser user = null;//from  w w  w .  j av a 2s  . co  m
    ArrayList<FbSimpleUser> friends = new ArrayList<FbSimpleUser>();

    try {
        JSONArray objs = jsonResponse.getJSONArray("data");

        for (int i = 0; i < objs.length(); i++) {
            JSONObject obj = objs.getJSONObject(i);

            user = new FbSimpleUser();
            user.setId(obj.getString("id"));
            user.setName(obj.getString("name"));

            String rsvpStatus = obj.optString("rsvp_status");

            if (rsvpStatus != null) {
                if (rsvpStatus.equals("not_replied")) {
                    user.setRSVPStatus(RSVP_STATUS.NOT_REPLIED);
                } else if (rsvpStatus.equals("attending")) {
                    user.setRSVPStatus(RSVP_STATUS.ATTENDING);
                } else if (rsvpStatus.equals("declined")) {
                    user.setRSVPStatus(RSVP_STATUS.DECLINED);
                } else if (rsvpStatus.equals("unsure")) {
                    user.setRSVPStatus(RSVP_STATUS.MAYBE);
                }
            }

            friends.add(user);
        }

        String nextPage = null;
        JSONObject paging = jsonResponse.optJSONObject("paging");
        if (paging != null) {
            nextPage = paging.optString("next");
        }

        listener.onComplete(friends, nextPage);

    } catch (JSONException e) {
        Util.logd(TAG, "Could not parse Json response", e);
        listener.onFail(e);
    }
}

From source file:br.com.indigo.android.facebook.SocialFacebook.java

private void handleEventsResponse(JSONObject jsonResponse, EventsListener listener) {

    ArrayList<FbEvent> events = new ArrayList<FbEvent>();

    try {// w w  w .  j  av a2  s.  com
        JSONArray objs = jsonResponse.getJSONArray("data");

        for (int i = 0; i < objs.length(); i++) {
            JSONObject obj = objs.getJSONObject(i);

            events.add(parseEvent(obj));
        }

        String nextPage = null;
        JSONObject paging = jsonResponse.optJSONObject("paging");
        if (paging != null) {
            nextPage = paging.optString("next");
        }

        listener.onComplete(events, nextPage);

    } catch (JSONException e) {
        Util.logd(TAG, "Could not parse Json response", e);
        listener.onFail(e);
    }
}

From source file:br.com.indigo.android.facebook.SocialFacebook.java

private FbEvent parseEvent(JSONObject eventJson) throws JSONException {
    FbEvent event = new FbEvent();

    event.setId(eventJson.getString("id"));
    event.setName(eventJson.getString("name"));
    event.setDescription(eventJson.optString("description"));
    event.setStartTime(dateWithFacebookUnixTimestamp(eventJson.getLong("start_time")));
    event.setEndTime(dateWithFacebookUnixTimestamp(eventJson.optLong("end_time")));
    event.setLocation(eventJson.optString("location"));
    event.setPrivacy(eventJson.optString("privacy"));

    JSONObject ownerJson = eventJson.optJSONObject("owner");
    if (ownerJson != null) {
        FbSimpleUser owner = new FbSimpleUser();
        owner.setId(ownerJson.getString("id"));
        owner.setName(ownerJson.optString("name"));
        event.setOwner(owner);//from   w  w  w  .  ja  va 2  s.c  om
    }

    return event;
}

From source file:br.com.indigo.android.facebook.SocialFacebook.java

private void handleCommentsResponse(JSONObject jsonResponse, CommentsListener listener) {

    FbComment comment = null;/*from  w w w  .  j a  v a2  s .  com*/
    ArrayList<FbComment> comments = new ArrayList<FbComment>();

    try {
        JSONArray objs = jsonResponse.getJSONArray("data");

        for (int i = 0; i < objs.length(); i++) {
            JSONObject obj = objs.getJSONObject(i);

            comment = new FbComment();
            comment.setId(obj.getString("id"));
            comment.setMessage(obj.optString("message"));
            comment.setCreatedTime(new Date(obj.optLong("created_time") * 1000));
            comment.setNumberOfLikes(obj.optInt("likes"));

            JSONObject fromJson = obj.optJSONObject("from");
            if (fromJson != null) {
                FbSimpleUser fromUser = new FbSimpleUser();
                fromUser.setId(fromJson.getString("id"));
                fromUser.setName(fromJson.optString("name"));

                comment.setFrom(fromUser);
            }

            comments.add(comment);
        }

        String nextPage = null;
        JSONObject paging = jsonResponse.optJSONObject("paging");
        if (paging != null) {
            nextPage = paging.optString("next");
        }

        listener.onComplete(comments, nextPage);

    } catch (JSONException e) {
        Util.logd(TAG, "Could not parse Json response", e);
        listener.onFail(e);
    }
}

From source file:de.luhmer.owncloudnewsreader.reader.owncloud.GetVersion_v1.java

@Override
public boolean performAction(JSONObject jObj) {
    this.version = jObj.optJSONObject("ocs").optJSONObject("data").optString("version");
    return true;/* w w  w  . j  a v  a 2  s  .c  om*/
}

From source file:com.example.m.niceproject.data.Item.java

@Override
public void populate(JSONObject data) {
    condition = new Condition();
    condition.populate(data.optJSONObject("condition"));
}