List of usage examples for org.json JSONArray put
public JSONArray put(Object value)
From source file:org.stockchart.StockChartView.java
public String save() throws JSONException { JSONObject view = new JSONObject(); JSONArray areas = new JSONArray(); for (Area a : fAreas) { areas.put(a.toJSONObject()); }// w w w . j av a2s. co m view.put("areas", areas); JSONArray ranges = new JSONArray(); Iterator<Entry<Axis.Side, AxisRange>> iterator = this.fGlobalRanges.entrySet().iterator(); while (iterator.hasNext()) { JSONObject range = new JSONObject(); Entry<Axis.Side, AxisRange> i = iterator.next(); range.put("side", i.getKey()); range.put("axisRange", i.getValue().toJSONObject()); ranges.put(range); } view.put("globalAxisRanges", ranges); view.put("indicators", fIndicatorManager.toJSONArray()); view.put("clearColor", fClearColor); view.put("crosshair", fCrosshair.toJSONObject()); return view.toString(); }
From source file:org.eclipse.orion.server.tests.servlets.git.GitResetTest.java
static WebRequest getPostGitIndexRequest(String location, String[] paths, String commit, String resetType) throws JSONException, UnsupportedEncodingException { String requestURI = toAbsoluteURI(location); JSONObject body = new JSONObject(); if (resetType != null) body.put(GitConstants.KEY_RESET_TYPE, resetType); if (paths != null) { // assertNull("Cannot mix paths and commit", commit); JSONArray jsonPaths = new JSONArray(); for (String path : paths) jsonPaths.put(path); body.put(ProtocolConstants.KEY_PATH, jsonPaths); }//from w w w .j a v a 2 s .c o m if (commit != null) body.put(GitConstants.KEY_TAG_COMMIT, commit); WebRequest request = new PostMethodWebRequest(requestURI, IOUtilities.toInputStream(body.toString()), "UTF-8"); request.setHeaderField(ProtocolConstants.HEADER_ORION_VERSION, "1"); setAuthentication(request); return request; }
From source file:com.tobolkac.triviaapp.ScreenSlideActivity.java
public void evalGame(int numCorrect, long time) { cdt.cancel();/*w w w . j a v a2 s .c om*/ if (isChallenger == true) { currentUser = ParseUser.getCurrentUser(); //create game object of current game and push to database gameScore = new ParseObject("Games"); gameScore.put("challenger", currentUser.getUsername()); gameScore.put("opponent", challengedUser); gameScore.put("challengerCorrect", numCorrect); gameScore.put("winner", 0); gameScore.put("challengerTime", time / 1000); gameScore.put("category", cat); gameScore.put("numberOfQuestions", 10); JSONArray qNUms = new JSONArray(); for (int a : questionNums) { qNUms.put(a); } gameScore.put("questionsArray", qNUms); JSONArray cArray = new JSONArray(); for (int a : correctArray) { cArray.put(a); } gameScore.put("chalCorrectArray", cArray); gameScore.saveInBackground(new SaveCallback() { public void done(ParseException e) { if (e == null) { String gameId = gameScore.getObjectId(); JSONObject data = new JSONObject(); try { data.put("game", "com.tobolkac.FETCH_GAME"); data.put("challenger", currentUser.getUsername()); data.put("opponent", challengedUser); data.put("gameId", gameId); } catch (JSONException e1) { e1.printStackTrace(); } ParsePush androidPush = new ParsePush(); androidPush.setMessage(cat + " Trivia Challenge from " + currentUser.getUsername()); androidPush.setData(data); androidPush.setChannel(challengedUser); androidPush.sendInBackground(); Intent intent = new Intent(ScreenSlideActivity.this, MainActivity.class); intent.putExtra("gameId", gameId); startActivity(intent); finish(); } } }); } else { ParseQuery<ParseObject> query = ParseQuery.getQuery("Games"); ParseQuery<ParseObject> queryChalllangerRecord = ParseQuery.getQuery("Record"); ParseQuery<ParseObject> queryOpponentRecord = ParseQuery.getQuery("Record"); try { ParseObject gameObj = query.get(gameId); // int opponentTime = (int)(Math.random() * 100); int challengerTime = gameObj.getNumber("challengerTime").intValue(); int challengerCorrect = gameObj.getNumber("challengerCorrect").intValue(); gameObj.put("opponentTime", time / 1000); gameObj.put("opponentCorrect", numCorrect); queryChalllangerRecord.whereEqualTo("user", gameObj.getString("challenger")); ParseObject challangerRecord = queryChalllangerRecord.getFirst(); queryOpponentRecord.whereEqualTo("user", gameObj.getString("opponent")); ParseObject opponentRecord = queryOpponentRecord.getFirst(); String res = "You won!"; //1 = challenger 2 = opponennt 0 = in progress if (numCorrect > challengerCorrect) { //opponent wins gameObj.put("winner", 2); int oWins = opponentRecord.getInt("wins"); opponentRecord.put("wins", (oWins + 1)); int cLoses = challangerRecord.getInt("loses"); opponentRecord.put("wins", (cLoses + 1)); } else if (numCorrect < challengerCorrect) { //challenger wins gameObj.put("winner", 1); res = "You Lost!"; int oLoses = opponentRecord.getInt("loses"); opponentRecord.put("loses", (oLoses + 1)); int cWins = challangerRecord.getInt("wins"); challangerRecord.put("wins", (cWins + 1)); } else { if (time < challengerTime) { //opponent wins gameObj.put("winner", 2); int oWins = opponentRecord.getInt("wins"); opponentRecord.put("wins", (oWins + 1)); int cLoses = challangerRecord.getInt("loses"); challangerRecord.put("loses", (cLoses + 1)); } else { //challenger wins gameObj.put("winner", 1); res = "You Lost!"; int oLoses = opponentRecord.getInt("loses"); opponentRecord.put("loses", (oLoses + 1)); int cWins = challangerRecord.getInt("wins"); challangerRecord.put("wins", (cWins + 1)); } } JSONArray cArray = new JSONArray(); for (int a : correctArray) { cArray.put(a); } gameObj.put("oppCorrectArray", cArray); Intent intent = new Intent(ScreenSlideActivity.this, MainActivity.class); intent.putExtra("gameId", gameId); startActivity(intent); Toast.makeText(this, res, Toast.LENGTH_SHORT).show(); gameObj.saveInBackground(); opponentRecord.saveInBackground(); challangerRecord.saveInBackground(); finish(); } catch (ParseException e1) { e1.printStackTrace(); } } }
From source file:com.codebutler.farebot.keys.ClassicCardKeys.java
public JSONObject toJSON() { try {//from w w w . j a va2s . c om JSONArray keysJson = new JSONArray(); for (ClassicSectorKey key : mSectorKeys) { keysJson.put(key.toJSON()); } JSONObject json = new JSONObject(); json.put(KEYS, keysJson); return json; } catch (JSONException ex) { throw new RuntimeException(ex); } }
From source file:nz.co.wholemeal.christchurchmetro.FavouritesActivity.java
public static void saveFavourites(SharedPreferences favourites) { SharedPreferences.Editor editor = favourites.edit(); JSONArray stopArray = new JSONArray(); Iterator iterator = stops.iterator(); while (iterator.hasNext()) { Stop stop = (Stop) iterator.next(); stopArray.put(stop.platformTag); }/* w ww. ja v a 2 s. c om*/ editor.putString("favouriteStops", stopArray.toString()); Log.d(TAG, "Saving " + stops.size() + " favourites"); Log.d(TAG, "json = " + stopArray.toString()); editor.commit(); }
From source file:com.google.identitytoolkit.RpcHelper.java
private static JSONArray toJsonArray(List<GitkitUser> accounts) throws JSONException { JSONArray infos = new JSONArray(); for (GitkitUser account : accounts) { JSONObject user = new JSONObject(); user.put("email", account.getEmail()); user.put("localId", account.getLocalId()); if (account.getHash() != null) { user.put("passwordHash", BaseEncoding.base64Url().encode(account.getHash())); }//from w w w . j a va2 s. co m if (account.getSalt() != null) { user.put("salt", BaseEncoding.base64Url().encode(account.getSalt())); } if (account.getProviders() != null) { JSONArray providers = new JSONArray(); for (GitkitUser.ProviderInfo idpInfo : account.getProviders()) { providers.put(new JSONObject().put("federatedId", idpInfo.getFederatedId()).put("providerId", idpInfo.getProviderId())); } user.put("providerUserInfo", providers); } infos.put(user); } return infos; }
From source file:com.ibm.mobilefirst.mobileedge.interpretation.Classification.java
private JSONArray createArrayJSONFromData(float x, float y, float z) { JSONArray array = new JSONArray(); try {//from www . j ava 2s .co m array.put(x); array.put(y); array.put(z); } catch (JSONException e) { e.printStackTrace(); } return array; }
From source file:com.ibm.mobilefirst.mobileedge.interpretation.Classification.java
/** * Get the next data for sending, and remove it from the list * @param data list of the next data//from w w w . j a v a2 s. co m * @return json result */ private JSONArray getNextDataAsJSONArray(List<JSONArray> data) { JSONArray result = new JSONArray(); for (int i = 0; i < DATA_SIZE; i++) { JSONArray array = data.get(0); result.put(array); data.remove(0); } return result; }
From source file:com.jeffstephens.castagainsthumanity.GameMessageStream.java
public final void submitResponse(int[] cardIDs) { // build responses JSONArray JSONArray responses = new JSONArray(); for (int i = 0; i < cardIDs.length; ++i) { responses.put(cardIDs[i]); }/* w w w .j a v a 2s . co m*/ try { // submit JSONObject payload = new JSONObject(); payload.put(KEY_TYPE, KEY_SUBMIT_CARD); payload.put(KEY_CARD_ID_ARRAY, responses); sendMessage(payload); } catch (JSONException e) { Log.e(TAG, "Cannot create object to submit response", e); } catch (IOException e) { Log.e(TAG, "Unable to send a(n) " + KEY_SUBMIT_CARD + " message", e); } catch (IllegalStateException e) { Log.e(TAG, "Message Stream is not attached", e); } }
From source file:com.jeffstephens.castagainsthumanity.GameMessageStream.java
public final void readSubmission(int[] cardsRead) { try {/* ww w. j ava 2 s.co m*/ JSONArray cardsJSON = new JSONArray(); for (int i = 0; i < cardsRead.length; ++i) { cardsJSON.put(cardsRead[i]); } JSONObject payload = new JSONObject(); payload.put(KEY_TYPE, KEY_HAVE_READ_SUBMISSION); payload.put(KEY_SUBMISSION_THAT_WAS_READ, cardsJSON); sendMessage(payload); } catch (JSONException e) { Log.e(TAG, "Cannot create object to send which submissions the judge has read", e); } catch (IOException e) { Log.e(TAG, "Unable to send a(n) " + KEY_HAVE_READ_SUBMISSION + " message", e); } catch (IllegalStateException e) { Log.e(TAG, "Message Stream is not attached", e); } }