Example usage for org.json JSONArray optJSONObject

List of usage examples for org.json JSONArray optJSONObject

Introduction

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

Prototype

public JSONObject optJSONObject(int index) 

Source Link

Document

Get the optional JSONObject associated with an index.

Usage

From source file:com.soomla.levelup.LevelUp.java

private static HashMap<String, JSONObject> getListFromWorlds(JSONObject model, String listName) {
    HashMap<String, JSONObject> resultHash = new HashMap<String, JSONObject>();

    HashMap<String, JSONObject> worldJSONs = getWorlds(model);

    for (JSONObject worldJSON : worldJSONs.values()) {
        JSONArray objectJSONs = worldJSON.optJSONArray(listName);
        if (objectJSONs != null) {
            for (int i = 0; i < objectJSONs.length(); i++) {
                JSONObject objectJSON = objectJSONs.optJSONObject(i);
                if (objectJSON != null) {
                    String objectId = objectJSON.optString("itemId");
                    if (!TextUtils.isEmpty(objectId)) {
                        resultHash.put(objectId, objectJSON);
                    }/*  w ww .  j a v  a 2s .  c o m*/
                }
            }
        }
    }

    return resultHash;
}

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

/**
 * ??/*  ww w.j a v a 2  s. c o m*/
 *
 * @param thumb url
 * @param originalUrl url
 */
private void loadThumbs(String thumb, final String originalUrl, final ImageView thumbs,
        final JSONArray jsonArray, View overflow) {
    if (!TextUtils.isEmpty(thumb)) {
        if (mStayInLatest) {
            int screenWidth = CatnutUtils.getScreenWidth(getActivity());
            Picasso.with(getActivity()).load(thumb).into(thumbs);
            thumbs.setOnTouchListener(new View.OnTouchListener() {
                @Override
                public boolean onTouch(View v, MotionEvent event) {
                    return CatnutUtils.imageOverlay(v, event);
                }
            });
            thumbs.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    thumbs.getDrawable().clearColorFilter();
                    mText.invalidate();
                    Intent intent = SingleFragmentActivity.getIntent(getActivity(),
                            SingleFragmentActivity.PHOTO_VIEWER);
                    intent.putExtra(Constants.PIC, originalUrl);
                    startActivity(intent);
                }
            });
        } else {
            thumbs.setImageResource(R.drawable.error);
        }
        thumbs.setVisibility(View.VISIBLE);
    } else {
        thumbs.setVisibility(View.GONE);
        overflow.setVisibility(View.GONE);
    }
    if (jsonArray != null && jsonArray.length() > 1) {
        overflow.setVisibility(View.VISIBLE);
        overflow.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                View view = LayoutInflater.from(getActivity()).inflate(R.layout.tweet_pics, null);
                LinePageIndicator indicator = (LinePageIndicator) view.findViewById(R.id.indicator);
                final ViewPager pager = (ViewPager) view.findViewById(R.id.pager);
                pager.setPageTransformer(true, new PageTransformer.DepthPageTransformer());
                pager.setAdapter(new PagerAdapter() {
                    @Override
                    public int getCount() {
                        return jsonArray.length();
                    }

                    @Override
                    public boolean isViewFromObject(View view, Object object) {
                        return view == object;
                    }

                    @Override
                    public void destroyItem(ViewGroup container, int position, Object object) {
                        container.removeView((View) object);
                    }

                    @Override
                    public Object instantiateItem(ViewGroup container, int position) {
                        View v = LayoutInflater.from(getActivity()).inflate(R.layout.photo, null);
                        TouchImageView iv = (TouchImageView) v.findViewById(R.id.image);
                        String url = jsonArray.optJSONObject(position).optString(Status.thumbnail_pic);
                        String replace = url.replace(Constants.THUMBNAIL, Status.MEDIUM_THUMBNAIL);
                        Picasso.with(getActivity()).load(replace).into(iv);
                        container.addView(v);
                        return v;
                    }
                });
                indicator.setViewPager(pager);
                new AlertDialog.Builder(
                        new ContextThemeWrapper(getActivity(), android.R.style.Theme_Holo_Dialog)).setView(view)
                                .setPositiveButton(getString(R.string.original_pics),
                                        new DialogInterface.OnClickListener() {
                                            @Override
                                            public void onClick(DialogInterface dialog, int which) {
                                                ArrayList<Uri> urls = new ArrayList<Uri>(jsonArray.length());
                                                for (int i = 0; i < jsonArray.length(); i++) {
                                                    String s = jsonArray.optJSONObject(i)
                                                            .optString(Status.thumbnail_pic)
                                                            .replace(Constants.THUMBNAIL,
                                                                    Status.LARGE_THUMBNAIL);
                                                    urls.add(Uri.parse(s));
                                                }
                                                Intent intent = SingleFragmentActivity.getIntent(getActivity(),
                                                        SingleFragmentActivity.GALLERY);
                                                intent.putExtra(GalleryPagerFragment.CUR_INDEX,
                                                        pager.getCurrentItem());
                                                intent.putExtra(GalleryPagerFragment.URLS, urls);
                                                intent.putExtra(GalleryPagerFragment.TITLE,
                                                        getString(R.string.tweet_pics));
                                                startActivity(intent);
                                            }
                                        })
                                .setNegativeButton(getString(R.string.close), null).show();
            }
        });
    } else {
        overflow.setVisibility(View.GONE);
    }
}

From source file:com.mikecorrigan.trainscorekeeper.XPagerAdapter.java

public XPagerAdapter(final Context context, final FragmentManager fm, final JSONArray jsonTabs) {
    super(fm);/*from   w w w  .  j av a2s. com*/
    Log.vc(VERBOSE, TAG, "ctor: fm=" + fm + ", jsonTabs=" + jsonTabs);

    // Add two for Summary and History tabs.
    pageSpecs = new PageSpec[jsonTabs.length() + 2];

    int i = 0;

    // Add button fragments.
    for (; i < jsonTabs.length(); i++) {
        JSONObject jsonTab = jsonTabs.optJSONObject(i);
        if (jsonTab == null) {
            continue;
        }
        final String name = jsonTab.optString(JsonSpec.TAB_NAME, JsonSpec.DEFAULT_TAB_NAME);
        pageSpecs[i] = new PageSpec(FragmentButton.class, name, jsonTab);
    }

    // Add summary fragment.
    pageSpecs[i] = new PageSpec(FragmentSummary.class, context.getResources().getString(R.string.summary),
            new JSONObject());
    i++;

    // Add history fragment.
    pageSpecs[i] = new PageSpec(FragmentHistory.class, context.getResources().getString(R.string.history),
            new JSONObject());
    i++;
}

From source file:se.chalmers.watchmetest.net.IMDBHandlerTest.java

public void testGetMovieByTitle() {
    JSONArray json = this.imdb.getMoviesByTitle("casino royale");

    JSONObject movie = json.optJSONObject(0);
    assertNotNull(movie);/*from   w  w w . j ava  2  s  . c om*/
}

From source file:co.mwater.foregroundcameraplugin.ForegroundCameraLauncher.java

/**
 * Executes the request and returns PluginResult.
 * /*from  www . j a  va2  s .c  om*/
 * @param action
 *            The action to execute.
 * @param args
 *            JSONArry of arguments for the plugin.
 * @param callbackId
 *            The callback id used when calling back into JavaScript.
 * @return A PluginResult object with a status and message.
 */
@Override
public boolean execute(String action, JSONArray args, CallbackContext callbackContext) throws JSONException {
    PluginResult.Status status = PluginResult.Status.OK;
    String result = "";
    this.callbackContext = callbackContext;

    try {
        if (action.equals("takePicture")) {
            this.targetHeight = 0;
            this.targetWidth = 0;
            this.mQuality = 80;

            JSONObject options = args.optJSONObject(0);
            if (options != null) {
                this.targetHeight = options.getInt("targetHeight");
                this.targetWidth = options.getInt("targetWidth");
                this.mQuality = options.getInt("quality");
            }

            this.takePicture();

            PluginResult r = new PluginResult(PluginResult.Status.NO_RESULT);
            r.setKeepCallback(true);
            callbackContext.sendPluginResult(r);
            return true;
        }
        return false;
    } catch (JSONException e) {
        e.printStackTrace();
        callbackContext.sendPluginResult(new PluginResult(PluginResult.Status.JSON_EXCEPTION));
        return true;
    }
}

From source file:org.uiautomation.ios.context.BaseWebInspector.java

public List<Cookie> getCookies() {

    List<Cookie> res = new ArrayList<Cookie>();
    JSONObject o = sendCommand(Page.getCookies());
    JSONArray cookies = o.optJSONArray("cookies");
    if (cookies != null) {
        for (int i = 0; i < cookies.length(); i++) {
            JSONObject cookie = cookies.optJSONObject(i);
            String name = cookie.optString("name");
            String value = cookie.optString("value");
            String domain = cookie.optString("domain");
            String path = cookie.optString("path");
            Date expiry = new Date(cookie.optLong("expires"));
            boolean isSecure = cookie.optBoolean("secure");
            Cookie c = new Cookie(name, value, domain, path, expiry, isSecure);
            res.add(c);//  w ww .ja va 2s.  c o m
        }
        return res;
    } else {
        // TODO
    }
    return null;
}

From source file:synapticloop.b2.response.B2ListFilesResponse.java

/**
 * Instantiate a list files response with the JSON response as a 
 * string from the API call.  This response is then parsed into the 
 * relevant fields.// ww  w .ja va  2s. com
 * 
 * @param json the response (in JSON format)
 * 
 * @throws B2ApiException if there was an error parsing the response
 */
public B2ListFilesResponse(String json) throws B2ApiException {
    super(json);

    this.nextFileName = this.readString(B2ResponseProperties.KEY_NEXT_FILE_NAME);
    this.nextFileId = this.readString(B2ResponseProperties.KEY_NEXT_FILE_ID);

    JSONArray filesArray = this.readObjects(B2ResponseProperties.KEY_FILES);

    files = new ArrayList<B2FileInfoResponse>();
    for (int i = 0; i < filesArray.length(); i++) {
        files.add(new B2FileInfoResponse(filesArray.optJSONObject(i)));
    }

    this.warnOnMissedKeys();
}

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

/**
 * Returns the available actions for the post.
 * //ww w  .j a v a  2 s.c o m
 * @param post
 * @return
 */
private HashSet<String> getActions(JSONObject post) {
    HashSet<String> actionsSet = new HashSet<String>();
    JSONArray actions = post.optJSONArray("actions");
    if (actions != null) {
        for (int j = 0; j < actions.length(); j++) {
            JSONObject action = actions.optJSONObject(j);
            String actionName = action.optString("name");
            actionsSet.add(actionName);
        }
    }
    return actionsSet;
}

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

/**
 * @param arr/*w  w w .j ava  2  s  . c om*/
 * @param pos
 * @return
 */
public static JSONObject getObject(JSONArray arr, Integer pos) {
    return arr == null ? null : arr.optJSONObject(pos);
}

From source file:org.apache.cordova.mediacapture.Capture.java

@Override
public boolean execute(String action, JSONArray args, CallbackContext callbackContext) throws JSONException {
    this.callbackContext = callbackContext;
    this.limit = 1;
    this.duration = 0;
    this.results = new JSONArray();

    JSONObject options = args.optJSONObject(0);
    if (options != null) {
        limit = options.optLong("limit", 1);
        duration = options.optInt("duration", 0);
    }//  w ww  .  j  a v a 2s.c o m

    if (action.equals("getFormatData")) {
        JSONObject obj = getFormatData(args.getString(0), args.getString(1));
        callbackContext.success(obj);
        return true;
    } else if (action.equals("captureAudio")) {
        this.captureAudio();
    } else if (action.equals("captureImage")) {
        this.captureImage();
    } else if (action.equals("captureVideo")) {
        this.captureVideo(duration);
    } else {
        return false;
    }

    return true;
}