List of usage examples for org.json JSONArray put
public JSONArray put(Object value)
From source file:org.official.json.CDL.java
/** * Produce a JSONArray of JSONObjects from a comma delimited text string * using a supplied JSONArray as the source of element names. * @param names A JSONArray of strings.//w ww.j av a 2s. c o m * @param x A JSONTokener of the source text. * @return A JSONArray of JSONObjects. * @throws JSONException */ public static JSONArray toJSONArray(JSONArray names, JSONTokener x) throws JSONException { if (names == null || names.length() == 0) { return null; } JSONArray ja = new JSONArray(); for (;;) { JSONObject jo = rowToJSONObject(names, x); if (jo == null) { break; } ja.put(jo); } if (ja.length() == 0) { return null; } return ja; }
From source file:com.mmclass.libsiren.Util.java
public static void putFloatArray(Editor editor, String key, float[] array) { try {/*from w w w. ja v a 2 s.co m*/ JSONArray json = new JSONArray(); for (float f : array) json.put(f); editor.putString("equalizer_values", json.toString()); } catch (JSONException e) { e.printStackTrace(); } }
From source file:ru.jkff.antro.ProfileListener.java
public void dumpReport(Report report, String reportFilename) { try {/*www.j a v a2s . co m*/ JSONArray res = new JSONArray(); for (String file : report.getUsedBuildFiles()) { AnnotatedFile f = report.getAnnotatedFile(file); JSONArray annotatedFile = new JSONArray(); for (int i = 0; i < f.getLineCount(); ++i) { String line = f.getLine(i); Stat stat = f.getStat(i); annotatedFile.put(annotateLine(line, i, stat)); } JSONObject fileObj = new JSONObject(); fileObj.put("name", file); fileObj.put("stat", annotatedFile); res.put(fileObj); } if (!dontWriteFile) { FileWriter w = new FileWriter(reportFilename); JSONArray data = new JSONArray(); data.put(res); data.put(toJSON(report.getTrace())); w.write(data.toString(2)); w.close(); } } catch (JSONException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } }
From source file:ru.jkff.antro.ProfileListener.java
private JSONArray toJSON(List<Call> stack) throws JSONException { JSONArray res = new JSONArray(); for (Call element : stack) { res.put(toJSON(element)); }/* ww w . ja v a 2 s . c om*/ return res; }
From source file:ru.jkff.antro.ProfileListener.java
private JSONObject toJSON(Trace trace) throws JSONException { JSONObject res = new JSONObject(); res.put("call", toJSON(trace.getCall())); if (trace.getTotalTime() > 0) res.put("total", trace.getTotalTime()); if (trace.getOwnTime() > 0) res.put("own", trace.getOwnTime()); if (trace.getPercentOfParent() > 0) res.put("percentOfParent", trace.getPercentOfParent()); JSONArray children = new JSONArray(); for (Trace child : trace.getChildren()) { children.put(toJSON(child)); }/* w w w . j a v a2 s .co m*/ if (children.length() > 0) res.put("children", children); return res; }
From source file:com.nextgis.firereporter.ScanexSubscriptionItem.java
public JSONObject getAsJSON() { JSONObject object = new JSONObject(); try {/*w w w. j a va2 s . com*/ object.put("id", nID); object.put("title", sTitle); object.put("layer_name", sLayerName); object.put("wkt", sWKT); object.put("sms_enabled", bSMSEnable); JSONArray oJSONItems = new JSONArray(); object.put("items", oJSONItems); for (ScanexNotificationItem Item : mmoItems.values()) { oJSONItems.put(Item.getAsJSON()); } } catch (JSONException e) { SendError(e.getLocalizedMessage()); } return object; }
From source file:com.hichinaschool.flashcards.libanki.sync.Syncer.java
public JSONObject sanityCheck() { JSONObject result = new JSONObject(); try {/* w w w. j ava 2 s . c o m*/ if (mCol.getDb().queryScalar("SELECT count() FROM cards WHERE nid NOT IN (SELECT id FROM notes)", false) != 0) { Log.e(AnkiDroidApp.TAG, "Sync - SanityCheck: there are cards without mother notes"); result.put("client", "missing notes"); return result; } if (mCol.getDb().queryScalar( "SELECT count() FROM notes WHERE id NOT IN (SELECT DISTINCT nid FROM cards)", false) != 0) { Log.e(AnkiDroidApp.TAG, "Sync - SanityCheck: there are notes without cards"); result.put("client", "missing cards"); return result; } if (mCol.getDb().queryScalar("SELECT count() FROM cards WHERE usn = -1", false) != 0) { Log.e(AnkiDroidApp.TAG, "Sync - SanityCheck: there are unsynced cards"); result.put("client", "cards had usn = -1"); return result; } if (mCol.getDb().queryScalar("SELECT count() FROM notes WHERE usn = -1", false) != 0) { Log.e(AnkiDroidApp.TAG, "Sync - SanityCheck: there are unsynced notes"); result.put("client", "notes had usn = -1"); return result; } if (mCol.getDb().queryScalar("SELECT count() FROM revlog WHERE usn = -1", false) != 0) { Log.e(AnkiDroidApp.TAG, "Sync - SanityCheck: there are unsynced revlogs"); result.put("client", "revlog had usn = -1"); return result; } if (mCol.getDb().queryScalar("SELECT count() FROM graves WHERE usn = -1", false) != 0) { Log.e(AnkiDroidApp.TAG, "Sync - SanityCheck: there are unsynced graves"); result.put("client", "graves had usn = -1"); return result; } for (JSONObject g : mCol.getDecks().all()) { if (g.getInt("usn") == -1) { Log.e(AnkiDroidApp.TAG, "Sync - SanityCheck: unsynced deck: " + g.getString("name")); result.put("client", "deck had usn = -1"); return result; } } for (Integer usn : mCol.getTags().allItems().values()) { if (usn == -1) { Log.e(AnkiDroidApp.TAG, "Sync - SanityCheck: there are unsynced tags"); result.put("client", "tag had usn = -1"); return result; } } boolean found = false; for (JSONObject m : mCol.getModels().all()) { if (mCol.getServer()) { // the web upgrade was mistakenly setting usn if (m.getInt("usn") < 0) { m.put("usn", 0); found = true; } } else { if (m.getInt("usn") == -1) { Log.e(AnkiDroidApp.TAG, "Sync - SanityCheck: unsynced model: " + m.getString("name")); result.put("client", "model had usn = -1"); return result; } } } if (found) { mCol.getModels().save(); } mCol.getSched().reset(); // check for missing parent decks mCol.getSched().deckDueList(); // return summary of deck JSONArray ja = new JSONArray(); JSONArray sa = new JSONArray(); for (int c : mCol.getSched().counts()) { sa.put(c); } ja.put(sa); ja.put(mCol.getDb().queryScalar("SELECT count() FROM cards")); ja.put(mCol.getDb().queryScalar("SELECT count() FROM notes")); ja.put(mCol.getDb().queryScalar("SELECT count() FROM revlog")); ja.put(mCol.getDb().queryScalar("SELECT count() FROM graves")); ja.put(mCol.getModels().all().size()); ja.put(mCol.getDecks().all().size()); ja.put(mCol.getDecks().allConf().size()); result.put("client", ja); return result; } catch (JSONException e) { Log.e(AnkiDroidApp.TAG, "Syncer.sanityCheck(): ", e); throw new RuntimeException(e); } }
From source file:com.hichinaschool.flashcards.libanki.sync.Syncer.java
public JSONObject chunk() { JSONObject buf = new JSONObject(); try {//w w w. j a v a 2s. co m buf.put("done", false); int lim = 2500; List<Integer> colTypes = null; while (!mTablesLeft.isEmpty() && lim > 0) { String curTable = mTablesLeft.getFirst(); if (mCursor == null) { mCursor = cursorForTable(curTable); colTypes = columnTypesForQuery(curTable); } JSONArray rows = new JSONArray(); int count = mCursor.getColumnCount(); while (mCursor.moveToNext() && mCursor.getPosition() <= lim) { JSONArray r = new JSONArray(); for (int i = 0; i < count; i++) { switch (colTypes.get(i).intValue()) { case TYPE_STRING: r.put(mCursor.getString(i)); break; case TYPE_FLOAT: r.put(mCursor.getDouble(i)); break; case TYPE_INTEGER: r.put(mCursor.getLong(i)); break; } } rows.put(r); } int fetched = rows.length(); if (fetched != lim) { // table is empty mTablesLeft.removeFirst(); mCursor.close(); mCursor = null; // if we're the client, mark the objects as having been sent if (!mCol.getServer()) { mCol.getDb().execute("UPDATE " + curTable + " SET usn=" + mMaxUsn + " WHERE usn=-1"); } } buf.put(curTable, rows); lim -= fetched; } if (mTablesLeft.isEmpty()) { buf.put("done", true); } } catch (JSONException e) { throw new RuntimeException(e); } return buf; }
From source file:com.hichinaschool.flashcards.libanki.sync.Syncer.java
/** * Deletions ******************************************************************** *//* w w w . j av a 2s. com*/ private JSONObject removed() { JSONArray cards = new JSONArray(); JSONArray notes = new JSONArray(); JSONArray decks = new JSONArray(); Cursor cur = null; try { cur = mCol.getDb().getDatabase().rawQuery( "SELECT oid, type FROM graves WHERE usn" + (mCol.getServer() ? (" >= " + mMinUsn) : (" = -1")), null); while (cur.moveToNext()) { int type = cur.getInt(1); switch (type) { case Sched.REM_CARD: cards.put(cur.getLong(0)); break; case Sched.REM_NOTE: notes.put(cur.getLong(0)); break; case Sched.REM_DECK: decks.put(cur.getLong(0)); break; } } } finally { if (cur != null && !cur.isClosed()) { cur.close(); } } if (!mCol.getServer()) { mCol.getDb().execute("UPDATE graves SET usn=" + mMaxUsn + " WHERE usn=-1"); } JSONObject o = new JSONObject(); try { o.put("cards", cards); o.put("notes", notes); o.put("decks", decks); } catch (JSONException e) { throw new RuntimeException(e); } return o; }
From source file:com.hichinaschool.flashcards.libanki.sync.Syncer.java
/** * Models ******************************************************************** *///from w w w. j ava 2 s . c o m private JSONArray getModels() { JSONArray result = new JSONArray(); try { if (mCol.getServer()) { for (JSONObject m : mCol.getModels().all()) { if (m.getInt("usn") >= mMinUsn) { result.put(m); } } } else { for (JSONObject m : mCol.getModels().all()) { if (m.getInt("usn") == -1) { m.put("usn", mMaxUsn); result.put(m); } } mCol.getModels().save(); } } catch (JSONException e) { throw new RuntimeException(e); } return result; }