List of usage examples for org.json JSONObject getInt
public int getInt(String key) throws JSONException
From source file:com.mygdx.game.multiplayer.World.java
private void generateLevel() { try {/*from w ww.j ava 2 s . c om*/ JSONArray array = new JSONArray(Assets.platformDataString); int i = 0; float y = Platform.PLATFORM_HEIGHT / 2; float maxJumpHeight = Bob.BOB_JUMP_VELOCITY * Bob.BOB_JUMP_VELOCITY / (2 * -gravity.y); while (y < WORLD_HEIGHT - WORLD_WIDTH / 2) { int type = rand.nextFloat() > 0.8f ? Platform.PLATFORM_TYPE_MOVING : Platform.PLATFORM_TYPE_STATIC; float x = rand.nextFloat() * (WORLD_WIDTH - Platform.PLATFORM_WIDTH) + Platform.PLATFORM_WIDTH / 2; if (i >= array.length()) { i = array.length() - 1; } JSONObject data = array.getJSONObject(i); i++; Platform platform = new Platform(data.getInt("type"), (float) data.getDouble("x"), (float) data.getDouble("y")); platforms.add(platform); if (rand.nextFloat() > 0.9f && type != Platform.PLATFORM_TYPE_MOVING) { Spring spring = new Spring(platform.position.x, platform.position.y + Platform.PLATFORM_HEIGHT / 2 + Spring.SPRING_HEIGHT / 2); springs.add(spring); } if (y > WORLD_HEIGHT / 3 && rand.nextFloat() > 0.8f) { Squirrel squirrel = new Squirrel(platform.position.x + rand.nextFloat(), platform.position.y + Squirrel.SQUIRREL_HEIGHT + rand.nextFloat() * 2); squirrels.add(squirrel); } if (rand.nextFloat() > 0.6f) { Coin coin = new Coin(platform.position.x + rand.nextFloat(), platform.position.y + Coin.COIN_HEIGHT + rand.nextFloat() * 3); coins.add(coin); } y += (maxJumpHeight - 0.5f); y -= rand.nextFloat() * (maxJumpHeight / 3); } castle = new Castle(WORLD_WIDTH / 2, y); } catch (Exception e) { e.printStackTrace(); } }
From source file:net.sensemaker.snappy.table.test.TableRequestDecoderTest.java
public void testCurrentPageChange() throws Exception { SnappyTable table = new SnappyTable(); table.setVar("row"); List value = new ArrayList(); for (int i = 0; i < 100; i++) { value.add("" + i); }/*from w w w .j a v a2 s . co m*/ table.setValue(value); TableRequestDecoder tableRequestDecoder = new TableRequestDecoder(); ColumnValueEncoder columnValueEncoder = new ColumnValueEncoder(); TableEncoder tableEncoder = new TableEncoder(table, columnValueEncoder); String event = ""; String sort = ""; JSONObject json = new JSONObject(); json.put("currentPage", 1); json.put("maxPages", 10); json.put("selectedIndex", "-1"); MockResponseWriter writerA = new MockResponseWriter(); facesContext.setResponseWriter(writerA); facesContext.getExternalContext().getRequestParameterMap() .put(table.genStatusId(table.getClientId(facesContext)), json.toString()); facesContext.getExternalContext().getRequestMap().put("status", json.toString()); tableRequestDecoder.decode(json.toString(), event, sort, facesContext, table); assertEquals(table.getCurrentPage(), json.getInt("currentPage")); tableEncoder.encode(facesContext); assertTrue("Empty after first pass", !writerA.isEmpty()); json.put("currentPage", 2); MockResponseWriter writerB = new MockResponseWriter(); facesContext.setResponseWriter(writerB); tableRequestDecoder.decode(json.toString(), event, sort, facesContext, table); assertEquals(table.getCurrentPage(), json.getInt("currentPage")); tableEncoder.encode(facesContext); assertTrue("Empty after second pass", !writerB.isEmpty()); assertNotNull(writerA.toString()); assertNotNull(writerB.toString()); assertTrue("Writers are not Equal", writerA.equals(writerB)); }
From source file:com.chaosinmotion.securechat.server.commands.DropMessages.java
public static void processRequest(UserInfo userinfo, JSONObject requestParams) throws ClassNotFoundException, SQLException, IOException { ArrayList<Message> messages = new ArrayList<Message>(); JSONArray a = requestParams.getJSONArray("messages"); int i, len = a.length(); for (i = 0; i < len; ++i) { JSONObject item = a.getJSONObject(i); Message msg = new Message(); msg.message = item.getInt("messageid"); msg.checksum = item.getString("checksum"); messages.add(msg);/*from w ww .jav a2s .co m*/ } /* * Iterate through the messages, deleting each. We only delete a * message if message belongs to the user and the checksum matches. * This assumes it's our message and it was read with someone who * can read the message. * * (Thus, the weird query) */ Connection c = null; PreparedStatement ps = null; ResultSet rs = null; try { int count = 0; c = Database.get(); ps = c.prepareStatement("DELETE FROM Messages " + "WHERE messageid IN " + " (SELECT Messages.messageid " + " FROM Messages, Devices " + " WHERE Messages.messageid = ? " + " AND Messages.checksum = ? " + " AND Devices.deviceid = Messages.deviceid " + " AND Devices.userid = ?)"); for (Message msg : messages) { /* * Get the device ID for this device. Verify it belongs to the * user specified */ ps.setInt(1, msg.message); ps.setString(2, msg.checksum); ps.setInt(3, userinfo.getUserID()); ps.addBatch(); ++count; if (count > 10240) { ps.executeBatch(); } } if (count > 0) { ps.executeBatch(); } } catch (BatchUpdateException batch) { throw batch.getNextException(); } finally { if (rs != null) rs.close(); if (ps != null) ps.close(); if (c != null) c.close(); } }
From source file:model.Annonce.java
@Override public void update(final JSONObject item) throws JSONException { setContent(item.getString("content")); setRank(item.getInt("rank")); setIsVisible(item.getBoolean("visibility")); setTitle(item.getString("title")); setDate(new DateTime(item.getString("date"))); }
From source file:com.nginious.http.serialize.JsonDeserializer.java
/** * Deserializes property with the specified name from the specified json object into a integer. * /*from w w w .j ava 2 s .co m*/ * @param object the json object to deserialize property from * @param name name of the property * @return the deserialized integer or <code>0</code> if property doesn't exist * @throws SerializerException uf unable to deserialize value */ protected int deserializeInt(JSONObject object, String name) throws SerializerException { try { if (object.has(name)) { return object.getInt(name); } return 0; } catch (JSONException e) { throw new SerializerException("Can't deserialize int property " + name, e); } }
From source file:com.nginious.http.serialize.JsonDeserializer.java
/** * Deserializes property with the specified name in the specified json object into a short. * /*from w ww .j a va2 s .c om*/ * @param object the json object * @param name the property name * @return the deserialized value or <code>0</code> if property doesn't exist * @throws SerializerException if unable to deserialize value */ protected short deserializeShort(JSONObject object, String name) throws SerializerException { try { if (object.has(name)) { return (short) object.getInt(name); } return 0; } catch (JSONException e) { throw new SerializerException("Can't deserialize short property " + name, e); } }
From source file:com.cpyf.twelve.spies.qr.code.book.SearchBookContentsActivity.java
private void handleSearchResults(JSONObject json) { try {//from ww w.j a v a 2s . com int count = json.getInt("number_of_results"); headerView.setText("Found " + (count == 1 ? "1 result" : count + " results")); if (count > 0) { JSONArray results = json.getJSONArray("search_results"); SearchBookContentsResult.setQuery(queryTextView.getText().toString()); List<SearchBookContentsResult> items = new ArrayList<SearchBookContentsResult>(count); for (int x = 0; x < count; x++) { items.add(parseResult(results.getJSONObject(x))); } resultListView.setOnItemClickListener(new BrowseBookListener(this, items)); resultListView.setAdapter(new SearchBookContentsAdapter(this, items)); } else { String searchable = json.optString("searchable"); if ("false".equals(searchable)) { headerView.setText(R.string.msg_sbc_book_not_searchable); } resultListView.setAdapter(null); } } catch (JSONException e) { Log.w(TAG, "Bad JSON from book search", e); resultListView.setAdapter(null); headerView.setText(R.string.msg_sbc_failed); } }
From source file:com.ikota.flickrclient.data.model.FlickerPhotoInfo.java
private void init(String json) throws JSONException { Gson gson = new Gson(); JSONObject root = new JSONObject(json); JSONObject photo_json = root.getJSONObject("photo"); this.photo = gson.fromJson(photo_json.toString(), Photo.class); this.owner = gson.fromJson(photo_json.getJSONObject("owner").toString(), Owner.class); this.title = photo_json.getJSONObject("title").getString("_content"); this.description = photo_json.getJSONObject("description").getString("_content"); this.dates = photo_json.getJSONObject("dates").getString("taken"); this.views = photo_json.getInt("views"); this.comments = photo_json.getJSONObject("comments").getInt("_content"); JSONArray tag_array = photo_json.getJSONObject("tags").getJSONArray("tag"); Type listType = new TypeToken<List<Tag>>() { }.getType();//from w ww .j a v a 2 s. co m tags = gson.fromJson(tag_array.toString(), listType); }
From source file:com.repkap11.repcast.VideoProvider.java
public static List<MediaInfo> buildMedia(String url) throws JSONException { if (null != mediaList) { return mediaList; }// w ww. ja va 2 s . c o m Map<String, String> urlPrefixMap = new HashMap<>(); mediaList = new ArrayList<>(); JSONObject jsonObj = new VideoProvider().parseUrl(url); JSONArray categories = jsonObj.getJSONArray(TAG_CATEGORIES); if (null != categories) { for (int i = 0; i < categories.length(); i++) { JSONObject category = categories.getJSONObject(i); urlPrefixMap.put(TAG_HLS, category.getString(TAG_HLS)); urlPrefixMap.put(TAG_DASH, category.getString(TAG_DASH)); urlPrefixMap.put(TAG_MP4, category.getString(TAG_MP4)); urlPrefixMap.put(TAG_IMAGES, category.getString(TAG_IMAGES)); urlPrefixMap.put(TAG_TRACKS, category.getString(TAG_TRACKS)); category.getString(TAG_NAME); JSONArray videos = category.getJSONArray(TAG_VIDEOS); if (null != videos) { for (int j = 0; j < videos.length(); j++) { String videoUrl = null; String mimeType = null; JSONObject video = videos.getJSONObject(j); String subTitle = video.getString(TAG_SUBTITLE); JSONArray videoSpecs = video.getJSONArray(TAG_SOURCES); if (null == videoSpecs || videoSpecs.length() == 0) { continue; } for (int k = 0; k < videoSpecs.length(); k++) { JSONObject videoSpec = videoSpecs.getJSONObject(k); if (TARGET_FORMAT.equals(videoSpec.getString(TAG_VIDEO_TYPE))) { videoUrl = urlPrefixMap.get(TARGET_FORMAT) + videoSpec.getString(TAG_VIDEO_URL); mimeType = videoSpec.getString(TAG_VIDEO_MIME); } } if (videoUrl == null) { continue; } String imageUrl = urlPrefixMap.get(TAG_IMAGES) + video.getString(TAG_THUMB); String bigImageUrl = urlPrefixMap.get(TAG_IMAGES) + video.getString(TAG_IMG_780_1200); String title = video.getString(TAG_TITLE); String studio = video.getString(TAG_STUDIO); int duration = video.getInt(TAG_DURATION); List<MediaTrack> tracks = null; if (video.has(TAG_TRACKS)) { JSONArray tracksArray = video.getJSONArray(TAG_TRACKS); if (tracksArray != null) { tracks = new ArrayList<>(); for (int k = 0; k < tracksArray.length(); k++) { JSONObject track = tracksArray.getJSONObject(k); tracks.add(buildTrack(track.getLong(TAG_TRACK_ID), track.getString(TAG_TRACK_TYPE), track.getString(TAG_TRACK_SUBTYPE), urlPrefixMap.get(TAG_TRACKS) + track.getString(TAG_TRACK_CONTENT_ID), track.getString(TAG_TRACK_NAME), track.getString(TAG_TRACK_LANGUAGE))); } } } mediaList.add(buildMediaInfo(title, studio, subTitle, duration, videoUrl, mimeType, imageUrl, bigImageUrl, tracks)); } } } } return mediaList; }
From source file:org.kei.android.phone.mangastore.AppFragment.java
private void convertFromJSON(JSONArray response) { boolean aborted = true; boolean finished = true; boolean inprogress = true; final int position = CFG.get(Config.KEY_PAGER_CURRENT_TAB, 0); final MangaType selectedType = MangaType.getType(position); if (position == MangaStorePagerActivity.POSITION_MANGAS) { aborted = CFG.get(Config.KEY_SHOW_MANGAS_ABORTED, true); finished = CFG.get(Config.KEY_SHOW_MANGAS_FINISHED, true); inprogress = CFG.get(Config.KEY_SHOW_MANGAS_INPROGRESS, true); } else if (position == MangaStorePagerActivity.POSITION_ANIMES) { aborted = CFG.get(Config.KEY_SHOW_ANIMES_ABORTED, true); finished = CFG.get(Config.KEY_SHOW_ANIMES_FINISHED, true); inprogress = CFG.get(Config.KEY_SHOW_ANIMES_INPROGRESS, true); } else if (position == MangaStorePagerActivity.POSITION_MOVIES) { aborted = CFG.get(Config.KEY_SHOW_MOVIES_ABORTED, true); finished = CFG.get(Config.KEY_SHOW_MOVIES_FINISHED, true); inprogress = CFG.get(Config.KEY_SHOW_MOVIES_INPROGRESS, true); } else if (position == MangaStorePagerActivity.POSITION_OAV) { aborted = CFG.get(Config.KEY_SHOW_OAV_ABORTED, true); finished = CFG.get(Config.KEY_SHOW_OAV_FINISHED, true); inprogress = CFG.get(Config.KEY_SHOW_OAV_INPROGRESS, true); }/*from ww w .j a v a 2 s.c om*/ if (response.length() > 0) { List<MangaModel> mangaList = new ArrayList<MangaModel>(); // looping through json and adding to movies list for (int i = 0; i < response.length(); i++) { try { JSONObject mObj = response.getJSONObject(i); MangaType type = MangaType.getType(mObj.getInt("type")); StatusType status = StatusType.getType(mObj.getInt("status")); if (type != selectedType) continue; if (!aborted && status == StatusType.ABORTED) continue; if (!finished && status == StatusType.FINISHED) continue; if (!inprogress && status == StatusType.INPROGRESS) continue; int id = mObj.getInt("id"); int year = mObj.getInt("year"); String label = mObj.getString("name"); int volume = mObj.getInt("number"); MangaModel m = new MangaModel(id, type, label, year, volume, status); mangaList.add(0, m); } catch (JSONException e) { Tools.toast(getActivity(), R.drawable.ic_launcher, "JSON Parsing error: " + e.getMessage()); e.printStackTrace(); mangaList.clear(); break; } } if (!mangaList.isEmpty()) { adapter.clear(); adapter.addAll(mangaList); adapter.sort(); if (isAdded()) indexer.forceReload(); else refresh(); } } // stopping swipe refresh swipeRefreshLayout.setRefreshing(false); }