List of usage examples for org.json JSONObject hashCode
@HotSpotIntrinsicCandidate public native int hashCode();
From source file:org.loklak.harvester.TwitterAPI.java
/** * enrich the user data with geocoding information * @param map the user json//w ww . j av a2s . c o m */ public static void enrichLocation(JSONObject map) { // if a location is given, try to reverse geocode to get country name, country code and coordinates String location = map.has("location") ? (String) map.get("location") : null; // in case that no location is given, we try to hack that information out of the context if (location == null || location.length() == 0) { // sometimes the time zone contains city names! Try that String time_zone = map.has("time_zone") && map.get("time_zone") != JSONObject.NULL ? (String) map.get("time_zone") : null; if (time_zone != null && time_zone.length() > 0) { GeoMark loc = DAO.geoNames.analyse(time_zone, null, 5, ""); // check if the time zone was actually a location name if (loc != null && loc.getNames().contains(time_zone)) { // success! It's just a guess, however... location = time_zone; map.put("location", location); //DAO.log("enrichLocation: TRANSLATED time_zone to location '" + location + "'"); } } } // if we finally have a location, then compute country name and geo-coordinates if (location != null && location.length() > 0) { String location_country = map.has("location_country") ? (String) map.get("location_country") : null; String location_country_code = map.has("location_country_code") ? (String) map.get("location_country_code") : null; Object location_point = map.has("location_point") ? map.get("location_point") : null; Object location_mark = map.has("location") ? map.get("location") : null; // maybe we already computed these values before, but they may be incomplete. If they are not complete, we repeat the geocoding if (location_country == null || location_country.length() == 0 || location_country_code == null || location_country_code.length() == 0 || location_point == null || location_mark == null) { // get a salt String created_at = map.has("created_at") ? (String) map.get("created_at") : null; int salt = created_at == null ? map.hashCode() : created_at.hashCode(); // reverse geocode GeoMark loc = DAO.geoNames.analyse(location, null, 5, Integer.toString(salt)); if (loc != null) { String countryCode = loc.getISO3166cc(); if (countryCode != null && countryCode.length() > 0) { String countryName = DAO.geoNames.getCountryName(countryCode); map.put("location_country", countryName); map.put("location_country_code", countryCode); } map.put("location_point", new double[] { loc.lon(), loc.lat() }); //[longitude, latitude] map.put("location_mark", new double[] { loc.mlon(), loc.mlat() }); //[longitude, latitude] //DAO.log("enrichLocation: FOUND location '" + location + "'"); } else { //DAO.log("enrichLocation: UNKNOWN location '" + location + "'"); } } } }
From source file:com.hichinaschool.flashcards.libanki.Models.java
public void moveTemplate(JSONObject m, JSONObject template, int idx) { try {//from www .j a va 2 s .co m JSONArray ja = m.getJSONArray("tmpls"); int oldidx = -1; ArrayList<JSONObject> l = new ArrayList<JSONObject>(); HashMap<Integer, Integer> oldidxs = new HashMap<Integer, Integer>(); for (int i = 0; i < ja.length(); ++i) { if (ja.get(i).equals(template)) { oldidx = i; if (idx == oldidx) { return; } } JSONObject t = ja.getJSONObject(i); oldidxs.put(t.hashCode(), t.getInt("ord")); l.add(t); } l.remove(oldidx); l.add(idx, template); m.put("tmpls", new JSONArray(l)); _updateTemplOrds(m); // generate change map - We use StringBuilder StringBuilder sb = new StringBuilder(); ja = m.getJSONArray("tmpls"); for (int i = 0; i < ja.length(); ++i) { JSONObject t = ja.getJSONObject(i); sb.append("when ord = ").append(oldidxs.get(t.hashCode())).append(" then ").append(t.getInt("ord")); if (i != ja.length() - 1) { sb.append(" "); } } // apply save(m); mCol.getDb().execute( "update cards set ord = (case " + sb.toString() + " end),usn=?,mod=? where nid in (select id from notes where mid = ?)", new Object[] { mCol.usn(), Utils.intNow(), m.getLong("id") }); } catch (JSONException e) { throw new RuntimeException(e); } }