List of usage examples for org.json.simple JSONObject size
int size();
From source file:Rpackage.MongoFixPostCode.java
public static void main(String[] args) { YelpAPI yelpApi = new YelpAPI(); JSONParser parser = new JSONParser(); try {/*ww w .j av a2 s .c om*/ // Connect to mongodb MongoClient mongo = new MongoClient("10.33.2.142", 27017); // get database // if database doesn't exists, mongodb will create it for you DB db = mongo.getDB("test"); // get collection // if collection doesn't exists, mongodb will create it for you DBCollection collection = db.getCollection("twitter_Stream"); DBCursor cursor; BasicDBObject query; //------------------------------------ // ( 1 ) collection.find() --> get all document DBObject clause1 = new BasicDBObject("Latitude", new BasicDBObject("$exists", true)); DBObject clause2 = new BasicDBObject("tweet_Country_Code", new BasicDBObject("$exists", true)); DBObject clause3 = new BasicDBObject("R_Post_Code", new BasicDBObject("$exists", false)); /*{ "$and":[ { "tweet_Country_Code":{ "$exists":true } }, { "Latitude":{ "$exists":false } }, { "tweet_Country_Code":"AU" } ] } */ BasicDBList or = new BasicDBList(); or.add(clause1); or.add(clause2); or.add(clause3); query = new BasicDBObject("$and", or); //{ "$and":[ { "Latitude":{ "$exists":true } }, { "R_Post_Code":{ "$exists":false } } ]} cursor = collection.find(query).addOption(Bytes.QUERYOPTION_NOTIMEOUT) // .addOption(Bytes.QUERYOPTION_AWAITDATA); System.out.println("( 1 ) .get the user id within latitide"); System.out.println("results --> " + cursor.count()); FoursquareAPI_backup qui4squreApi = new FoursquareAPI_backup(); try { while (cursor.hasNext()) { DBObject data = cursor.next(); String v_user_Name = (String) data.get("user_name"); Long v_twitte_id = (Long) data.get("tweet_ID"); String v_twitte_text = (String) data.get("tweet_text"); Long v_user_Id = (Long) data.get("user_Id"); Double v_Latitude = (Double) data.get("Latitude"); Double v_Longtitude = (Double) data.get("Longitude"); String v_tweet_Place_Name = (String) data.get("tweet_Place_Name"); String v_tweet_Country_Code = (String) data.get("tweet_Country_Code"); if (v_user_Id == null) { /* System.out.print("update:" +v_user_Name+"/status/"+ v_twitte_id); try{ Status status = twitter.showStatus(v_twitte_id); BasicDBObject jo = GetMongoRecord(status); System.out.println( "-->" + status.getId() + " : " + jo.getString("Re_user_screenName") + ":" + jo.getString("tweet_text")); collection.update(new BasicDBObject("tweet_ID", v_twitte_id), jo); // set the document in the DB to the new document for Jo }catch (TwitterException ex){ if (ex.getStatusCode()==144) continue; } */ continue; } JSONObject businesses = yelpApi.search4Yelp("city", v_Latitude, v_Longtitude);//-27.497835,153.017472); boolean searchAgain = false; if (businesses == null) searchAgain = true; else if (businesses.size() < 1) searchAgain = true; if (searchAgain) { System.out.println("La:" + v_Latitude + "\tLo:" + v_Longtitude); businesses = qui4squreApi.search4Square("city", v_Latitude, v_Longtitude); searchAgain = false; } if (businesses == null) searchAgain = true; else if (businesses.size() < 1) searchAgain = true; if (searchAgain) { businesses = qui4squreApi.searchGoogleMap("city", v_Latitude, v_Longtitude); if (businesses == null) { System.out.println("\t" + v_tweet_Country_Code + "\t:" + v_tweet_Place_Name); continue; } else if (businesses.size() < 1) { System.out.println("\t" + v_tweet_Country_Code + "\t:" + v_tweet_Place_Name); continue; } } String country_code = (String) businesses.get("country_code"); String city_code = (String) businesses.get("city_name"); String state_code = (String) businesses.get("state_code"); String post_code = (String) businesses.get("post_code"); BasicDBObject setNewFieldQuery = new BasicDBObject().append("$set", new BasicDBObject().append("R_Country_Code", country_code) .append("R_State_Code", state_code).append("R_City", city_code) .append("R_Post_Code", post_code)); collection.update(new BasicDBObject().append("tweet_ID", v_twitte_id), setNewFieldQuery); // set the document in the DB to the new document for Jo setNewFieldQuery.clear(); setNewFieldQuery = null; } } finally { cursor.close(); } System.out.println("---------------------------------"); } catch (UnknownHostException e) { e.printStackTrace(); } catch (MongoException e) { e.printStackTrace(); } }
From source file:util.FoursquareAPI.java
public JSONObject getPostCodeByGeo(Double v_Latitude, Double v_Longtitude) { boolean searchAgain = false; JSONObject businesses = null; if (v_Latitude == null) { return null; }//from w w w .j a v a 2 s.com // businesses = yelpApi.search4Yelp("city", v_Latitude, v_Longtitude);//-27.497835,153.017472); if (businesses == null) { searchAgain = true; } else if (businesses.size() < 1) { searchAgain = true; } if (searchAgain) { businesses = search4Square("city", v_Latitude, v_Longtitude); searchAgain = false; } if (businesses == null) { searchAgain = true; } else if (businesses.size() < 1) { searchAgain = true; } if (searchAgain) { searchAgain = false; System.out.println("La:" + v_Latitude + "\tLo:" + v_Longtitude); businesses = searchGoogleMap("city", v_Latitude, v_Longtitude); if (businesses == null) { // System.out.println("\t" + v_tweet_Country_Code + "\t:" + v_tweet_Place_Name); searchAgain = true; } else if (businesses.size() < 1) { //System.out.println("\t" + v_tweet_Country_Code + "\t:" + v_tweet_Place_Name); searchAgain = true; } } return businesses; }