canonical example by Tabnine
/** * Construct a UrlDevice. * @param id The id of the device. * @param url The URL broadcasted by the device. * @param extraData Extra data to associate with this UrlDevice. */ private UrlDevice(String id, String url, JSONObject extraData) { mId = id; mUrl = url; mExtraData = extraData == null ? new JSONObject() : new JSONObject(extraData.toString()); }
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);
public boolean isJSONValid(String test) { try { new JSONObject(test); } catch (JSONException ex) { // edited, to include @Arthur's comment // e.g. in case JSONArray is valid as well... try { new JSONArray(test); } catch (JSONException ex1) { return false; } } return true; }
List<Photo> parse(String response) throws JSONException { JSONObject searchResults = new JSONObject(response.substring(FLICKR_API_PREFIX_LENGTH, response.length() - 1)); JSONArray photos = searchResults.getJSONObject("photos").getJSONArray("photo"); List<Photo> results = new ArrayList<>(photos.length()); for (int i = 0, size = photos.length(); i < size; i++) { results.add(new Photo(photos.getJSONObject(i))); } return results; } }
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());
JSONObject result = new JSONObject(); result.put("photos", new JSONArray()); JSONArray jsonGalleries = json.getJSONArray("galleries"); for (int i = 0; i < jsonGalleries.length(); i++) { if (i > 0) { sleep(500); JSONObject jsonGallery = jsonGalleries.getJSONObject(i); long galleryID = jsonGallery.getLong("id"); String userID = Long.toString(jsonGallery.getLong("user_id")); sendUpdate(STATUS.LOADING_RESOURCE, "Gallery ID " + galleryID + " for userID " + userID); JSONObject thisJSON = Http.url(blogURL).getJSON(); JSONArray thisPhotos = thisJSON.getJSONArray("photos"); for (int j = 0; j < thisPhotos.length(); j++) { result.getJSONArray("photos").put(thisPhotos.getJSONObject(j)); JSONObject result = new JSONObject(); result.put("photos", new JSONArray()); for (int j = 0; j < thisPhotos.length(); j++) { result.getJSONArray("photos").put(thisPhotos.getJSONObject(j));
JSONObject obj = new JSONObject("{\"name\": \"John\"}"); System.out.println(obj.getString("name")); //John
private static JSONObject handleGeneric(JSONArray links, String service, String noVideoMessage) { JSONObject ret = new JSONObject(true); ret.put("service", service); if (links.length() > 0) { ret.put("status", 0); ret.put("message", "OK"); ret.put("links", links); } else { ret.put("status", 2); ret.put("message", noVideoMessage); } return ret; }
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 synchronized static JSONObject parseAppGateKeepersFromJSON( final String applicationId, JSONObject gateKeepersJSON) { JSONObject result; if (fetchedAppGateKeepers.containsKey(applicationId)) { result = fetchedAppGateKeepers.get(applicationId); } else { result = new JSONObject(); } JSONArray arr = gateKeepersJSON.optJSONArray(APPLICATION_GRAPH_DATA); JSONObject gateKeepers = null; if (arr != null) { gateKeepers = arr.optJSONObject(0); } // If there does exist a valid JSON object in arr, initialize result with this JSON object if (gateKeepers != null && gateKeepers.optJSONArray(APPLICATION_GATEKEEPER_FIELD) != null) { JSONArray data = gateKeepers.optJSONArray(APPLICATION_GATEKEEPER_FIELD); for (int i = 0; i < data.length(); i++) { try { JSONObject gk = data.getJSONObject(i); result.put(gk.getString("key"), gk.getBoolean("value")); } catch (JSONException je) { Utility.logd(Utility.LOG_TAG, je); } } } fetchedAppGateKeepers.put(applicationId, result); return result; }
public static void main(String[] args) { // convert Java to json JSONObject root = new JSONObject(); root.put("message", "Hi"); JSONObject place = new JSONObject(); place.put("name", "World!"); root.put("place", place); String json = root.toString(); System.out.println(json); System.out.println(); // convert json to Java JSONObject jsonObject = new JSONObject(json); String message = jsonObject.getString("message"); String name = jsonObject.getJSONObject("place").getString("name"); System.out.println(message + " " + name); } }
/** * If during thinking we observe something that we want to memorize, we can memorize this here * @param featureName the object key * @param observation the object value * @return the thought */ public SusiThought addObservation(String featureName, String observation) { JSONArray data = getData(); for (int i = 0; i < data.length(); i++) { JSONObject spark = data.getJSONObject(i); if (!spark.has(featureName)) { spark.put(featureName, observation); return this; } } data.put(new JSONObject().put(featureName, observation)); return this; }
/** Copy, save and return. */ public JSONObject copy(JSONObject m) { JSONObject m2 = null; try { m2 = new JSONObject(Utils.jsonToString(m)); m2.put("name", m2.getString("name") + " copy"); } catch (JSONException e) { throw new RuntimeException(e); } add(m2); return m2; }
private JSONObject parseJSONObject(String jsonStr) { if (jsonStr.startsWith("{")) { return new JSONObject(jsonStr); } else if (jsonStr.startsWith("[")) { JSONArray jsonArray = new JSONArray(jsonStr); if (jsonArray.length() > 0 && jsonArray.get(0) instanceof JSONObject) { return getJsonObject(jsonArray); } } return null; }
@Test public void testObjectToPrimitive() throws JSONException { ArrayOfPrimitivesContainer container = new ArrayOfPrimitivesContainer(); ArrayList<Object> primitives = container.primitives; primitives.add(Long.MIN_VALUE); primitives.add(Long.MAX_VALUE); primitives.add(Integer.MIN_VALUE); primitives.add(Integer.MAX_VALUE); primitives.add(Float.MIN_VALUE); primitives.add(Float.MAX_VALUE); primitives.add(Double.MIN_VALUE); primitives.add(Double.MAX_VALUE); String json = mObjectMapper.convertValue(container, JSONObject.class).toString(); JSONObject obj = new JSONObject(json); JSONArray array = obj.getJSONArray("primitives"); ArrayList<Object> actual = new ArrayList<>(); for (int i = 0, N = array.length(); i < N; i++) { actual.add(array.get(i)); } assertEquals(primitives.toString(), actual.toString()); }
/** * Merging of data is required during an mind-meld. * To meld two thoughts, we combine their data arrays into one. * The resulting table has the maximum length of the source tables * @param table the information to be melted into our existing table. * @return the thought */ public SusiThought mergeData(JSONArray table1) { JSONArray table0 = this.getData(); while (table0.length() < table1.length()) table0.put(new JSONObject()); for (int i = 0; i < table1.length(); i++) { table0.getJSONObject(i).putAll(table1.getJSONObject(i)); } setData(table0); return this; }
@NonNull private static List<ObservedUri> convertJsonToObservedUris(@NonNull String contentUrisJson) { List<ObservedUri> uris = new ArrayList<>(); try { JSONObject json = new JSONObject(contentUrisJson); JSONArray jsonFlags = json.getJSONArray(JSON_URI_FLAGS); JSONArray jsonUris = json.getJSONArray(JSON_URIS); int length = jsonFlags.length(); for (int i = 0; i < length; i++) { int flags = jsonFlags.getInt(i); String uri = jsonUris.getString(i); uris.add(new ObservedUri(Uri.parse(uri), flags)); } } catch (JSONException e) { throw new RuntimeException(e); } return uris; } }
public void actionPerformed(ActionEvent e) { String json = editTP.getText(); json = json.trim(); if (json.startsWith("{")) { JSONObject jsonObject = new JSONObject(json); String formatJson = jsonObject.toString(4); editTP.setText(formatJson); } else if (json.startsWith("[")) { JSONArray jsonArray = new JSONArray(json); String formatJson = jsonArray.toString(4); editTP.setText(formatJson); } } });
JSONObject json = new JSONObject(); JSONObject manJson = new JSONObject(); manJson.put("name", "emil"); manJson.put("username", "emil111"); manJson.put("age", "111"); json.put("man",manJson);