List of usage examples for org.json JSONObject getString
public String getString(String key) throws JSONException
From source file:ch.lom.clemens.android.bibliography.data.JSONWebConnector.java
public MCollection[] getCollections() { if (!isConnected()) { StartActivity.singleton.doAuthentication(StartActivity.MENDELEY_RETRIEVE_COLLECTIONS); return null; }/* w ww.j a v a2 s.co m*/ MCollection[] collections = null; try { String strResponse = connect( "http://www.mendeley.com/oapi/library/collections?consumer_key=" + m_consumerkey); Log.i("MendeleyComm", strResponse); JSONArray jcols = new JSONArray(strResponse); collections = new MCollection[jcols.length()]; for (int i = 0; i < jcols.length(); i++) { JSONObject collection = jcols.getJSONObject(i); collections[i] = new MCollection(null); collections[i].id = collection.getString("id"); collections[i].name = collection.getString("name"); collections[i].type = collection.getString("type"); collections[i].size = collection.getInt("size"); } } catch (Exception e) { Log.e("MendeleyConnector", "Got exception when parsing online collection data"); Log.e("MendeleyConnector", e.getClass().getSimpleName() + ": " + e.getMessage()); } return collections; }
From source file:ch.lom.clemens.android.bibliography.data.JSONWebConnector.java
public MGroup[] getGroups(MendeleyConnector con) { if (!isConnected()) { StartActivity.singleton.doAuthentication(StartActivity.MENDELEY_RETRIEVE_COLLECTIONS); return null; }/*from ww w. ja va 2 s . c o m*/ MGroup[] groups = null; try { String strResponse = connect( "http://www.mendeley.com/oapi/library/groups?consumer_key=" + m_consumerkey); Log.i("MendeleyComm", strResponse); JSONArray items = new JSONArray(strResponse); groups = new MGroup[items.length()]; for (int i = 0; i < items.length(); i++) { JSONObject collection = items.getJSONObject(i); groups[i] = new MGroup(con); groups[i].id = collection.getString("id"); groups[i].name = collection.getString("name"); groups[i].type = collection.getString("type"); groups[i].size = collection.getInt("size"); } } catch (Exception e) { Log.e("MendeleyConnector", "Got exception when parsing online group data"); Log.e("MendeleyConnector", e.getClass().getSimpleName() + ": " + e.getMessage()); } return groups; }
From source file:ch.lom.clemens.android.bibliography.data.JSONWebConnector.java
private MDocument parseDocumentResult(MDocument mdoc, String strResponse) throws JSONException { JSONObject doc = new JSONObject(strResponse); try {//from ww w . j a v a 2 s . co m mdoc.title = doc.getString("title"); } catch (JSONException e) { Log.w("WebConnector", "Parsing error while getting document title"); } try { mdoc.year = doc.getString("year"); } catch (JSONException e) { Log.w("WebConnector", "Parsing error while getting document year"); } try { mdoc.notes = doc.getString("notes"); } catch (JSONException e) { Log.w("WebConnector", "Parsing error while getting document notes"); } try { mdoc.type = doc.getString("type"); } catch (JSONException e) { Log.w("WebConnector", "Parsing error while getting document type"); } try { mdoc.urls = new String[] { doc.getString("url") }; } catch (JSONException e) { Log.w("WebConnector", "Parsing error while getting document url"); } try { mdoc.pages = doc.getString("pages"); } catch (JSONException e) { Log.w("WebConnector", "Parsing error while getting document pages"); } try { mdoc.docabstract = doc.getString("abstract"); } catch (JSONException e) { Log.w("WebConnector", "Parsing error while getting document abstract"); } try { JSONArray authors = doc.getJSONArray("authors"); String[] strAuthors = new String[authors.length()]; for (int j = 0; j < authors.length(); j++) { strAuthors[j] = authors.getString(j); } mdoc.authors = strAuthors; } catch (JSONException e) { Log.w("WebConnector", "Parsing error while getting document authors"); } try { JSONArray tags = doc.getJSONArray("tags"); String[] mtags = new String[tags.length()]; for (int j = 0; j < tags.length(); j++) { mtags[j] = tags.getString(j); } mdoc.tags = mtags; } catch (JSONException e) { Log.w("WebConnector", "Parsing error while getting document tags"); } try { JSONObject ids = doc.getJSONObject("identifiers"); mdoc.identifiers = new HashMap<String, String>(); JSONArray names = ids.names(); for (int j = 0; j < names.length(); j++) { mdoc.identifiers.put(names.getString(j), ids.getString(names.getString(j))); } } catch (JSONException e) { Log.w("WebConnector", "Parsing error while getting document identifiers"); } try { JSONObject ids = doc.getJSONObject("discipline"); mdoc.discipline = new HashMap<String, String>(); JSONArray names = ids.names(); for (int j = 0; j < names.length(); j++) { mdoc.discipline.put(names.getString(j), ids.getString(names.getString(j))); } } catch (JSONException e) { Log.w("WebConnector", "Parsing error while getting document disciplines"); } return mdoc; }
From source file:gmc.hotplate.util.JsonParser.java
public Recipe parseRecipeObject(JSONObject obj) { Recipe recipe = null;/*w w w .j ava2s .co m*/ try { int recipeId = obj.getInt(TAG_RECIPE_ID); String recipeName = obj.getString(TAG_RECIPE_NAME); String recipeDescription = obj.getString(TAG_RECIPE_DESCRIPTION); int personCount = obj.getInt(TAG_RECIPE_PERSON); JSONArray jsonSteps = obj.getJSONArray(TAG_RECIPE_STEPS); List<Step> steps = new ArrayList<Step>(); for (int i = 0; i < jsonSteps.length(); i++) { Step step = parseStepObject(jsonSteps.getJSONObject(i)); if (step != null) { steps.add(step); } } List<Ingredient> ingredients = new ArrayList<Ingredient>(); JSONArray jsonIngredients = obj.getJSONArray(TAG_RECIPE_INGREDIENTS); for (int i = 0; i < jsonIngredients.length(); i++) { Ingredient ingredient = parseIngredientObject(jsonIngredients.getJSONObject(i)); if (ingredient != null) { ingredients.add(ingredient); } } List<String> categories = new ArrayList<String>(); if (obj.has(TAG_RECIPE_CATEGORIES)) { JSONArray jsonCategories = obj.getJSONArray(TAG_RECIPE_CATEGORIES); for (int i = 0; i < jsonCategories.length(); i++) { String tag = jsonCategories.getString(i); categories.add(tag); } } recipe = new Recipe(recipeId, recipeName, recipeDescription, personCount, steps); recipe.setIngredients(ingredients); recipe.setCategories(categories); } catch (JSONException e) { Log.w(LOG_TAG, "ParseRecipe() error: " + e.getMessage()); } return recipe; }
From source file:gmc.hotplate.util.JsonParser.java
public Step parseStepObject(JSONObject obj) { Step step = null;// w ww. j a v a2s . co m try { int stepId = obj.getInt(TAG_STEP_ID); String stepDescription = obj.getString(TAG_STEP_DESCRIPTION); int seconds = obj.getInt(TAG_STEP_TIME); step = new Step(stepId, stepDescription, seconds); } catch (JSONException e) { Log.w(LOG_TAG, "ParseStep() error: " + e.getMessage()); } return step; }
From source file:gmc.hotplate.util.JsonParser.java
public Ingredient parseIngredientObject(JSONObject obj) { Ingredient ingredient = null;//ww w . j a va2s .c o m try { String name = obj.getString(TAG_INGREDIENTS_NAME); double amount = obj.getDouble(TAG_INGREDIENTS_AMOUNT); String type = obj.getString(TAG_INGREDIENTS_TYPE); ingredient = new Ingredient(name, amount, type); } catch (JSONException e) { Log.w(LOG_TAG, "ParseIngredient() error: " + e.getMessage()); } return ingredient; }
From source file:com.example.android.camera2basic.RecognizeText.java
@Override protected String doInBackground(Object... paths) { System.out.println("Performing Visual Recognition..."); // params comes from the execute() call: params[0] is the url. com.ibm.watson.developer_cloud.visual_recognition.v3.VisualRecognition service = new com.ibm.watson.developer_cloud.visual_recognition.v3.VisualRecognition( com.ibm.watson.developer_cloud.visual_recognition.v3.VisualRecognition.VERSION_DATE_2016_05_20); service.setApiKey("26a259b7f5dc0f5c8c1cc933d8722b0e66aed5df"); File actualImageFile = new File((String) paths[0]); // Library link : https://github.com/zetbaitsu/Compressor Bitmap compressedBitmap = Compressor.getDefault(mContext).compressToBitmap(actualImageFile); DirectoryPath = (String) paths[1]; File compressedImage = bitmapToFile(compressedBitmap); System.out.println("The size of image for RecognizeText (in kB) : " + (compressedImage.length() / 1024)); // TODO Image size may be still greater than 1 MB ! VisualRecognitionOptions options = new VisualRecognitionOptions.Builder().images(compressedImage).build(); RecognizedText result = service.recognizeText(options).execute(); System.out.println("OnPostExecute..."); System.out.println(result);//from ww w . j av a 2s .c o m try { JSONObject obj = new JSONObject(result.toString()); JSONObject resultarray1 = obj.getJSONArray("images").getJSONObject(0); text = resultarray1.getString("text"); System.out.println("Recognize Text : " + text); //new TextToSpeechTask().execute(classes,DirectoryPath); } catch (JSONException e) { System.out.println("Nothing Detected In Text "); } countDownLatch.countDown(); System.out.println("Latch counted down in Recongize Text"); return result.toString(); }
From source file:ai.susi.mind.SusiCognition.java
/** * The cognition is the result of a though extraction. We can reconstruct * the dispute as list of last mindstates using the cognition data. * @return a backtrackable thought reconstructed from the cognition data *///from w w w.j a v a 2 s.c o m public SusiThought recallDispute() { SusiThought dispute = new SusiThought(); if (this.json.has("answers")) { JSONArray answers = this.json.getJSONArray("answers"); // in most cases there is only one answer for (int i = answers.length() - 1; i >= 0; i--) { SusiThought clonedThought = new SusiThought(answers.getJSONObject(i)); dispute.addObservation("query", this.json.getString("query")); // we can unify "query" in queries SusiAction expressionAction = null; for (SusiAction a : clonedThought.getActions()) { ArrayList<String> phrases = a.getPhrases(); if (phrases.size() > 0) { expressionAction = a; break; } } if (expressionAction != null) dispute.addObservation("answer", expressionAction.getPhrases().get(0)); // we can unify with "answer" in queries // add all data from the old dispute JSONArray clonedData = clonedThought.getData(); if (clonedData.length() > 0) { JSONObject row = clonedData.getJSONObject(0); row.keySet().forEach(key -> { if (key.startsWith("_")) dispute.addObservation(key, row.getString(key)); }); //data.put(clonedData.get(0)); } } } return dispute; }
From source file:org.collectionspace.chain.csp.webui.record.RecordCreateUpdate.java
private void setRelations(Storage storage, String csid, JSONArray relations) throws JSONException, ExistException, UnimplementedException, UnderlyingStorageException { deleteAllRelations(storage, csid);/* ww w .j av a 2s .c o m*/ for (int i = 0; i < relations.length(); i++) { // Extract data from miniobject JSONObject in = relations.getJSONObject(i); String dst_type = spec.getRecordByWebUrl(in.getString("recordtype")).getID(); String dst_id = in.getString("csid"); String type = in.getString("relationshiptype"); // Create relation JSONObject r = new JSONObject(); r.put("src", base + "/" + csid); r.put("dst", dst_type + "/" + dst_id); r.put("type", type); storage.autocreateJSON("relations/main", r, null); } }
From source file:org.collectionspace.chain.csp.webui.record.RecordCreateUpdate.java
public String sendJSON(Storage storage, String path, JSONObject data, JSONObject restrictions) throws ExistException, UnimplementedException, UnderlyingStorageException, JSONException { final String WORKFLOW_TRANSITION = "workflowTransition"; final String WORKFLOW_TRANSITION_LOCK = "lock"; JSONObject fields = data.optJSONObject("fields"); JSONArray relations = data.optJSONArray("relations"); if (path != null) { // Update if (fields != null) storage.updateJSON(base + "/" + path, fields, restrictions); } else {/*from w w w .ja va2 s . com*/ // Create if (fields != null) path = storage.autocreateJSON(base, fields, restrictions); } if (relations != null) setRelations(storage, path, relations); if (record.supportsLocking() && data.has(WORKFLOW_TRANSITION) && WORKFLOW_TRANSITION_LOCK.equalsIgnoreCase(data.getString(WORKFLOW_TRANSITION))) { // If any problem, will throw exception. storage.transitionWorkflowJSON(base + "/" + path, WORKFLOW_TRANSITION_LOCK); } return path; }