List of usage examples for org.json JSONObject put
public JSONObject put(String key, Object value) throws JSONException
From source file:com.controlj.addon.weather.util.ResponseWriter.java
public void putStringChild(String parentName, String fieldName, String value) { try {//www. ja va2 s . co m JSONObject child = getOrCreateObject(parentName); child.put(fieldName, value); } catch (JSONException e) { Logging.println("Error encoding json int value", e); } }
From source file:com.controlj.addon.weather.util.ResponseWriter.java
public void addValidationError(String fieldName, String message) { try {/* w w w.java 2 s . co m*/ JSONArray errors = getOrCreateArray(ERROR_LIST); JSONObject error = new JSONObject(); error.put("errortype", "validation"); error.put("field", fieldName); error.put("message", message); errors.put(error); } catch (JSONException e) { Logging.println("Error trying to write out errors with JSON", e); } }
From source file:com.controlj.addon.weather.util.ResponseWriter.java
public void addError(String message) { try {// ww w. j a v a 2s. co m JSONArray errors = getOrCreateArray(ERROR_LIST); JSONObject error = new JSONObject(); error.put("errortype", "general"); error.put("message", message); errors.put(error); } catch (JSONException e) { Logging.println("Error trying to write out errors with JSON", e); } }
From source file:org.cloudfoundry.client.lib.util.JsonUtil.java
public static JSONObject convertMapToJson(Map<String, Object> map) throws JSONException { JSONObject res = new JSONObject(); for (String key : map.keySet()) { res.put(key, map.get(key)); }//from w w w.ja v a2 s . c om return res; }
From source file:com.layer.atlas.messenger.AtlasIdentityProvider.java
private String[] refreshContacts(boolean requestIdentityToken, String nonce, String userName) { try {//from w w w.j a va 2 s. co m String url = "https://layer-identity-provider.herokuapp.com/apps/" + appId + "/atlas_identities"; HttpPost post = new HttpPost(url); post.setHeader("Content-Type", "application/json"); post.setHeader("Accept", "application/json"); post.setHeader("X_LAYER_APP_ID", appId); JSONObject rootObject = new JSONObject(); if (requestIdentityToken) { rootObject.put("nonce", nonce); rootObject.put("name", userName); } else { rootObject.put("name", "Web"); // name must be specified to make entiry valid } StringEntity entity = new StringEntity(rootObject.toString(), "UTF-8"); entity.setContentType("application/json"); post.setEntity(entity); HttpResponse response = (new DefaultHttpClient()).execute(post); if (HttpStatus.SC_OK != response.getStatusLine().getStatusCode() && HttpStatus.SC_CREATED != response.getStatusLine().getStatusCode()) { StringBuilder sb = new StringBuilder(); sb.append("Got status ").append(response.getStatusLine().getStatusCode()).append(" [") .append(response.getStatusLine()).append("] when logging in. Request: ").append(url); if (requestIdentityToken) sb.append(" login: ").append(userName).append(", nonce: ").append(nonce); Log.e(TAG, sb.toString()); return new String[] { null, sb.toString() }; } String responseString = EntityUtils.toString(response.getEntity()); JSONObject jsonResp = new JSONObject(responseString); JSONArray atlasIdentities = jsonResp.getJSONArray("atlas_identities"); List<Participant> participants = new ArrayList<Participant>(atlasIdentities.length()); for (int i = 0; i < atlasIdentities.length(); i++) { JSONObject identity = atlasIdentities.getJSONObject(i); Participant participant = new Participant(); participant.firstName = identity.getString("name"); participant.userId = identity.getString("id"); participants.add(participant); } if (participants.size() > 0) { setParticipants(participants); save(); if (debug) Log.d(TAG, "refreshContacts() contacts: " + atlasIdentities); } if (requestIdentityToken) { String error = jsonResp.optString("error", null); String identityToken = jsonResp.optString("identity_token"); return new String[] { identityToken, error }; } return new String[] { null, "Refreshed " + participants.size() + " contacts" }; } catch (Exception e) { Log.e(TAG, "Error when fetching identity token", e); return new String[] { null, "Cannot obtain identity token. " + e }; } }
From source file:com.layer.atlas.messenger.AtlasIdentityProvider.java
private boolean save() { Collection<Participant> participants; synchronized (participantsMap) { participants = participantsMap.values(); }/*from ww w . java 2 s. c om*/ JSONArray contactsJson; try { contactsJson = new JSONArray(); for (Participant participant : participants) { JSONObject contactJson = new JSONObject(); contactJson.put("id", participant.userId); contactJson.put("first_name", participant.firstName); contactJson.put("last_name", participant.lastName); contactsJson.put(contactJson); } } catch (JSONException e) { Log.e(TAG, "Error while saving", e); return false; } SharedPreferences.Editor editor = context.getSharedPreferences("contacts", Context.MODE_PRIVATE).edit(); editor.putString("json", contactsJson.toString()); editor.commit(); return true; }
From source file:com.codebutler.farebot.keys.ClassicSectorKey.java
public JSONObject toJSON() { try {/*from www . j a v a 2s . c o m*/ JSONObject json = new JSONObject(); json.put(TYPE, mType); json.put(KEY, Utils.getHexString(mKey)); return json; } catch (Exception ex) { throw new RuntimeException(ex); } }
From source file:com.basetechnology.s0.agentserver.field.MoneyField.java
public JSONObject toJson() throws JSONException { JSONObject json = new JSONObject(); json.put("type", "money"); if (symbol.name != null) json.put("name", symbol.name); if (label != null) json.put("label", label); if (description != null) json.put("description", description); if (defaultValue != 0) json.put("default_value", defaultValue); if (minValue != 0) json.put("min_value", minValue); if (maxValue != Double.MAX_VALUE) json.put("max_value", maxValue); if (nominalWidth != 0) json.put("nominal_width", nominalWidth); if (compute != null) json.put("compute", compute); return json;//w ww . j a v a 2s.c o m }
From source file:com.googlecode.android_scripting.facade.ui.TimePickerDialogTask.java
@Override public void onCreate() { super.onCreate(); mDialog = new TimePickerDialog(getActivity(), new TimePickerDialog.OnTimeSetListener() { public void onTimeSet(TimePicker view, int hour, int minute) { JSONObject result = new JSONObject(); try { result.put("which", "positive"); result.put("hour", hour); result.put("minute", minute); setResult(result);/* w w w .j a v a 2 s . co m*/ } catch (JSONException e) { throw new AndroidRuntimeException(e); } } }, mHour, mMinute, mIs24Hour); mDialog.setOnCancelListener(new DialogInterface.OnCancelListener() { public void onCancel(DialogInterface view) { JSONObject result = new JSONObject(); try { result.put("which", "neutral"); result.put("hour", mHour); result.put("minute", mMinute); setResult(result); } catch (JSONException e) { throw new AndroidRuntimeException(e); } } }); mDialog.setOnDismissListener(new DialogInterface.OnDismissListener() { public void onDismiss(DialogInterface dialog) { JSONObject result = new JSONObject(); try { result.put("which", "negative"); result.put("hour", mHour); result.put("minute", mMinute); setResult(result); } catch (JSONException e) { throw new AndroidRuntimeException(e); } } }); mDialog.show(); mShowLatch.countDown(); }
From source file:ca.viaware.dlna.webinterface.InterfaceServer.java
public void start() { server.createContext("/list/", new HttpHandler() { private void exploreEntries(JSONArray children, ArrayList<LibraryEntry> entries, int parent) { for (LibraryEntry e : entries) { if (e.getParent() == parent) { JSONObject entryJson = new JSONObject(); entryJson.put("name", e.getName()); entryJson.put("id", e.getId()); entryJson.put("type", e.getTypeID()); if (e.getTypeID() == EntryType.CONTAINER) { JSONArray sub = new JSONArray(); entryJson.put("children", sub); exploreEntries(sub, entries, e.getId()); }//from ww w . j a va 2 s. c o m children.put(entryJson); } } } @Override public void handle(HttpExchange exchange) throws IOException { String json = (String) Library.runInstance(new LibraryInstanceRunner() { @Override public Object run(LibraryFactory factory) { JSONArray root = new JSONArray(); ArrayList<LibraryEntry> entries = factory.getAll(); exploreEntries(root, entries, -1); return new JSONObject().put("entries", root).toString(4); } }); while (exchange.getRequestBody().read() != -1) { } byte[] bytes = json.getBytes("UTF-8"); Headers headers = exchange.getResponseHeaders(); headers.set("CONTENT-TYPE", "application/json"); headers.set("CONTENT-LANGUAGE", "en"); exchange.sendResponseHeaders(200, bytes.length); exchange.getResponseBody().write(bytes); exchange.getResponseBody().close(); } }); server.start(); Log.info("Started Web Interface HTTP server"); }