List of usage examples for org.json JSONTokener JSONTokener
public JSONTokener(String s)
From source file:com.novemser.voicetest.utils.JsonParser.java
public static String parseIatResult(String json) { StringBuffer ret = new StringBuffer(); try {//from w w w. j a v a 2 s . co m JSONTokener tokener = new JSONTokener(json); JSONObject joResult = new JSONObject(tokener); JSONArray words = joResult.getJSONArray("ws"); for (int i = 0; i < words.length(); i++) { // ? JSONArray items = words.getJSONObject(i).getJSONArray("cw"); JSONObject obj = items.getJSONObject(0); ret.append(obj.getString("w")); // ?? // for(int j = 0; j < items.length(); j++) // { // JSONObject obj = items.getJSONObject(j); // ret.append(obj.getString("w")); // } } } catch (Exception e) { e.printStackTrace(); } return ret.toString(); }
From source file:com.novemser.voicetest.utils.JsonParser.java
public static String parseGrammarResult(String json) { StringBuffer ret = new StringBuffer(); try {/*from ww w. jav a2 s. co m*/ JSONTokener tokener = new JSONTokener(json); JSONObject joResult = new JSONObject(tokener); JSONArray words = joResult.getJSONArray("ws"); for (int i = 0; i < words.length(); i++) { JSONArray items = words.getJSONObject(i).getJSONArray("cw"); for (int j = 0; j < items.length(); j++) { JSONObject obj = items.getJSONObject(j); if (obj.getString("w").contains("nomatch")) { ret.append("?."); return ret.toString(); } ret.append("?" + obj.getString("w")); ret.append("?" + obj.getInt("sc")); ret.append("\n"); } } } catch (Exception e) { e.printStackTrace(); ret.append("?."); } return ret.toString(); }
From source file:com.novemser.voicetest.utils.JsonParser.java
public static String parseLocalGrammarResult(String json) { StringBuffer ret = new StringBuffer(); try {/*from w w w. j a v a 2 s .c o m*/ JSONTokener tokener = new JSONTokener(json); JSONObject joResult = new JSONObject(tokener); JSONArray words = joResult.getJSONArray("ws"); for (int i = 0; i < words.length(); i++) { JSONArray items = words.getJSONObject(i).getJSONArray("cw"); for (int j = 0; j < items.length(); j++) { JSONObject obj = items.getJSONObject(j); if (obj.getString("w").contains("nomatch")) { ret.append("?."); return ret.toString(); } ret.append("?" + obj.getString("w")); ret.append("\n"); } } ret.append("?" + joResult.optInt("sc")); } catch (Exception e) { e.printStackTrace(); ret.append("?."); } return ret.toString(); }
From source file:com.novemser.voicetest.utils.JsonParser.java
public static HashMap parseSemanticResult(String json) { HashMap<String, String> map = new HashMap<>(); try {// w ww. ja v a2 s .com JSONTokener tokener = new JSONTokener(json); JSONObject joResult = new JSONObject(tokener); if (json == null || json.equals("") || !joResult.has("semantic")) { map.put("semanticNull", "true"); return map; } if (joResult.has("operation")) map.put("operation", joResult.getString("operation")); if (joResult.has("message")) map.put("message", joResult.getString("service")); JSONObject semantic = joResult.getJSONObject("semantic"); if (semantic.has("slots")) { JSONObject slots = semantic.getJSONObject("slots"); if (slots.has("code")) map.put("code", slots.getString("code")); if (slots.has("name")) map.put("name", slots.getString("name")); if (slots.has("content")) map.put("content", slots.getString("content")); if (slots.has("datetime")) { JSONObject dateTime = slots.getJSONObject("datetime"); if (dateTime.has("dateOrig")) map.put("dateOrig", dateTime.getString("dateOrig")); if (dateTime.has("time")) map.put("time", dateTime.getString("time")); if (dateTime.has("timeOrig")) map.put("timeOrig", dateTime.getString("timeOrig")); if (dateTime.has("date")) map.put("date", dateTime.getString("date")); } } } catch (Exception e) { e.printStackTrace(); } return map; }
From source file:com.google.android.apps.muzei.util.IOUtil.java
public static JSONObject readJsonObject(String json) throws IOException, JSONException { JSONTokener tokener = new JSONTokener(json); Object val = tokener.nextValue(); if (!(val instanceof JSONObject)) { throw new JSONException("Expected JSON object."); }/*ww w. j av a 2 s. c o m*/ return (JSONObject) val; }
From source file:net.dahanne.gallery3.client.business.G3Client.java
public Item getItem(int itemId) throws G3GalleryException { Item item = null;// w w w . j a v a2 s . c o m String stringResult = sendHttpRequest(INDEX_PHP_REST_ITEM + itemId, new ArrayList<NameValuePair>(), GET, null); try { JSONObject jsonResult = (JSONObject) new JSONTokener(stringResult).nextValue(); item = ItemUtils.parseJSONToItem(jsonResult); } catch (JSONException e) { throw new G3GalleryException(e.getMessage()); } catch (ClassCastException e) { throw new G3GalleryException( "The Gallery returned an unexpected result when trying to load albums/photos; please check for info on the ReGalAndroid project page" + e.getMessage()); } return item; }
From source file:net.dahanne.gallery3.client.business.G3Client.java
private Item getItems(int albumId, List<Item> items, String type) throws G3GalleryException { logger.debug("getting items in albumId : {}, type : {}", albumId, type); Item item = this.getItem(albumId); Collection<String> members = item.getMembers(); JSONArray urls = new JSONArray(members); try {//from ww w . ja va2 s. c o m String encodedUrls; encodedUrls = URLEncoder.encode(urls.toString(), "UTF-8"); StringBuilder requestToAppend = new StringBuilder(); requestToAppend.append(INDEX_PHP_REST_ITEMS); requestToAppend.append("?urls="); requestToAppend.append(encodedUrls); requestToAppend.append("&type="); requestToAppend.append(type); String sendHttpRequest = sendHttpRequest(requestToAppend.toString(), new ArrayList<NameValuePair>(), GET, null); JSONTokener jsonTokener = new JSONTokener(sendHttpRequest); JSONArray jsonResult = (JSONArray) jsonTokener.nextValue(); for (int i = 0; i < jsonResult.length(); i++) { items.add(ItemUtils.parseJSONToItem((JSONObject) jsonResult.get(i))); } } catch (UnsupportedEncodingException e) { throw new G3GalleryException(e); } catch (JSONException e) { throw new G3GalleryException(e); } return item; }
From source file:net.dahanne.gallery3.client.business.G3Client.java
private List<Item> getTree(int albumId, String type) throws G3GalleryException { logger.debug("getting tree for albumId : {}, type : {}", albumId, type); List<Item> items = new ArrayList<Item>(); String stringResult = sendHttpRequest(INDEX_PHP_REST_TREE + albumId + "?depth=1", new ArrayList<NameValuePair>(), GET, null); try {/*from w w w.jav a 2 s .c o m*/ JSONObject jsonResult = (JSONObject) new JSONTokener(stringResult).nextValue(); items = ItemUtils.parseJSONToMultipleItems(jsonResult); } catch (JSONException e) { throw new G3GalleryException(e.getMessage()); } catch (ClassCastException e) { throw new G3GalleryException( "The Gallery returned an unexpected result when trying to load albums/photos; please check for info on the ReGalAndroid project page" + e.getMessage()); } return items; }
From source file:ru.elifantiev.yandex.oauth.AsyncContinuationHandler.java
@Override protected AuthResult doInBackground(Uri... params) { String code, error;// w ww .j ava 2 s . c o m if (params.length == 3) { // TODO: Add redir URL checking if ((code = params[2].getQueryParameter("code")) == null) { if ((error = params[2].getQueryParameter("error")) == null) { return new AuthResult("Unknown error"); } else { return new AuthResult(error); } } } else return new AuthResult("Wrong parameters count"); HttpClient client = SSLHttpClientFactory.getNewHttpClient(); HttpPost method = new HttpPost(params[0].buildUpon().path("/oauth/token").build().toString()); List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(4); nameValuePairs.add(new BasicNameValuePair("code", code)); nameValuePairs.add(new BasicNameValuePair("client_id", clientId)); nameValuePairs.add(new BasicNameValuePair("grant_type", "authorization_code")); nameValuePairs.add(new BasicNameValuePair("redirect_uri", params[1].toString())); try { method.setEntity(new UrlEncodedFormEntity(nameValuePairs)); } catch (UnsupportedEncodingException e) { return new AuthResult(e.getMessage()); } String token = null; try { HttpResponse httpResponse = client.execute(method); int statusCode = httpResponse.getStatusLine().getStatusCode(); if (statusCode == 200 || statusCode == 400) { JSONObject response = (JSONObject) (new JSONTokener(EntityUtils.toString(httpResponse.getEntity())) .nextValue()); if (response.has("access_token")) token = response.getString("access_token"); else if (response.has("error")) return new AuthResult(response.getString("error")); } else return new AuthResult("Call failed. Returned HTTP response code " + String.valueOf(statusCode)); } catch (IOException e) { return new AuthResult(e.getMessage()); } catch (JSONException e) { return new AuthResult(e.getMessage()); } return new AuthResult(new AccessToken(token)); }
From source file:com.whizzosoftware.hobson.dto.property.PropertyContainerDTOTest.java
@Test public void testJSONConstructor() { JSONObject json = new JSONObject(new JSONTokener( "{\"cclass\":{\"@id\":\"/api/v1/users/local/hubs/local/plugins/plugin1/actionClasses/actionclass1\"},\"values\":{\"foo\":\"bar\"}}")); PropertyContainerDTO dto = new PropertyContainerDTO.Builder(json).build(); assertEquals("/api/v1/users/local/hubs/local/plugins/plugin1/actionClasses/actionclass1", dto.getContainerClass().getId()); assertTrue(dto.hasPropertyValues()); assertEquals(1, dto.getValues().size()); assertEquals("bar", dto.getValues().get("foo")); }