/** * Encodes this array as a compact JSON string, such as: * <pre>[94043,90210]</pre> */ @Override public String toString() { try { JSONStringer stringer = new JSONStringer(); writeTo(stringer); return stringer.toString(); } catch (JSONException e) { return null; } }
/** * Encodes this object as a compact JSON string, such as: * <pre>{"query":"Pizza","locations":[94043,90210]}</pre> */ @Override public String toString() { try { JSONStringer stringer = new JSONStringer(); writeTo(stringer); return stringer.toString(); } catch (JSONException e) { return null; } }
JSONStringer json = new JSONStringer(); StringBuilder sb=new StringBuilder(); Iterator<String> itKeys = returnedJObject.keys(); if(itKeys.hasNext()) json.object(); while (itKeys.hasNext()) json.key(k).value(returnedJObject.get(k)); Log.e("keys "+k,"value "+returnedJObject.get(k).toString()); json.endObject(); StringEntity entity = new StringEntity(json.toString()); entity.setContentType("application/json;charset=UTF-8"); entity.setContentEncoding(new BasicHeader(HTTP.CONTENT_TYPE,"application/json;charset=UTF-8")); DefaultHttpClient httpClient = new DefaultHttpClient(); HttpConnectionParams.setSoTimeout(httpClient.getParams(), Constants.ANDROID_CONNECTION_TIMEOUT*1000); HttpConnectionParams.setConnectionTimeout(httpClient.getParams(),Constants.ANDROID_CONNECTION_TIMEOUT*1000); try{ response = httpClient.execute(request);
JSONObject prefObject = new JSONObject(); prefObject.put(JsonKeys.dbType.name(), dbType.name()); prefObject.put(JsonKeys.hostname.name(), hostname); prefObject.put(JsonKeys.portnumber.name(), portnumber); prefObject.put(JsonKeys.username.name(), username); "ImportDatabaseTableCommand"+"Preferences", prefObject); JSONStringer jsonStr = new JSONStringer(); JSONWriter writer = jsonStr.object().key(JsonKeys.commandId.name()) .value(commandId).key(GenericJsonKeys.updateType.name()) .value("GetDatabaseTableList"); JSONArray dataRows = new JSONArray(); dataRows.put(listOfTables); writer.key(JsonKeys.TableList.name()).value(dataRows); writer.endObject(); pw.print(jsonStr.toString()); er.generateJson(prefix, pw, vWorkspace); } catch (JSONException e) { String message = e.getMessage().replaceAll("\n", "") .replaceAll("\"", "\\\""); ErrorUpdate er = new ErrorUpdate(message); er.generateJson(prefix, pw, vWorkspace); e.printStackTrace();
HttpPost request = new HttpPost("http://10.242.48.54/restinsert/Service1.svc/save"); JSONStringer json = new JSONStringer() .object() .key("cname").value(name) .key("cmail").value(email) .endObject(); StringEntity entity = new StringEntity(json.toString(), "UTF-8"); entity.setContentType("application/json;charset=UTF-8");//text/plain;charset=UTF-8 entity.setContentEncoding(new BasicHeader(HTTP.CONTENT_TYPE,"application/json;charset=UTF-8")); request.setEntity(entity); // Send request to WCF service DefaultHttpClient httpClient = new DefaultHttpClient(); HttpResponse response = httpClient.execute(request);
@Test public void deserializeInvalidBase64forErrorAttachment() throws JSONException { ErrorAttachmentLog log = new ErrorAttachmentLog(); log.setTimestamp(new Date()); log.setId(UUID.randomUUID()); log.setErrorId(UUID.randomUUID()); log.setData(new byte[0]); log.setContentType("text/plain"); JSONStringer jsonWriter = new JSONStringer(); jsonWriter.object(); log.write(jsonWriter); jsonWriter.endObject(); JSONObject json = new JSONObject(jsonWriter.toString()); json.put(DATA, "a"); try { new ErrorAttachmentLog().read(json); Assert.fail("Expected json exception here"); } catch (JSONException e) { assertEquals("bad base-64", e.getMessage()); } }
public static String writeToJson(Form form) { JSONStringer js = new JSONStringer(); js.object(); List<Field> fields = new ArrayList<Field>(); collectVisibleFields(form, fields); fieldsToJson(js, fields); js.endObject(); return js.toString(); }
@Override public void write(JSONStringer writer) throws JSONException { /* Serialize part B before. */ JSONUtils.write(writer, BASE_TYPE, mProperties.optString(BASE_TYPE, null)); JSONUtils.write(writer, BASE_DATA, mProperties.optJSONObject(BASE_DATA)); /* Then part C. */ JSONArray names = mProperties.names(); if (names != null) { for (int i = 0; i < names.length(); i++) { String name = names.getString(i); if (!name.equals(BASE_TYPE) && !name.equals(BASE_DATA)) { writer.key(name).value(mProperties.get(name)); } } } }
result = jsonArray.toString(getIndentingSize()); } else { result = jsonArray.toString(); result = jsonObject.toString(getIndentingSize()); } else { result = jsonObject.toString(); result = jsonStringer.toString(); } else if (this.jsonValue instanceof JSONTokener) { JSONTokener jsonTokener = (JSONTokener) this.jsonValue; result = this.jsonRepresentation.getText(); } catch (IOException e) { throw new JSONException(e);
portnumber, username, password, tableName, dBorSIDName, 10); JSONStringer jsonStr = new JSONStringer(); JSONWriter writer = jsonStr.object().key(JsonKeys.commandId.name()).value(commandId) .key(GenericJsonKeys.updateType.name()).value("ImportDatabaseTablePreview").key(JsonKeys.tableName.name()).value(tableName); JSONArray arr = new JSONArray(data.get(0)); writer.key(JsonKeys.headers.name()).value(arr); JSONArray dataRows = new JSONArray(); for(int i = 1; i<data.size(); i++) { dataRows.put(data.get(i)); writer.key(JsonKeys.rows.name()).value(dataRows); writer.endObject(); pw.println(jsonStr.toString()); } catch (SQLException e) { e.printStackTrace(); } catch (JSONException e) { e.printStackTrace();
private static void addToStringer(JSONStringer stringer, Object o) throws JSONException { if (o instanceof JSONObject) { stringer.object(); JSONObject object = (JSONObject) o; Iterator<String> keyIterator = object.keys(); ArrayList<String> keys = new ArrayList<>(); while (keyIterator.hasNext()) { keys.add(keyIterator.next()); } Collections.sort(keys); for (String key : keys) { stringer.key(key); addToStringer(stringer, object.opt(key)); } stringer.endObject(); return; } if (o instanceof JSONArray) { JSONArray array = (JSONArray) o; stringer.array(); for (int i = 0; i < array.length(); ++i) { addToStringer(stringer, array.get(i)); } stringer.endArray(); return; } stringer.value(o); }
if(cr.moveToFirst()){ reports = new JSONArray(); do{ try{ SessionData session = new SessionData(getApplicationContext()); JSONArray ja = new JSONArray(cr.getString(cr.getColumnIndex(gh._ReportData))); JSONStringer jo = new JSONStringer().object(). key("ReportedBy").value(cr.getInt(cr.getColumnIndex(gh._CreatedBy)) != 0 ? cr.getInt(cr.getColumnIndex(gh._CreatedBy)) : session.UserID_get()). key("TourReportData").value(ja). key("localID").value(cr.getInt(cr.getColumnIndex(gh._LocalID))). key("TourInstanceID").value(cr.getInt(cr.getColumnIndex(gh._TourInstanceID))). key("TourDetailID").value(cr.getInt(cr.getColumnIndex(gh._TourDetailID))). key("TourReportTypeID").value(cr.getInt(cr.getColumnIndex(gh.GuardTourReports_ReportTypeID))). key("TourReportType").value(cr.getString(cr.getColumnIndex(gh.GuardTourReports_ReportType))). key("TourReportComments").value(cr.getString(cr.getColumnIndex(gh._TourReportComments))). key("DateCreated").value(cr.getString(cr.getColumnIndex(gh._DateCreated))) .endObject(); reports.put(jo); } catch(JSONException jse){ jse.printStackTrace(); return null; } catch(Exception e){ e.printStackTrace(); return null; } }while(cr.moveToNext());
HttpPost request = new HttpPost("http://ws.audioscrobbler.com/2.0/something_here"); request.setHeader("Accept", "application/json"); request.setHeader("Content-type", "application/json"); JSONStringer vm; try { vm = new JSONStringer().object().key("method") .value("auth.getMobileSession").key("api_key").value(apikey) .key("api_sig") .value(authSig).key("format").value("json").key(authToken).value(authToken).key("username").value(username) .endObject(); StringEntity entity = new StringEntity(vm.toString()); request.setEntity(entity); HttpClient httpClient = new DefaultHttpClient(); HttpResponse response = httpClient.execute(request);
JSONStringer json = new JSONStringer(); json.object(); json.key("stuff").value("1") .key("name").value("2") .key("type_name").value("3") .key("type").value("4"); json.endObject(); //after calling the method which returns a jsonobject like above: JSONObject jo=new JSONObject(json.toString()); jo.put("hi", "hi2");
@Override public JobResult doRun() throws FastcatSearchException { JSONStringer stringer = new JSONStringer(); try { stringer.object().key("taskState").array(); TaskKey taskKey = entry.getKey(); TaskState taskState = entry.getValue(); stringer.object() .key("summary").value(taskKey.getSummary() + " " + taskState.getSummary()) .key("state").value(taskState.getState()) .key("step").value(taskState.getStep()) stringer.endArray().endObject(); result.setResult(stringer.toString()); return new JobResult(result); logger.debug("exception occurs : {}",e.getMessage()); } finally {
private static HttpResponse doPost(String url, JSONStringer json) { try { HttpPost request = new HttpPost(url); StringEntity entity; entity = new StringEntity(json.toString()); entity.setContentType("application/json;charset=UTF-8"); entity.setContentEncoding(new BasicHeader(HTTP.CONTENT_TYPE, "application/json;charset=UTF-8")); request.setEntity(entity); try { DefaultHttpClient httpClient = new DefaultHttpClient(); HttpResponse response = httpClient.execute(request); return response; } catch (ClientProtocolException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } catch (UnsupportedEncodingException e) { e.printStackTrace(); } return null; }