canonical example by Tabnine
JSONObject jo = new JSONObject(); jo.put("firstName", "John"); jo.put("lastName", "Doe"); JSONArray ja = new JSONArray(); ja.put(jo); JSONObject mainObj = new JSONObject(); mainObj.put("employees", ja);
/** * Create a JSON object that represents this data structure. * @return a JSON serialization of this data structure. */ public JSONObject jsonSerialize() { JSONObject jsonObject = new JSONObject(); jsonObject.put(ID_KEY, mId); jsonObject.put(URL_KEY, mUrl); if (mExtraData.length() > 0) { jsonObject.put(EXTRA_KEY, mExtraData); } return jsonObject; }
JSONObject cred = new JSONObject(); JSONObject auth=new JSONObject(); JSONObject parent=new JSONObject(); cred.put("username","adm"); cred.put("password", "pwd"); auth.put("tenantName", "adm"); auth.put("passwordCredentials", cred); parent.put("auth", auth); OutputStreamWriter wr= new OutputStreamWriter(con.getOutputStream()); wr.write(parent.toString());
public JSONObject outputDependencies(PrintStream out, boolean jsonOutput, boolean appendTaskType, List<Task> tasks) throws Exception { if (out != null) { out.println(STAGE_DEPENDENCIES + ":"); } JSONObject json = jsonOutput ? new JSONObject(new LinkedHashMap<>()) : null; for (Task task : tasks) { JSONObject jsonOut = outputDependencies(task, out, json, jsonOutput, appendTaskType, 2); if (jsonOutput && jsonOut != null) { json.put(task.getId(), jsonOut); } } return jsonOutput ? json : null; }
public String toJson() { JSONObject jsonObject= new JSONObject(); try { jsonObject.put(CALLBACK_ID_STR, getCallbackId()); jsonObject.put(DATA_STR, getData()); jsonObject.put(HANDLER_NAME_STR, getHandlerName()); String data = getResponseData(); if (TextUtils.isEmpty(data)) { jsonObject.put(RESPONSE_DATA_STR, data); } else { jsonObject.put(RESPONSE_DATA_STR, new JSONTokener(data).nextValue()); } jsonObject.put(RESPONSE_DATA_STR, getResponseData()); jsonObject.put(RESPONSE_ID_STR, getResponseId()); return jsonObject.toString(); } catch (JSONException e) { e.printStackTrace(); } return null; }
private void addCardStyle(JSONObject cardData, Card card) { try { GsonBuilder gsonBuilder = new GsonBuilder(); gsonBuilder.registerTypeAdapter(Float.class, new JsonSerializer<Float>() { @Override public JsonElement serialize(final Float src, final Type typeOfSrc, final JsonSerializationContext context) { try { if (src.isInfinite() || src.isNaN()) { return new JsonPrimitive(0f); } BigDecimal value = BigDecimal.valueOf(src); return new JsonPrimitive(value); } catch (Exception e) { e.printStackTrace(); } return new JsonPrimitive(0f); } }); Gson gson = gsonBuilder.create(); GridCard.GridStyle gridStyle = new GridCard.GridStyle(); if (card instanceof BannerCard) { gridStyle.aspectRatio = 3.223f; } cardData.put(Card.KEY_STYLE, new JSONObject(gson.toJson(gridStyle))); } catch (JSONException e) { e.printStackTrace(); } }
private void collectCurrentSizeRange(@NonNull Display display, @NonNull JSONObject container) throws JSONException { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) { final Point smallest = new Point(); final Point largest = new Point(); display.getCurrentSizeRange(smallest, largest); final JSONObject result = new JSONObject(); result.put("smallest", new JSONArray(Arrays.asList(smallest.x, smallest.y))); result.put("largest", new JSONArray(Arrays.asList(largest.x, largest.y))); container.put("currentSizeRange", result); } }
private void transformCardCellData(JSONObject cardData) { try { if (cardData.has("iconList")) { cardData.put(Card.KEY_ITEMS, cardData.getJSONArray("iconList")); } else if (cardData.has("centerBannerList")) { cardData.put(Card.KEY_ITEMS, cardData.getJSONArray("centerBannerList")); } } catch (JSONException e) { e.printStackTrace(); } }
public JSONObject outputDependencies(PrintStream out, boolean jsonOutput, boolean appendTaskType, List<Task> tasks) throws Exception { if (out != null) { out.println("STAGE DEPENDENCIES:"); } JSONObject json = jsonOutput ? new JSONObject(new LinkedHashMap<>()) : null; for (Task task : tasks) { JSONObject jsonOut = outputDependencies(task, out, json, jsonOutput, appendTaskType, 2); if (jsonOutput && jsonOut != null) { json.put(task.getId(), jsonOut); } } return jsonOutput ? json : null; }
JSONObject student1 = new JSONObject(); try { student1.put("id", "3"); student1.put("name", "NAME OF STUDENT"); student1.put("year", "3rd"); student1.put("curriculum", "Arts"); student1.put("birthday", "5/5/1993"); JSONObject student2 = new JSONObject(); try { student2.put("id", "2"); student2.put("name", "NAME OF STUDENT2"); student2.put("year", "4rd"); student2.put("curriculum", "scicence"); student2.put("birthday", "5/5/1993"); JSONArray jsonArray = new JSONArray(); jsonArray.put(student1); jsonArray.put(student2); JSONObject studentsObj = new JSONObject(); studentsObj.put("Students", jsonArray); String jsonStr = studentsObj.toString(); System.out.println("jsonString: "+jsonStr);
JSONObject cred = new JSONObject(); JSONObject auth=new JSONObject(); JSONObject parent=new JSONObject(); cred.put("username","adm"); cred.put("password", "pwd"); auth.put("tenantName", "adm"); auth.put("passwordCredentials", cred.toString()); // <-- toString() parent.put("auth", auth.toString()); // <-- toString() OutputStreamWriter wr= new OutputStreamWriter(con.getOutputStream()); wr.write(parent.toString());
RequestQueue requestQueue = Volley.newRequestQueue(this); String URL = "http://..."; JSONObject jsonBody = new JSONObject(); jsonBody.put("Title", "Android Volley Demo"); jsonBody.put("Author", "BNK"); final String mRequestBody = jsonBody.toString(); e.printStackTrace();
public void createAnUserJSONObject(View view) { JSONObject jsonObject = new JSONObject(); try { jsonObject.put("firstname", "Rohit"); jsonObject.put("lastname", "Kumar"); } catch (JSONException e) { e.printStackTrace();
ArrayList<String> list = new ArrayList<String>(); list.add("john"); list.add("mat"); list.add("jason"); list.add("matthew"); JSONObject school = new JSONObject(); school.put("class","4"); school.put("name", new JSONArray(list));
private JSONObject toJSONObject(Map<?, ?> m) { JSONObject obj = new JSONObject(); for (Map.Entry<?, ?> entry : m.entrySet()) { Object k = entry.getKey(); Object value = entry.getValue(); String key = String.valueOf(k); value = toJSONInstance(value); obj.put(key, value); } return obj; }