List of usage examples for org.json JSONTokener JSONTokener
public JSONTokener(String s)
From source file:net.dahanne.gallery3.client.utils.ItemUtilsTest.java
@Test public void parseJSONTest__secretalbum() throws IOException, JSONException { URL resource = Resources.getResource("get-album-11.json"); String string = Resources.toString(resource, Charsets.UTF_8); JSONObject jsonResult = (JSONObject) new JSONTokener(string).nextValue(); Item item = ItemUtils.parseJSONToItem(jsonResult); assertEquals("http://g3.dahanne.net/index.php/rest/item/11", item.getUrl()); Entity entity = item.getEntity(); assertEquals(11, entity.getId());/*from w ww . j a v a 2s . c o m*/ assertEquals(0, entity.getCaptured()); assertEquals(1284818165, entity.getCreated()); assertEquals("", entity.getDescription()); assertEquals(0, entity.getHeight()); assertEquals(2, entity.getLevel()); assertEquals(null, entity.getMimeType()); assertEquals("G2AndroidSecretAlbum", entity.getName()); assertEquals(2, entity.getOwnerId()); assertEquals(0.952582f, entity.getRandKey(), 0.000001f); assertEquals(0, entity.getResizeHeight()); assertEquals(0, entity.getResizeWidth()); assertEquals("G2AndroidSecretAlbum", entity.getSlug()); assertEquals("created", entity.getSortColumn()); assertEquals("ASC", entity.getSortOrder()); assertEquals(0, entity.getThumbHeight()); assertEquals(0, entity.getThumbWidth()); assertEquals("G2AndroidSecretAlbum", entity.getTitle()); assertEquals("album", entity.getType()); assertEquals(1284830781, entity.getUpdated()); assertEquals(6, entity.getViewCount()); assertEquals(0, entity.getWidth()); assertEquals(0, entity.getView1()); assertEquals(0, entity.getView2()); assertEquals(ItemUtils.getItemIdFromUrl("http://g3.dahanne.net/index.php/rest/item/1"), new Integer(entity.getParent())); assertEquals("http://g3.dahanne.net/index.php/G2AndroidSecretAlbum", entity.getWebUrl()); assertEquals(true, entity.isCanEdit()); RelationShips relationShips = item.getRelationships(); assertEquals("http://g3.dahanne.net/index.php/rest/item_tags/11", relationShips.getTags().getUrl()); assertEquals(new HashSet<String>(), relationShips.getTags().getMembers()); assertEquals("http://g3.dahanne.net/index.php/rest/item_comments/11", relationShips.getComments().getUrl()); Collection<String> members = new HashSet<String>(); members.add("http://g3.dahanne.net/index.php/rest/item/13"); members.add("http://g3.dahanne.net/index.php/rest/item/14"); members.add("http://g3.dahanne.net/index.php/rest/item/15"); members.add("http://g3.dahanne.net/index.php/rest/item/16"); members.add("http://g3.dahanne.net/index.php/rest/item/18"); members.add("http://g3.dahanne.net/index.php/rest/item/20"); members.add("http://g3.dahanne.net/index.php/rest/item/22"); members.add("http://g3.dahanne.net/index.php/rest/item/25"); members.add("http://g3.dahanne.net/index.php/rest/item/26"); members.add("http://g3.dahanne.net/index.php/rest/item/28"); members.add("http://g3.dahanne.net/index.php/rest/item/30"); members.add("http://g3.dahanne.net/index.php/rest/item/32"); members.add("http://g3.dahanne.net/index.php/rest/item/34"); members.add("http://g3.dahanne.net/index.php/rest/item/35"); assertEquals(members, item.getMembers()); }
From source file:net.dahanne.gallery3.client.utils.ItemUtilsTest.java
@Test public void parseJSONTest__photo() throws IOException, JSONException { URL resource = Resources.getResource("get-photo-2.json"); String string = Resources.toString(resource, Charsets.UTF_8); JSONObject jsonResult = (JSONObject) new JSONTokener(string).nextValue(); Item item = ItemUtils.parseJSONToItem(jsonResult); assertEquals("http://g3.dahanne.net/index.php/rest/item/2", item.getUrl()); Entity entity = item.getEntity(); assertEquals(2, entity.getId());// w w w.j av a2 s . c om assertEquals(1272750491, entity.getCaptured()); assertEquals(1276227630, entity.getCreated()); assertEquals("La March\u00e9 bon secours \u00e0 Montr\u00e9al", entity.getDescription()); assertEquals(3072, entity.getHeight()); assertEquals(2, entity.getLevel()); assertEquals("image/jpeg", entity.getMimeType()); assertEquals("marche-bonsecours.JPG", entity.getName()); assertEquals(2, entity.getOwnerId()); assertEquals(0.451528, entity.getRandKey(), 0.000001f); assertEquals(640, entity.getResizeHeight()); assertEquals(480, entity.getResizeWidth()); assertEquals("marche-bonsecours", entity.getSlug()); assertEquals("created", entity.getSortColumn()); assertEquals("ASC", entity.getSortOrder()); assertEquals(200, entity.getThumbHeight()); assertEquals(150, entity.getThumbWidth()); assertEquals("March\u00e9 Bon secours", entity.getTitle()); assertEquals("photo", entity.getType()); assertEquals(1276229274, entity.getUpdated()); assertEquals(60, entity.getViewCount()); assertEquals(2304, entity.getWidth()); assertEquals(1, entity.getView1()); assertEquals(1, entity.getView2()); assertEquals(ItemUtils.getItemIdFromUrl("http://g3.dahanne.net/index.php/rest/item/1"), new Integer(entity.getParent())); assertEquals("http://g3.dahanne.net/index.php/marche-bonsecours", entity.getWebUrl()); assertEquals("http://g3.dahanne.net/index.php/rest/data/2?size=full", entity.getFileUrl()); assertEquals(675745, entity.getFileSize()); assertEquals("http://g3.dahanne.net/var/albums/marche-bonsecours.JPG?m=1276229274", entity.getFileUrlPublic()); assertEquals("http://g3.dahanne.net/index.php/rest/data/2?size=resize", entity.getResizeUrl()); assertEquals(58309, entity.getResizeSize()); assertEquals("http://g3.dahanne.net/var/resizes/marche-bonsecours.JPG?m=1276229274", entity.getResizeUrlPublic()); assertEquals("http://g3.dahanne.net/index.php/rest/data/2?size=thumb", entity.getThumbUrl()); assertEquals(17151, entity.getThumbSize()); assertEquals("http://g3.dahanne.net/var/thumbs/marche-bonsecours.JPG?m=1276229274", entity.getThumbUrlPublic()); assertEquals(false, entity.isCanEdit()); RelationShips relationShips = item.getRelationships(); assertEquals("http://g3.dahanne.net/index.php/rest/item_tags/2", relationShips.getTags().getUrl()); Collection<String> members = new HashSet<String>(); members.add("http://g3.dahanne.net/index.php/rest/tag_item/6,2"); members.add("http://g3.dahanne.net/index.php/rest/tag_item/7,2"); members.add("http://g3.dahanne.net/index.php/rest/tag_item/8,2"); members.add("http://g3.dahanne.net/index.php/rest/tag_item/9,2"); members.add("http://g3.dahanne.net/index.php/rest/tag_item/10,2"); members.add("http://g3.dahanne.net/index.php/rest/tag_item/11,2"); members.add("http://g3.dahanne.net/index.php/rest/tag_item/12,2"); assertEquals(members, relationShips.getTags().getMembers()); assertEquals("http://g3.dahanne.net/index.php/rest/item_comments/2", relationShips.getComments().getUrl()); }
From source file:net.dahanne.gallery3.client.utils.ItemUtilsTest.java
@Test public void parseJSONTest_issue32() throws IOException, JSONException { //this test just tests an exception is not thrown; a smoke test in other words URL resource = Resources.getResource("get-album-bug32_owner-id-null.json"); String string = Resources.toString(resource, Charsets.UTF_8); JSONObject jsonResult = (JSONObject) new JSONTokener(string).nextValue(); Item item = ItemUtils.parseJSONToItem(jsonResult); Entity entity = item.getEntity(); RelationShips relationShips = item.getRelationships(); }
From source file:net.dahanne.gallery3.client.utils.ItemUtilsTest.java
@Test public void parseJSONTest_issue38() throws IOException, JSONException { //this test just tests an exception is not thrown; a smoke test in other words URL resource = Resources.getResource("get-albums-no-relationships-38.json"); String string = Resources.toString(resource, Charsets.UTF_8); JSONObject jsonResult = (JSONObject) new JSONTokener(string).nextValue(); Item item = ItemUtils.parseJSONToItem(jsonResult); Entity entity = item.getEntity(); RelationShips relationShips = item.getRelationships(); }
From source file:net.dahanne.gallery3.client.utils.ItemUtilsTest.java
@Test public void parseJSONTest_issue83() throws IOException, JSONException { //this test just tests an exception is not thrown; a smoke test in other words URL resource = Resources.getResource("get-albums-no-comments-issue82.json"); String string = Resources.toString(resource, Charsets.UTF_8); JSONObject jsonResult = (JSONObject) new JSONTokener(string).nextValue(); Item item = ItemUtils.parseJSONToItem(jsonResult); Entity entity = item.getEntity(); RelationShips relationShips = item.getRelationships(); }
From source file:com.melniqw.instagramsdk.Network.java
public static JSONObject sendRequest(String url, Params params, Request request) throws IOException, JSONException { String signedUrl = getSignedUrl(url, params, request); String body = ""; if (request == Request.POST) body = params.getParamsStringUtf8(); System.out.println(TAG + " url : " + signedUrl); if (body.length() != 0) System.out.println(TAG + " body : " + body); String response = ""; for (int i = 1; i <= REQUEST_MAX_TRIES; ++i) { try {/*from ww w. ja v a 2 s .c om*/ if (i != 1) System.out.println(TAG + " try send = " + i); response = sendDummyRequest(signedUrl, body, request); break; } catch (SSLException ex) { processNetworkException(i, ex); } catch (SocketException ex) { processNetworkException(i, ex); } } // JSONObject rootJSON = new JSONObject(response); JSONObject rootJSON = (JSONObject) new JSONTokener(response).nextValue(); return rootJSON; }
From source file:com.ibm.iot.auto.bluemix.samples.ui.InputData.java
public List<CarProbe> getCarProbes() throws IOException, JSONException { List<CarProbe> carProbes = new ArrayList<CarProbe>(); FileReader fileReader = null; try {//w w w. j a v a 2 s. c o m fileReader = new FileReader(inputFile); JSONTokener jsonTokener = new JSONTokener(fileReader); JSONArray cars = new JSONArray(jsonTokener); for (int i = 0; i < cars.length(); i++) { JSONObject car = cars.getJSONObject(i); CarProbe carProbe = new CarProbe(car.getString("trip_id"), car.getString("timestamp"), car.getDouble("heading"), car.getDouble("speed"), car.getDouble("longitude"), car.getDouble("latitude")); carProbes.add(carProbe); } } finally { if (fileReader != null) { fileReader.close(); } } Collections.sort(carProbes); return carProbes; }
From source file:com.fdwills.external.http.JsonHttpResponseHandler.java
/** * Returns Object of type {@link JSONObject}, {@link JSONArray}, String, Boolean, Integer, Long, * Double or {@link JSONObject#NULL}, see {@link org.json.JSONTokener#nextValue()} * * @param responseBody response bytes to be assembled in String and parsed as JSON * @return Object parsedResponse/*from w w w . j av a 2s. c o m*/ * @throws org.json.JSONException exception if thrown while parsing JSON */ protected Object parseResponse(byte[] responseBody) throws JSONException { if (null == responseBody) return null; Object result = null; //trim the string to prevent start with blank, and test if the string is valid JSON, because the parser don't do this :(. If JSON is not valid this will return null String jsonString = getResponseString(responseBody, getCharset()); if (jsonString != null) { jsonString = jsonString.trim(); if (jsonString.startsWith(UTF8_BOM)) { jsonString = jsonString.substring(1); } if (jsonString.startsWith("{") || jsonString.startsWith("[")) { result = new JSONTokener(jsonString).nextValue(); } } if (result == null) { result = jsonString; } return result; }
From source file:org.nuclearbunny.icybee.net.impl.BitlyImpl.java
public String shrinkURL(String url) throws IOException { /**//from ww w. jav a 2s .c o m * bit.ly provides an elegant REST API that returns results in either JSON * or XML format. See http://bitly.com/app/developers for more information. */ StringBuilder buffer = new StringBuilder(BITLY_URL); buffer.append("?version=2.0.1").append("&format=json").append("&login=").append(BITLY_API_LOGIN) .append("&apiKey=").append(BITLY_API_KEY).append("&longUrl=") .append(URLEncoder.encode(url, "UTF-8")); URL bitlyURL = new URL(buffer.toString()); URLConnection connection = bitlyURL.openConnection(); String newURL = url; try { BufferedReader in = new BufferedReader(new InputStreamReader(connection.getInputStream())); JSONObject jsonObject = new JSONObject(new JSONTokener(in)); if ("OK".equals(jsonObject.opt("statusCode"))) { JSONObject results = (JSONObject) jsonObject.get("results"); results = (JSONObject) results.get(url); newURL = results.get("shortUrl").toString(); } } catch (JSONException e) { e.printStackTrace(System.err); throw new IOException(e.getMessage()); } return newURL; }
From source file:com.moarub.kipptapi.KipptAPIToken.java
@Override protected void onPostExecute(StringBuilder builder) { if (builder == null) { return;/*from w ww .j ava 2s . c o m*/ } try { JSONObject jobj = (JSONObject) new JSONTokener(builder.toString()).nextValue(); fToken = jobj.getString("api_token"); setUserName(jobj.getString("username")); } catch (JSONException e) { Log.d("ApiTokenFailure", "Can't fetch API Token (Broken JSON) " + e.getMessage()); fListener.setAPIToken(false); return; } Log.d("Result", fToken); fListener.setAPIToken(true); }