List of usage examples for org.json JSONTokener JSONTokener
public JSONTokener(String s)
From source file:com.mobilesolutionworks.android.twitter.limit.AccessLimit.java
public AccessLimit(String json) { JSONTokener tokener = new JSONTokener(json); }
From source file:com.gmx.library.JsonUtils.java
public static JSONObject string2JSONObject(String json) { JSONObject jsonObject = null;// w ww. ja v a 2s . c o m try { JSONTokener jsonParser = new JSONTokener(json); jsonObject = (JSONObject) jsonParser.nextValue(); } catch (Exception e) { e.printStackTrace(); } return jsonObject; }
From source file:mr.robotto.ApplicationTest.java
public void testLoaders() { Context context = getContext(); AssetManager am = context.getAssets(); try {//w w w. j a va2 s. co m InputStream stream = am.open("kingVer3.json"); JSONObject drac = (JSONObject) new JSONTokener(MrStreamReader.read(stream)).nextValue(); /*MrObjectLoader loader = new MrObjectLoader(drac); MrSceneData ob = (MrSceneData)loader.parse(); getRenderer().setScene(ob); getRenderer().model = new MrModelController((MrModelData)ob.getChildren().findByKey(0), new MrModelRender());*/ //MrRobottoJsonLoader loader = new MrRobottoJsonLoader(drac); //context1 = loader.parse(); //getRenderer().setScene((MrSceneData)context1.getObjectsData().findByKey("Scene")); //getRenderer().model = new MrModelController((MrModelData)context1.getObjectsData().findByKey("Cube"), new MrModelRender()); } catch (IOException e) { e.printStackTrace(); } catch (JSONException e) { e.printStackTrace(); } assertNotNull(null); }
From source file:co.uk.sentinelweb.drawcustom.api.DrawCustomSetTemplate.java
public boolean fromJSON(String s) { try {//w w w . j ava2 s . co m JSONObject o = new JSONObject(new JSONTokener(s)); try { this.version = o.getInt("version"); } catch (JSONException e) { } try { this.width = o.getInt("width"); } catch (JSONException e) { } try { this.height = o.getInt("height"); } catch (JSONException e) { } try { String colorString = o.getString("bg"); bgColor = Color.parseColor(colorString); } catch (Exception e) { } try { description = o.getString("description"); } catch (JSONException e) { } JSONArray itemsJSON = o.getJSONArray("items"); items.clear(); itemsOrder.clear(); //JSONArray names = itemsJSON.names(); for (int i = 0; i < itemsJSON.length(); i++) { JSONObject itemJSON = itemsJSON.getJSONObject(i); DrawCustomSetItem dcsi = new DrawCustomSetItem(); String key = dcsi.fromJSON(itemJSON); items.put(key, dcsi); itemsOrder.add(key); } return true; } catch (JSONException e) { Log.d(DrawCustomGlobals.TAG, "Error parsing JSON template:", e); return false; } }
From source file:org.apache.oltu.oauth2.ext.dynamicreg.server.request.JSONHttpServletRequestWrapper.java
public Map<String, String[]> getParameterMap() { if (!bodyRead) { String body = readJsonBody(); final JSONTokener x = new JSONTokener(body); char c;//from w w w . ja va 2 s . c o m String key; if (x.nextClean() != '{') { throw new OAuthRuntimeException(format( "String '%s' is not a valid JSON object representation, a JSON object text must begin with '{'", body)); } for (;;) { c = x.nextClean(); switch (c) { case 0: throw new OAuthRuntimeException(format( "String '%s' is not a valid JSON object representation, a JSON object text must end with '}'", body)); case '}': return Collections.unmodifiableMap(parameters); default: x.back(); key = x.nextValue().toString(); } /* * The key is followed by ':'. We will also tolerate '=' or '=>'. */ c = x.nextClean(); if (c == '=') { if (x.next() != '>') { x.back(); } } else if (c != ':') { throw new OAuthRuntimeException(format( "String '%s' is not a valid JSON object representation, expected a ':' after the key '%s'", body, key)); } Object value = x.nextValue(); // guard from null values if (value != null) { if (value instanceof JSONArray) { // only plain simple arrays in this version JSONArray array = (JSONArray) value; String[] values = new String[array.length()]; for (int i = 0; i < array.length(); i++) { values[i] = String.valueOf(array.get(i)); } parameters.put(key, values); } else { parameters.put(key, new String[] { String.valueOf(value) }); } } /* * Pairs are separated by ','. We will also tolerate ';'. */ switch (x.nextClean()) { case ';': case ',': if (x.nextClean() == '}') { return Collections.unmodifiableMap(parameters); } x.back(); break; case '}': return Collections.unmodifiableMap(parameters); default: throw new OAuthRuntimeException(format( "String '%s' is not a valid JSON object representation, Expected a ',' or '}", body)); } } } return Collections.unmodifiableMap(parameters); }
From source file:net.dahanne.gallery3.client.utils.G3ConvertUtilsTest.java
@Test public void itemToAlbum() throws IOException, JSONException { URL resource = Resources.getResource("get-album-1.json"); String string = Resources.toString(resource, Charsets.UTF_8); JSONObject jsonResult = (JSONObject) new JSONTokener(string).nextValue(); Item albumItem = ItemUtils.parseJSONToItem(jsonResult); Album itemToAlbum = G3ConvertUtils.itemToAlbum(albumItem); Album expectedAlbum = new Album(); expectedAlbum.setId(1);/*from w ww .jav a2s .co m*/ expectedAlbum.setName(1); expectedAlbum.setTitle("Gallery"); expectedAlbum.setSummary(""); expectedAlbum.setAlbumUrl("http://g3.dahanne.net/index.php/"); assertEquals(expectedAlbum, itemToAlbum); //not part of the equals assertEquals("http://g3.dahanne.net/index.php/rest/data/1?size=thumb", itemToAlbum.getAlbumCoverUrl()); }
From source file:net.dahanne.gallery3.client.utils.G3ConvertUtilsTest.java
@Test public void itemToPicture() 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 pictureItem = ItemUtils.parseJSONToItem(jsonResult); Picture itemToPicture = G3ConvertUtils.itemToPicture(pictureItem); Picture expectedPicture = new Picture(); expectedPicture.setId(2);//from w w w . ja va2 s .c om expectedPicture.setTitle("March\u00e9 Bon secours"); expectedPicture.setFileName("marche-bonsecours.JPG"); expectedPicture.setThumbUrl("http://g3.dahanne.net/index.php/rest/data/2?size=thumb"); expectedPicture.setThumbWidth(150); expectedPicture.setThumbHeight(200); expectedPicture.setThumbSize(17151); expectedPicture.setResizedUrl("http://g3.dahanne.net/index.php/rest/data/2?size=resize"); expectedPicture.setResizedWidth(480); expectedPicture.setResizedHeight(640); expectedPicture.setResizedSize(58309); expectedPicture.setFileUrl("http://g3.dahanne.net/index.php/rest/data/2?size=full"); expectedPicture.setWidth(2304); expectedPicture.setHeight(3072); expectedPicture.setFileSize(675745); expectedPicture.setPublicUrl("http://g3.dahanne.net/index.php/marche-bonsecours"); assertEquals(expectedPicture, itemToPicture); }
From source file:net.dahanne.gallery3.client.utils.G3ConvertUtilsTest.java
@Test public void itemToPicture__bug3_resize_size_false() throws IOException, JSONException { URL resource = Resources.getResource("get-photo-bug3_resize_size_false.json"); String string = Resources.toString(resource, Charsets.UTF_8); JSONObject jsonResult = (JSONObject) new JSONTokener(string).nextValue(); Item pictureItem = ItemUtils.parseJSONToItem(jsonResult); Picture itemToPicture = G3ConvertUtils.itemToPicture(pictureItem); Picture expectedPicture = new Picture(); expectedPicture.setId(502);/*w ww . j ava 2 s . com*/ expectedPicture.setTitle("graduation pics 427"); expectedPicture.setFileName("graduation pics 427.jpg"); expectedPicture.setResizedUrl("http://www.iffam.org/gallery/index.php/rest/data/502?size=resize"); expectedPicture.setFileUrl("http://www.iffam.org/gallery/index.php/rest/data/502?size=full"); expectedPicture.setWidth(2576); expectedPicture.setHeight(1932); expectedPicture.setFileSize(1144458); expectedPicture.setPublicUrl("http://www.iffam.org/gallery/index.php/Twitpic/graduation-pics-427"); assertEquals(expectedPicture, itemToPicture); }
From source file:org.ESLM.Parser.JSONLessonParser.java
private void parseLesson() throws BadFormedJSONExerciseException { JSONTokener tokener = new JSONTokener(reader); JSONObject jsonLesson = new JSONObject(tokener); String title = jsonLesson.optString("title", "Untitled Lesson"); JSONArray jsonExercises = jsonLesson.optJSONArray("exercises"); Exercise[] exercises = parseExercises(jsonExercises); parsedLesson = new Lesson(title, exercises); }
From source file:org.boris.xlloop.http.JSONCodec.java
public static FunctionRequest decodeRequest(Reader r) throws IOException, JSONException { JSONTokener t = new JSONTokener(r); JSONObject jo = new JSONObject(t); String name = jo.getString("name"); JSONArray args = jo.getJSONArray("args"); String sheetName = (String) jo.opt("sheetName"); JSONObject caller = (JSONObject) jo.opt("caller"); XLSRef cref = null;/*w ww.j a v a 2 s. co m*/ if (caller != null) { cref = (XLSRef) decode(caller); } XLoper[] xargs = new XLoper[args.length()]; for (int i = 0; i < xargs.length; i++) { xargs[i] = decode(args.getJSONObject(i)); } return new FunctionRequest(name, xargs, cref, sheetName); }