Example usage for org.json JSONObject optJSONArray

List of usage examples for org.json JSONObject optJSONArray

Introduction

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

Prototype

public JSONArray optJSONArray(String key) 

Source Link

Document

Get an optional JSONArray associated with a key.

Usage

From source file:org.catnut.fragment.HomeTimelineFragment.java

private void loadFromCloud(long max_id) {
    mSwipeRefreshLayout.setRefreshing(true);
    CatnutAPI api = TweetAPI.homeTimeline(0, max_id, getFetchSize(), 0, 0, 0, 0);
    mRequestQueue.add(new CatnutRequest(getActivity(), api, new StatusProcessor.TimelineProcessor(true),
            new Response.Listener<JSONObject>() {
                @Override/*ww  w  .  j av a2s  .  com*/
                public void onResponse(JSONObject response) {
                    Log.d(TAG, "load more from cloud done...");
                    mTotal = response.optInt(TOTAL_NUMBER);
                    int newSize = response.optJSONArray(Status.MULTIPLE).length() + mAdapter.getCount();
                    Bundle args = new Bundle();
                    args.putInt(TAG, newSize);
                    getLoaderManager().restartLoader(0, args, HomeTimelineFragment.this);
                }
            }, errorListener)).setTag(TAG);
}

From source file:com.quantcast.measurement.service.QCLocation.java

private MeasurementLocation parseJson(String jsonString) throws JSONException {
    JSONObject json = new JSONObject(jsonString);
    if (OK.equals(json.optString(STATUS))) {
        JSONArray resultsArray = json.optJSONArray(RESULTS);
        if (resultsArray != null) {
            JSONArray addressComponents;
            for (int i = 0; i < resultsArray.length(); i++) {
                addressComponents = resultsArray.getJSONObject(i).optJSONArray(ADDRESS);
                if (addressComponents != null) {
                    String country = "", locality = "", state = "", postalCode = "";
                    for (int j = 0; j < addressComponents.length(); j++) {
                        JSONObject obj = addressComponents.getJSONObject(j);
                        JSONArray types = obj.optJSONArray(TYPES);
                        if (types != null) {
                            if (containsString(types, LOCALITY))
                                locality = obj.getString(SHORT_NAME);
                            if (containsString(types, STATE))
                                state = obj.getString(SHORT_NAME);
                            if (containsString(types, COUNTRY))
                                country = obj.getString(SHORT_NAME);
                            if (containsString(types, POSTAL_CODE))
                                postalCode = obj.getString(SHORT_NAME);
                        }//from  w w w  .  j  ava 2s .com
                    }
                    return new MeasurementLocation(country, state, locality, postalCode);
                }
            }
        }
    }
    return null;
}

From source file:com.vk.sdkweb.api.model.VKApiArray.java

@Override
public VKApiModel parse(JSONObject object) {
    try {/*from  w w w. j a va  2  s  . c  o m*/
        JSONArray jsonArray;
        if ((jsonArray = object.optJSONArray("response")) == null) {
            object = object.getJSONObject("response");
            count = object.getInt("count");
            jsonArray = object.getJSONArray("items");
        }
        parse(jsonArray);

    } catch (JSONException e) {
        if (VKSdk.DEBUG)
            e.printStackTrace();
    }
    fields = object;
    return this;
}

From source file:com.rapid.actions.Timer.java

public Timer(RapidHttpServlet rapidServlet, JSONObject jsonAction) throws Exception {
    // call super constructor to set xml version
    super();//from   ww w  .j  a v a2s .  co m
    // save all key/values from the json into the properties 
    for (String key : JSONObject.getNames(jsonAction)) {
        // add all json properties to our properties (except for sessionVariables)
        if (!"actions".equals(key))
            addProperty(key, jsonAction.get(key).toString());
    }
    // grab any actions
    JSONArray jsonActions = jsonAction.optJSONArray("actions");
    // if we had some instantiate our collection
    if (jsonActions != null)
        _actions = Control.getActions(rapidServlet, jsonActions);

}

From source file:com.vk.sdkweb.api.model.VKList.java

/**
 * Fills list according with data in {@code from}.
 * @param from an object that represents a list adopted in accordance with VK API format. You can use null.
 * @param clazz class represents a model that has a public constructor with {@link org.json.JSONObject} argument.
 *//*from   www. j a va 2  s .c o  m*/
public void fill(JSONObject from, Class<? extends T> clazz) {
    if (from.has("response")) {
        JSONArray array = from.optJSONArray("response");
        if (array != null) {
            fill(array, clazz);
        } else {
            fill(from.optJSONObject("response"), clazz);
        }
    } else {
        fill(from, new ReflectParser<T>(clazz));
    }
}

From source file:com.vk.sdkweb.api.model.VKList.java

/**
 * Fills list according with data in {@code from}.
 * @param from an object that represents a list adopted in accordance with VK API format. You can use null.
 * @param creator interface implementation to parse objects.
 *//*from   w  ww  . jav  a  2  s.  co  m*/
public void fill(JSONObject from, Parser<? extends T> creator) {
    if (from != null) {
        fill(from.optJSONArray("items"), creator);
        count = from.optInt("count", count);
    }
}

From source file:com.jsonstore.api.JSONStoreFindOptions.java

/**
 * @exclude Used internally/*  w  w  w .jav a 2  s .  c om*/
 */
public JSONStoreFindOptions(JSONObject options) throws JSONException, JSONStoreInvalidSortObjectException {

    filter = new HashMap<String, Boolean>();
    sort = new LinkedHashMap<String, SortDirection>();

    String limitStr = options.optString(JSONStoreFindOptions.OPTION_LIMIT, null);
    if (limitStr != null) {
        Integer limitParse = Integer.parseInt(limitStr);
        setLimit(limitParse);
    }

    String offsetStr = options.optString(JSONStoreFindOptions.OPTION_OFFSET, null);
    if (offsetStr != null) {
        Integer offsetParse = Integer.parseInt(offsetStr);
        setOffset(offsetParse);
    }

    JSONArray sortArray = options.optJSONArray(JSONStoreFindOptions.OPTION_SORT_ARRAY);
    if (sortArray != null) {
        for (int idx = 0; idx < sortArray.length(); idx++) {
            JSONObject sortObject = sortArray.getJSONObject(idx);

            Iterator<String> keys = sortObject.keys();
            String key = keys.next();

            if (keys.hasNext()) {
                throw new JSONStoreInvalidSortObjectException(
                        "One of the sort objects in the sort array has more than one field.");
            }

            //Parse the direction of the sort for this search field:
            String sortDirectionStr = sortObject.getString(key);
            if (sortDirectionStr.equalsIgnoreCase(DatabaseConstants.ASCENDING)) {
                sortBySearchFieldAscending(key);
            } else if (sortDirectionStr.equalsIgnoreCase(DatabaseConstants.DESCENDING)) {
                sortBySearchFieldDescending(key);
            } else {
                throw new JSONStoreInvalidSortObjectException(
                        "Invalid sorting direction (ascending or descending) specified.");
            }
        }

    }

    JSONArray filterParse = options.optJSONArray(JSONStoreFindOptions.OPTION_FILTER);
    if (filterParse != null) {
        for (int idx = 0; idx < filterParse.length(); idx++) {
            addSearchFilter(filterParse.getString(idx));
        }
    }
}

From source file:org.catnut.fragment.FavoriteFragment.java

@Override
protected void refresh() {
    // ???/*from  ww w.j a  va 2 s .com*/
    if (!isNetworkAvailable()) {
        Toast.makeText(getActivity(), getString(R.string.network_unavailable), Toast.LENGTH_SHORT).show();
        initFromLocal();
        return;
    }
    // ??
    mCurrentPage = 0;
    mDeleteCount = 0;
    mTotal = 0;

    // go go go
    mRequestQueue.add(new CatnutRequest(getActivity(), FavoritesAPI.favorites(getFetchSize(), mCurrentPage),
            new StatusProcessor.FavoriteTweetsProcessor(), new Response.Listener<JSONObject>() {
                @Override
                public void onResponse(JSONObject response) {
                    Log.d(TAG, "refresh done...");
                    mDeleteCount += response.optInt(TAG);
                    mTotal = response.optInt(TOTAL_NUMBER);
                    // ???
                    JSONArray jsonArray = response.optJSONArray(Status.FAVORITES);
                    int newSize = jsonArray.length(); // ...
                    Bundle args = new Bundle();
                    args.putInt(TAG, newSize);
                    getLoaderManager().restartLoader(0, args, FavoriteFragment.this);
                }
            }, errorListener)).setTag(TAG);
}

From source file:org.catnut.fragment.FavoriteFragment.java

private void loadFromCloud() {
    mSwipeRefreshLayout.setRefreshing(true);
    mRequestQueue.add(new CatnutRequest(getActivity(), FavoritesAPI.favorites(getFetchSize(), mCurrentPage),
            new StatusProcessor.FavoriteTweetsProcessor(), new Response.Listener<JSONObject>() {
                @Override/*from   w w  w. j a  v a 2 s.  c  o m*/
                public void onResponse(JSONObject response) {
                    Log.d(TAG, "load more from cloud done...");
                    mDeleteCount += response.optInt(TAG);
                    mTotal = response.optInt(TOTAL_NUMBER);
                    int newSize = response.optJSONArray(Status.FAVORITES).length() + mAdapter.getCount();
                    Bundle args = new Bundle();
                    args.putInt(TAG, newSize);
                    getLoaderManager().restartLoader(0, args, FavoriteFragment.this);
                }
            }, errorListener)).setTag(TAG);
}

From source file:com.sina.weibo.sdk_lib.openapi.models.Favorite.java

public static Favorite parse(JSONObject jsonObject) {
    if (null == jsonObject) {
        return null;
    }/*from www. j  av a 2 s.  c o  m*/

    Favorite favorite = new Favorite();
    favorite.status = Status.parse(jsonObject.optJSONObject("status"));
    favorite.favorited_time = jsonObject.optString("favorited_time");

    JSONArray jsonArray = jsonObject.optJSONArray("tags");
    if (jsonArray != null && jsonArray.length() > 0) {
        int length = jsonArray.length();
        favorite.tags = new ArrayList<Tag>(length);
        for (int ix = 0; ix < length; ix++) {
            favorite.tags.add(Tag.parse(jsonArray.optJSONObject(ix)));
        }
    }

    return favorite;
}