canonical example by Tabnine
public void accessingJson(JSONObject json) { Object invalid = json.get("invalid"); // throws JSONException - "invalid" entry doesn't exists String name = json.getString("name"); // name = "John Brown" int number = json.getInt("name"); // throws JSONException - "name" entry isn't int int age = json.optInt("age", 42); // using default value instead of throwing exception JSONArray pets = json.getJSONArray("pets"); for (int i = 0; i < pets.length(); i++) { String pet = pets.getString(i); } }
JSONObject json = (JSONObject)new JSONParser().parse("{\"name\":\"MyNode\", \"width\":200, \"height\":100}"); System.out.println("name=" + json.get("name")); System.out.println("width=" + json.get("width"));
String jString = "{\"a\": 1, \"b\": \"str\"}"; JSONObject jObj = new JSONObject(jString); Object aObj = jObj.get("a"); if(aObj instanceof Integer){ System.out.println(aObj); }
private static void putImageInBundleWithArrayFormat( Bundle parameters, int index, JSONObject image) throws JSONException{ Iterator<String> keys = image.keys(); while (keys.hasNext()) { String property = keys.next(); String key = String.format(Locale.ROOT, "image[%d][%s]", index, property); parameters.putString(key, image.get(property).toString()); } }
JSONParser parser = new JSONParser(); Object obj = parser.parse(new FileReader("simple.json")); JSONObject jsonObject = (JSONObject) obj; for(Iterator iterator = jsonObject.keySet().iterator(); iterator.hasNext();) { String key = (String) iterator.next(); System.out.println(jsonObject.get(key)); }
Iterator<String> it=obj.keys(); while(it.hasNext()){ String keys=it.next(); JSONObject innerJson=new JSONObject(obj.toString()); JSONArray innerArray=innerJson.getJSONArray(keys); for(int i=0;i<innerArray.length();i++){ JSONObject innInnerObj=innerArray.getJSONObject(i); Iterator<String> InnerIterator=innInnerObj.keys(); while(InnerIterator.hasNext()){ System.out.println("InnInnerObject value is :"+innInnerObj.get(InnerIterator.next())); } }
HashMap<String,String> hashOut = new HashMap<String,String>(); String input = "{\"others\":[ { \"id\":\"1\", \"caption\":\"test\" }, { \"id\":\"2\", \"caption\":\"self\" }, { \"id\":\"2\", \"caption\":\"self\" }, { \"id\":\"1\", \"caption\":\"test\" }],\"quantity\":[ { \"id\":\"1\", \"caption\":\"self1\" }, { \"id\":\"1\", \"caption\":\"self1\" }]}"; JSONObject jsonObj = new JSONObject(input); Iterator it = jsonObj.keys(); while (it.hasNext()) { hashOut.clear(); JSONArray jsInner = (JSONArray) jsonObj.getJSONArray((String) it.next()); for (int i=0;i<jsInner.length();i++) { JSONObject jo = new JSONObject(jsInner.get(i).toString()); hashOut.put((String)jo.get("id"), (String)jo.get("caption")); } System.out.println(hashOut); }
final JSONParser jParser = new JSONParser(); final JSONObject jObject = jParser.getJSONFromUrl(url); Iterator<?> keys = jObject.keys(); while(keys.hasNext()){ final String key = (String)keys.next(); final Object obj = jObject.get(key); if(obj instanceof JSONObject){ final String rate = jObject.getString(key); System.out.println(key + " => " + rate); list.add(rate); } }
public static String checkMessageExistence(JSONObject message) { String source_type = (String) message.get("source_type"); JSONArray location_point = message.getJSONArray("location_point"); Double latitude = (Double) location_point.get(0); Double longitude = (Double) location_point.get(1); String query = "/source_type=" + source_type + " /location=" + latitude + "," + longitude; // search only latest message DAO.SearchLocalMessages search = new DAO.SearchLocalMessages(query, Order.CREATED_AT, 0, 1, 0); Iterator<TwitterTweet> it = search.timeline.iterator(); while (it.hasNext()) { TwitterTweet messageEntry = it.next(); if (compareMessage(messageEntry.toJSON(), message)) { return messageEntry.getPostId(); } } return null; }
JSONObject expanded = new JSONObject(); while (!toTraverse.isEmpty()) { String current = toTraverse.remove(); JSONObject json = load(current); JSONObject conf = json.getJSONObject(CONF_KEY); Iterator<String> iter = conf.keys(); while (iter.hasNext()) { String key = iter.next(); expanded.put(key, conf.get(key)); JSONArray includes = json.getJSONArray(INCLUDES_KEY); for (int idx = 0; idx < includes.length(); idx++) { String include = resolve(current, includes.getString(idx)); if (traversedFiles.contains(include)) {
JSONArray a = (JSONArray) parser.parse(new FileReader("c:\\exer4-courses.json")); for (Object o : a) { JSONObject person = (JSONObject) o; String name = (String) person.get("name"); System.out.println(name); String city = (String) person.get("city"); System.out.println(city); String job = (String) person.get("job"); System.out.println(job); JSONArray cars = (JSONArray) jsonObject.get("cars"); for (Object c : cars) { System.out.println(c+""); } }
JSONObject obj = (JSONObject) new JSONParser().parse(data); System.out.println(obj); JSONObject userList = (JSONObject) obj.get("userList"); JSONArray user = (JSONArray) userList.get("user"); JSONObject userObj = (JSONObject) user.get(0); String customerId = (String) userObj.get("customerId"); System.out.println("Check Data " + customerId);
JSONArray conditionMap = opObject.getJSONArray("condition map:"); for (int index = 0; index < conditionMap.length(); index++) { JSONObject cond = conditionMap.getJSONObject(index); String k = (String) cond.keys().next(); JSONObject condObject = new JSONObject((String)cond.get(k)); String type = condObject.getString("type"); String left = condObject.getString("left"); String right = condObject.getString("right"); if (keys.length() != 0) { sb.append(posToOpId.get(left) + "." + keys.get(left) + "=" + posToOpId.get(right) + "." + keys.get(right) + "(" + type + "),"); } else { JSONArray conditionMap = opObject.getJSONArray("condition map:"); for (int index = 0; index < conditionMap.length(); index++) { JSONObject cond = conditionMap.getJSONObject(index); String k = (String) cond.keys().next(); JSONObject condObject = new JSONObject((String)cond.get(k)); String type = condObject.getString("type"); String left = condObject.getString("left"); String right = condObject.getString("right"); if (keys.length() != 0) { sb.append(posToOpId.get(left) + "." + keys.get(left) + "=" + posToOpId.get(right) + "." + keys.get(right) + "(" + type + "),"); } else {
if (this.selectionMapping != null && this.selectionMapping.size() == 1) { final String aggregator = this.selectionMapping.keySet().iterator().next(); final String aggregator_as = this.selectionMapping.get(aggregator); if (aggregator.startsWith("COUNT(") && aggregator.endsWith(")")) { // TODO: there should be a special pattern for this to make it more efficient return a.put(new JSONObject().put(aggregator_as, choices.length())); return a.put(new JSONObject().put(aggregator_as, max.get())); return a.put(new JSONObject().put(aggregator_as, min.get())); final AtomicDouble sum = new AtomicDouble(0.0d); String c = aggregator.substring(4, aggregator.length() - 1); choices.forEach(json -> sum.addAndGet(((JSONObject) json).getDouble(c))); return a.put(new JSONObject().put(aggregator_as, sum.get() / choices.length())); String aggregator = ci.next(); String column = ci.next(); if (column.indexOf('(') >= 0) {String s = aggregator; aggregator = column; column = s;} final String aggregator_as = this.selectionMapping.get(aggregator); choices.forEach(json -> a.put(new JSONObject() .put(aggregator_as, 100.0d * ((JSONObject) json).getDouble(c) / sum.get()) .put(column_as, ((JSONObject) json).get(column_final)))); return a;
JSONObject json = jsonOutput ? new JSONObject(new LinkedHashMap<>()) : null; if (this.work != null && (this.work.isUserLevelExplain() || this.work.isFormatted())) { if (jsonOut != null && jsonOut.length() > 0) { ((JSONObject) jsonOut.get(JSONObject.getNames(jsonOut)[0])).put("OperatorId:", operator.getOperatorId()); ((JSONObject) jsonOut.get(JSONObject.getNames(jsonOut)[0])).put("outputname:", ((ReduceSinkDesc) operator.getConf()).getOutputName()); JSONObject jsonOut = outputPlan(op, out, extended, jsonOutput, cindent, "", inTest); if (jsonOutput) { ((JSONObject)json.get(JSONObject.getNames(json)[0])).accumulate("children", jsonOut); out.print(" "); out.println(val); } else { for(String k: JSONObject.getNames(jsonOut)) { json.put(k, jsonOut.get(k)); JSONObject ret = new JSONObject(new LinkedHashMap<>()); ret.put(keyJSONObject, json); return ret;
JSONObject jsonObject = new JSONObject(jsonString); JSONObject myResponse = jsonObject.getJSONObject("MyResponse"); JSONArray tsmresponse = (JSONArray) myResponse.get("listTsm"); for(int i=0; i<tsmresponse.length(); i++){ list.add(tsmresponse.getJSONObject(i).getString("name")); System.out.println(list);
Object value = source.get(key); if (!target.has(key)) { JSONObject a = new JSONObject("{offer: {issue1: value1}, accept: true}"); JSONObject b = new JSONObject("{offer: {issue2: value2}, reject: false}"); System.out.println(a+ " + " + b+" = "+JsonUtils.deepMerge(a,b));
private User jsonToUser(JSONObject user) throws JSONException { Uri picture = Uri.parse(user.getJSONObject("picture").getJSONObject("data").getString ("url")); String name = user.getString("name"); String id = user.getString("id"); String email = null; if (user.has("email")) { email = user.getString("email"); } // Build permissions display string StringBuilder builder = new StringBuilder(); JSONArray perms = user.getJSONObject("permissions").getJSONArray("data"); builder.append("Permissions:\n"); for (int i = 0; i < perms.length(); i++) { builder.append(perms.getJSONObject(i).get("permission")).append(": ").append(perms .getJSONObject(i).get("status")).append("\n"); } String permissions = builder.toString(); return new User(picture, name, id, email, permissions); }
@Override protected void customProcessing(JSONObject message) { JSONObject location = (JSONObject) message.get("position"); final Double longitude = Double.parseDouble((String) location.get("long")); final Double latitude = Double.parseDouble((String) location.get("lat")); List<Double> location_point = new ArrayList<>(); location_point.add(longitude); location_point.add(latitude); message.put("location_point", location_point); message.put("location_mark", location_point); JSONObject user = new JSONObject(true); user.put("screen_name", "freifunk_" + message.get("name")); user.put("name", message.get("name")); message.put("user", user); } }