Example usage for org.json JSONArray get

List of usage examples for org.json JSONArray get

Introduction

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

Prototype

public Object get(int index) throws JSONException 

Source Link

Document

Get the object value associated with an index.

Usage

From source file:org.jabsorb.ng.serializer.impl.SetSerializer.java

@Override
public Object unmarshall(final SerializerState state, final Class<?> clazz, final Object o)
        throws UnmarshallException {

    final JSONArray jsonset;
    final Set<Object> abset;
    if (o instanceof JSONArray) {
        jsonset = (JSONArray) o;// w w  w  . j  av  a  2s. c o m
        abset = new LinkedHashSet<Object>();

    } else if (o instanceof JSONObject) {
        final JSONObject jso = (JSONObject) o;
        String java_class;

        // Hint check
        try {
            java_class = jso.getString("javaClass");
        } catch (final JSONException e) {
            throw new UnmarshallException("Could not read javaClass", e);
        }

        if (java_class == null) {
            throw new UnmarshallException("no type hint");
        }

        // Create the set
        if (java_class.equals("java.util.Set") || java_class.equals("java.util.AbstractSet")
                || java_class.equals("java.util.HashSet")) {
            abset = new HashSet<Object>();
        } else if (java_class.equals("java.util.TreeSet")) {
            abset = new TreeSet<Object>();
        } else if (java_class.equals("java.util.LinkedHashSet")) {
            abset = new LinkedHashSet<Object>();
        } else {
            throw new UnmarshallException("not a Set");
        }

        // Parse the JSON set
        try {
            jsonset = jso.getJSONArray("set");

        } catch (final JSONException e) {
            throw new UnmarshallException("set missing", e);
        }
    } else {
        throw new UnmarshallException("Given object is not JSON object/array");
    }

    if (jsonset == null) {
        throw new UnmarshallException("set missing");
    }

    state.setSerialized(o, abset);

    int idx = 0;
    try {
        for (idx = 0; idx < jsonset.length(); idx++) {
            abset.add(ser.unmarshall(state, null, jsonset.get(idx)));
        }
    } catch (final UnmarshallException e) {
        throw new UnmarshallException("index " + idx + " " + e.getMessage(), e);
    } catch (final JSONException e) {
        throw new UnmarshallException("index " + idx + " " + e.getMessage(), e);
    }
    return abset;
}

From source file:com.miz.service.TraktMoviesSyncService.java

/**
 * Get movie collection from Trakt//w ww  .j av  a 2  s.  com
 */
private void downloadMovieCollection() {
    JSONArray jsonArray = Trakt.getMovieLibrary(this, Trakt.COLLECTION);
    if (jsonArray.length() > 0) {
        for (int i = 0; i < jsonArray.length(); i++) {
            try {
                mMovieCollection.add(String.valueOf(jsonArray.getJSONObject(i).get("tmdb_id")));
            } catch (Exception e) {
            }
        }
    }

    jsonArray = null;
}

From source file:com.miz.service.TraktMoviesSyncService.java

private void downloadWatchedMovies() {
    JSONArray jsonArray = Trakt.getMovieLibrary(this, Trakt.WATCHED);
    if (jsonArray.length() > 0) {
        for (int i = 0; i < jsonArray.length(); i++) {
            try {
                String tmdbId = String.valueOf(jsonArray.getJSONObject(i).get("tmdb_id"));
                mWatchedMovies.add(tmdbId);
                mMovieDatabase.updateMovieSingleItem(tmdbId, DbAdapterMovies.KEY_HAS_WATCHED, "1");
            } catch (Exception e) {
            }// w  w w .  ja va2  s.  co  m
        }
    }
}

From source file:com.miz.service.TraktMoviesSyncService.java

private void downloadMovieFavorites() {
    JSONArray jsonArray = Trakt.getMovieLibrary(this, Trakt.RATINGS);
    if (jsonArray.length() > 0) {
        for (int i = 0; i < jsonArray.length(); i++) {
            try {
                String tmdbId = String.valueOf(jsonArray.getJSONObject(i).get("tmdb_id"));
                mMovieFavorites.add(tmdbId);
                mMovieDatabase.updateMovieSingleItem(tmdbId, DbAdapterMovies.KEY_FAVOURITE, "1");
            } catch (Exception e) {
            }//from   www.  ja  v  a2  s.co  m
        }
    }
}

From source file:com.miz.service.TraktMoviesSyncService.java

private void downloadWatchlist() {
    JSONArray jsonArray = Trakt.getMovieLibrary(this, Trakt.WATCHLIST);
    if (jsonArray.length() > 0) {
        for (int i = 0; i < jsonArray.length(); i++) {
            try {
                String tmdbId = String.valueOf(jsonArray.getJSONObject(i).get("tmdb_id"));
                mWatchlist.add(tmdbId);/*from  w ww.j  av a  2s  .c o  m*/
                mMovieDatabase.updateMovieSingleItem(tmdbId, DbAdapterMovies.KEY_TO_WATCH, "1");
            } catch (Exception e) {
            }
        }
    }
}

From source file:de.andidog.phonegapplugins.ActionBarSherlockTabBarPlugin.java

@Override
public boolean execute(String action, final JSONArray args, CallbackContext callbackContext)
        throws JSONException {
    if (action.equals("setTabSelectedListener")) {
        this.callback = null;

        if (args.length() != 0)
            throw new AssertionError("setTabSelectedListener takes no arguments");

        this.callback = callbackContext;

        return true;
    } else if (action.equals("hide")) {
        final ActionBar actionBar = sherlock.getActionBar();
        cordova.getActivity().runOnUiThread(new Runnable() {
            @Override//from   w  w w  .j av a2s . c  o m
            public void run() {
                actionBar.hide();
            }
        });
        return true;
    } else if (action.equals("show")) {
        final ActionBar actionBar = sherlock.getActionBar();
        cordova.getActivity().runOnUiThread(new Runnable() {
            @Override
            public void run() {
                actionBar.show();
            }
        });
        return true;
    } else if (action.equals("selectItem")) {
        if (args.length() != 1)
            throw new AssertionError("selectItem takes tab tag as only argument");

        final ActionBar actionBar = sherlock.getActionBar();
        cordova.getActivity().runOnUiThread(new Runnable() {
            @Override
            public void run() {
                int count = actionBar.getTabCount();
                boolean found = false;

                try {
                    for (int i = 0; i < count; ++i) {
                        Tab t = actionBar.getTabAt(i);
                        if (t.getTag().equals(args.get(0))) {
                            actionBar.selectTab(t);
                            found = true;
                            break;
                        }
                    }

                    if (!found)
                        Log.e(TAG, "Tab '" + args.get(0) + "' not found");
                } catch (JSONException e) {
                    // Can't happen
                    Log.e(TAG, "", e);
                }
            }
        });

        return true;
    } else if (action.equals("_init")) {
        // This is the signal send from the JavaScript that forces construction of this plugin
        if (onInitListener != null) {
            onInitListener.onActionBarSherlockTabBarPluginInitialized();
            onInitListener = null;
        }

        return true;
    } else {
        Log.e(TAG, "Invalid call: " + action);
        return false;
    }
}

From source file:com.googlecode.goclipse.tooling.oracle.OraclePackageDescribeParser.java

protected ArrayList2<StructureElement> parseElements(JSONArray members, boolean parsingMethods)
        throws JSONException, CommonException {
    ArrayList2<StructureElement> elements = new ArrayList2<>();

    if (members != null) {
        for (int i = 0; i < members.length(); i++) {
            Object object = members.get(i);
            if (object instanceof JSONObject) {
                JSONObject jsonObject = (JSONObject) object;
                StructureElement element = parseStructureElement(jsonObject, parsingMethods);
                if (element == null) {
                    continue; // Can happen for external elements
                }//from  ww w  .  j a  v a2  s  . co m
                elements.add(element);
            } else {
                throw new CommonException("'members' element is not a JSONObject: " + object);
            }
        }
    }

    Collections.sort(elements, new Comparator<StructureElement>() {
        @Override
        public int compare(StructureElement o1, StructureElement o2) {
            SourceRange sr1 = o1.getSourceRange();
            SourceRange sr2 = o2.getSourceRange();
            int cmp = sr1.getOffset() - sr2.getOffset();
            if (cmp == 0) {
                return o1.getNameSourceRange().getOffset() - o2.getNameSourceRange().getOffset();
            }
            return cmp;
        }
    });

    return elements;
}

From source file:com.jjoseba.podemosquotes.model.JSONReader.java

public ArrayList<Quote> parse() {
    String contentsJson = readJSONFile();
    HashMap<String, QuoteAuthor> authors = new HashMap<>();
    ArrayList<Quote> quotes = new ArrayList<>();

    try {/*from  w w  w  .j  a  v a2s .  c  o m*/
        JSONObject json = new JSONObject(contentsJson);
        JSONArray authorsArray = json.getJSONArray("authors");
        for (int i = 0; i < authorsArray.length(); ++i) {
            JSONObject auth = (JSONObject) authorsArray.get(i);
            QuoteAuthor author = new QuoteAuthor();
            author.setName(auth.getString("name"));
            author.setImagePath(auth.getString("image"));
            author.setPosition(auth.getString("position"));

            authors.put(auth.getString("id"), author);
        }

        JSONArray quotesArray = json.getJSONArray("quotes");
        for (int i = 0; i < quotesArray.length(); ++i) {
            JSONObject q = (JSONObject) quotesArray.get(i);
            Quote quote = new Quote();
            quote.setQuote(q.getString("quote"));
            quote.setSoundPath(q.getString("sound"));
            quote.setAuthor(authors.get(q.getString("author")));
            quotes.add(quote);
        }

    } catch (JSONException e) {
        e.printStackTrace();
    }
    return quotes;
}

From source file:org.chromium.ChromeI18n.java

private static List<String> toStringList(JSONArray array) throws JSONException {
    if (array == null) {
        return null;
    }//from  w  w w .j  a v  a 2s  . c o m
    List<String> list = new ArrayList<String>();

    for (int i = 0, l = array.length(); i < l; i++) {
        list.add(array.get(i).toString());
    }

    return list;
}

From source file:com.adintellig.UDFJson.java

private Object extract_json_withindex(Object json, ArrayList<String> indexList) throws JSONException {

    jsonList.clear();//ww w  . j  av  a  2s .c o  m
    jsonList.add(json);
    Iterator<String> itr = indexList.iterator();
    while (itr.hasNext()) {
        String index = itr.next();
        ArrayList<Object> tmp_jsonList = new ArrayList<Object>();
        if (index.equalsIgnoreCase("*")) {
            for (int i = 0; i < (jsonList).size(); i++) {
                try {
                    JSONArray array = (JSONArray) (jsonList).get(i);
                    for (int j = 0; j < array.length(); j++) {
                        tmp_jsonList.add(array.get(j));
                    }
                } catch (Exception e) {
                    continue;
                }
            }
            jsonList = tmp_jsonList;
        } else {
            for (int i = 0; i < (jsonList).size(); i++) {
                try {
                    tmp_jsonList.add(((JSONArray) (jsonList).get(i)).get(Integer.parseInt(index)));
                } catch (ClassCastException e) {
                    continue;
                } catch (JSONException e) {
                    return null;
                }
                jsonList = tmp_jsonList;
            }
        }
    }
    return (jsonList.size() > 1) ? new JSONArray(jsonList) : jsonList.get(0);
}