Example usage for twitter4j JSONObject getJSONObject

List of usage examples for twitter4j JSONObject getJSONObject

Introduction

In this page you can find the example usage for twitter4j JSONObject getJSONObject.

Prototype

public JSONObject getJSONObject(String name) throws JSONException 

Source Link

Document

Returns the value mapped by name if it exists and is a JSONObject , or throws otherwise.

Usage

From source file:com.isdp.twitterposter.GoogleManager.java

License:Open Source License

public String[] parseYelp(JSONObject jsonObj) {
    try {//from w  ww. j  ava 2s. c  o m
        ArrayList<String> results = new ArrayList<String>();

        JSONArray itemsArray = jsonObj.getJSONArray("items");

        for (int i = 0; i < itemsArray.length(); ++i) {
            try {
                JSONObject item = itemsArray.getJSONObject(i);
                JSONObject localbusiness = item.getJSONObject("pagemap").getJSONArray("localbusiness")
                        .getJSONObject(0);
                JSONObject postaladdress = item.getJSONObject("pagemap").getJSONArray("postaladdress")
                        .getJSONObject(0);
                JSONObject aggregaterating = item.getJSONObject("pagemap").getJSONArray("aggregaterating")
                        .getJSONObject(0);

                String textString = Util.generateRandomString(5) + "\n";
                textString += Util.truncateString(localbusiness.getString("name"), 30) + "\n";
                textString += localbusiness.getString("telephone") + "\n";
                textString += postaladdress.getString("streetaddress") + "\n";
                textString += postaladdress.getString("addresslocality") + " "
                        + postaladdress.getString("addressregion") + "\n";
                textString += postaladdress.getString("postalcode") + "\n";
                textString += "R|" + aggregaterating.getString("ratingvalue") + "\n";
                textString += "P|" + localbusiness.getString("pricerange") + "\n";

                results.add(textString);
            } catch (Exception e) {
            }
        }

        String[] resultsArray = new String[results.size()];
        return results.toArray(resultsArray);
    } catch (Exception e) {
        e.printStackTrace();
        return null;
    }
}

From source file:com.sampleapp.db.DBUtil.java

License:Open Source License

private DBUtil() {
    Map<String, String> env;
    env = System.getenv();//  w w w. j a  va2 s  .  c  om
    String vcap = env.get("VCAP_SERVICES");

    if (vcap == null) {
        System.out.println("No VCAP_SERVICES found");
        return;
    }

    System.out.println("VCAP_SERVICES found");

    try {
        JSONObject vcap_services = new JSONObject(vcap);

        @SuppressWarnings("rawtypes")
        Iterator iter = vcap_services.keys();
        JSONArray cloudant = null;

        // find instance of cloudant bound to app
        while (iter.hasNext()) {
            String key = (String) iter.next();
            if (key.startsWith("cloudantNoSQLDB")) {
                cloudant = vcap_services.getJSONArray(key);
            }
        }

        JSONObject instance = cloudant.getJSONObject(0); // Grab the first instance of mongoDB for this app (there is only one)
        JSONObject credentials = instance.getJSONObject("credentials");

        // Get all VCAP_SERVICES credentials
        String host = credentials.getString("host");
        String port = credentials.getString("port");
        String username = credentials.getString("username");
        String password = credentials.getString("password");
        String url = credentials.getString("url");

        System.out.println("Found all the params: " + username + " " + password + " " + url);

        // Create the client connection the the database and then create the database
        client = new CloudantClient(url, username, password);
        db = client.database(dbname, true);

        System.out.println("Connected to cloudant on " + host + ":" + port);
    } catch (Exception e) {
        e.printStackTrace();
    }
}

From source file:EjemploObservador.BuscadorDeTweets.java

License:Apache License

@Override
public void run() {
    IniciaVariablesDeConeccion();/*www .  ja  v a2  s .c o m*/
    client.connect();
    while (true) {
        try {
            /// Toma el JSON devuelto por el API de tweeter 
            /// Y extrae los datos en el TweetText

            String msg = queue.take();
            JSONObject Tweet = new JSONObject(msg);
            String Text = Tweet.getString("text");
            String User = Tweet.getJSONObject("user").getString("name");
            String location = Tweet.getJSONObject("user").getString("location");
            String TweetText = ">>>>>>>> " + location + "\n" + User + ": " + Text + "\n";

            /// Envia el TweetText al la Lista
            ListaDeTweets.agregaTweet(TweetText);

        } catch (InterruptedException ex) {
            Logger.getLogger(BuscadorDeTweets.class.getName()).log(Level.SEVERE, null, ex);

        } catch (JSONException ex) {
            Logger.getLogger(BuscadorDeTweets.class.getName()).log(Level.SEVERE, null, ex);
        }

    }
}

From source file:org.csi.yucca.storage.datamanagementapi.model.metadata.ckan.MetadataCkanFactory.java

protected static String formatMessages(Locale locale, String element) {

    JSONObject messages = loadMessages(locale, element);

    StringBuffer sb = new StringBuffer("");
    String loc = locale.getLanguage().substring(0, 1).toUpperCase() + locale.getLanguage().substring(1);

    String label1 = (element.equals("tags") ? "streamTags" : "streamDomains");
    String label2 = (element.equals("tags") ? "tagCode" : "codDomain");

    try {/* w ww  .ja v  a  2  s .  co  m*/
        JSONObject streamTags = messages.getJSONObject(label1);
        JSONArray elements = streamTags.getJSONArray("element");
        for (int i = 0; i < elements.length(); i++) {
            String tagCode = elements.getJSONObject(i).getString(label2);
            String langEl = elements.getJSONObject(i).getString("lang" + loc);
            sb.append(tagCode + " = " + langEl + "\n");
        }

    } catch (JSONException ex) {
        // TODO Auto-generated catch block
        ex.printStackTrace();
    }

    return sb.toString();
}

From source file:org.csi.yucca.storage.datamanagementapi.service.StoreService.java

protected static String formatMessages(Locale locale, String element) {

    JSONObject messages = loadMessages(locale, element);

    log.debug("[StoreService::formatMessages] - START");
    StringBuffer sb = new StringBuffer("");
    String loc = locale.getLanguage().substring(0, 1).toUpperCase() + locale.getLanguage().substring(1);

    String label1 = (element.equals("tags") ? "streamTags" : "streamDomains");
    String label2 = (element.equals("tags") ? "tagCode" : "codDomain");

    try {/*from   w  w  w.  ja v  a2 s .  com*/
        JSONObject streamTags = messages.getJSONObject(label1);
        JSONArray elements = streamTags.getJSONArray("element");
        for (int i = 0; i < elements.length(); i++) {
            String tagCode = elements.getJSONObject(i).getString(label2);
            String langEl = elements.getJSONObject(i).getString("lang" + loc);
            sb.append(tagCode + " = " + langEl + "\n");
        }

    } catch (JSONException ex) {
        // TODO Auto-generated catch block
        ex.printStackTrace();
    } finally {
        log.debug("[StoreService::formatMessages] - END");
    }
    return sb.toString();
}

From source file:twitterapp.TweetsProcessing.java

public static void createSeparateEntities() throws JSONException {

    String[] collectionsTweets = { "myTweetCol", "myTweetCol2", "myTweetCol3", "myTweetCol4" };
    String[] colEntities = { "separateEntities", "separateEntities2", "separateEntities3",
            "separateEntities4" };
    for (int col = 0; col < collectionsTweets.length; col++) {
        MongoClient mongo = new MongoClient("localhost", 27017);
        MongoDatabase database = mongo.getDatabase("myTweetdb");
        MongoCollection<Document> collection = database.getCollection(collectionsTweets[col]);
        Iterator<Document> kati = collection.find().iterator();

        while (kati.hasNext()) {

            Document doc = kati.next();

            String user, url, hashtag, mentioned, id, timestamp;
            user = url = hashtag = mentioned = id = timestamp = "";

            JSONObject a = new JSONObject(doc);
            String temp = a.getString("user");
            String tokens[] = temp.split(",");
            for (int j = 0; j < tokens.length; j++) {
                if (tokens[j].contains("screen_name")) {

                    temp = tokens[j].replace("\"screen_name\":", "");
                    user = temp.replace("\"", "");

                }//from w ww  . ja  v a2  s .co m

            }
            timestamp = String.valueOf(a.getLong("timestamp_ms"));
            JSONObject b = a.getJSONObject("entities");
            tokens = b.toString().split(",");
            for (int j = 0; j < tokens.length; j++) {
                if (tokens[j].contains("text")) {
                    String temp2 = tokens[j].replace("\"", "");
                    temp2 = temp2.replace(":", "");
                    temp2 = temp2.replace("}", "");
                    temp2 = temp2.replace("]", "");
                    temp2 = temp2.replace("text", "");
                    hashtag = hashtag.concat(temp2 + " ").trim();

                }
                if (tokens[j].contains("expanded_url")) {
                    String temp2 = tokens[j].replace("\":\"", "");
                    temp2 = temp2.replace("\"", "");
                    temp2 = temp2.replace("expanded_url", "");
                    url = url.concat(temp2 + " ");
                }
                if (tokens[j].contains("screen_name")) {
                    String temp2 = tokens[j].replace(":", "");
                    temp2 = temp2.replace("\"", "");
                    temp2 = temp2.replace("screen_name", "");
                    mentioned = mentioned.concat(temp2 + " ");
                }

            }

            if (a.toString().contains("retweeted_status")) {
                b = (JSONObject) a.getJSONObject("retweeted_status");
                id = b.getString("id_str");

            }

            Document object = new Document("user", user).append("timestamp", timestamp).append("hashtag",
                    hashtag);
            Document object1 = new Document("user", user).append("timestamp", timestamp).append("url", url);
            Document object2 = new Document("user", user).append("timestamp", timestamp)
                    .append("mentioned_users", mentioned);
            Document object3 = new Document("user", user).append("timestamp", timestamp)
                    .append("retweeted_tweet", id);

            MongoCollection<Document> collection2 = database.getCollection(colEntities[col]);

            collection2.insertOne(object);
            collection2.insertOne(object1);
            collection2.insertOne(object2);
            collection2.insertOne(object3);

        }
    }

}