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);
/** * Put a value in the JSONArray, where the value will be a JSONObject which * is produced from a Map. * * @param value * A Map value. * @return this. */ public JSONArray put(Map<String, Object> value) { this.put(new JSONObject(value)); return this; }
cookieJSONArray = new JSONArray(value); if (null == cookieJSONArray || 0 == cookieJSONArray.length()) { return false; for (int j = 0; j < cookieJSONArray.length(); j++) { final String visitedURL = cookieJSONArray.optString(j); response.addCookie(c); } else if (needToAppend) { cookieJSONArray.put(request.getRequestURI()); final Cookie c = new Cookie("visited", URLEncoder.encode(cookieJSONArray.toString(), "UTF-8")); c.setMaxAge(COOKIE_EXPIRY); c.setPath("/");
// using http://jettison.codehaus.org/ JSONObject outerObject = new JSONObject(); JSONArray outerArray = new JSONArray(); JSONObject innerObject = new JSONObject(); JSONArray innerArray = new JSONArray(); innerArray.put("shakil"); innerArray.put("29"); innerArray.put("7676"); innerObject.put("id", "2"); innerObject.put("data", innerArray); outerArray.put(innerObject); outerObject.put("rows", outerArray); System.out.println(outerObject.toString());
/** * Put a value in the JSONArray, where the value will be a JSONObject which * is produced from a Map. * * @param value * A Map value. * @return this. */ public JSONArray put(Map<?, ?> value) { this.put(new JSONObject(value)); return this; }
private JSONArray convertListToJsonArray(Object value) throws InvocationTargetException, IllegalAccessException { JSONArray array = new JSONArray(); List<Object> list = (List<Object>) value; for(Object obj : list) { // Send null, if this is an array of arrays we are screwed array.put(obj != null ? getJsonValue(obj, obj.getClass(), null /* field */) : null); } return array; }
try { JSONObject parent = new JSONObject(); JSONObject jsonObject = new JSONObject(); JSONArray jsonArray = new JSONArray(); jsonArray.put("lv1"); jsonArray.put("lv2"); jsonObject.put("mk1", "mv1"); jsonObject.put("mk2", jsonArray); parent.put("k2", jsonObject); Log.d("output", parent.toString(2)); } catch (JSONException e) { e.printStackTrace(); }
/** * Produce a JSONArray containing the names of the elements of this * JSONObject. * * @return A JSONArray containing the key strings, or null if the JSONObject * is empty. */ public JSONArray names() { JSONArray ja = new JSONArray(); Iterator<String> keys = this.keys(); while (keys.hasNext()) { ja.put(keys.next()); } return ja.length() == 0 ? null : ja; }
private JSONArray dumpStructureNodes( AssistStructure.ViewNode node, JSONArray children) throws JSONException { for (int i=0; i<node.getChildCount(); i++) { children.put(dumpStructureNode(node.getChildAt(i), new JSONObject())); } return (children); }
@Override protected void customProcessing(JSONObject message) { JSONObject location = (JSONObject) message.get("location"); final Double longitude = (Double) location.get("lon"); final Double latitude = (Double) location.get("lat"); JSONArray location_point = new JSONArray(); location_point.put(longitude); location_point.put(latitude); message.put("location_point", location_point); message.put("location_mark", location_point); } }