Pregunta

I'm using Processing to parse data I downloaded from Twitter's Streaming API, and I have a question about how to translate something from Javascript into Processing.

Basically, the information I got from Twitter roughly looks like this:

[
 {"created-at": "May 5th, xx:xx",
  "text": "This is the content of the tweet",
  "user":{
          "name": "John Doe",
          "friends_count": 100,
         }
 }
]

With my current code:

JSONObject tweets = values.getJSONObject(i);

String time = tweets.getString("created-at");
String tweet_text = tweets.getString("text");

I can use the tweets.getString("x"); to access anything within the first layer of brackets (pardon my non-technical term, I'm new to this), but how do I get into the "user" array?

According to this answer, the javascript solution would look something like this:

var author_name = tweets.user[0].name;

But I'm not sure how to translate that to code that Processing knows how to interpret. I tried this:

String author_name = tweets.user[0].getString("name");

But that's not working. Does anybody have suggestions?

¿Fue útil?

Solución

I figured it out by myself! Who would have guessed. If somebody else stumbles onto this with a similar question here's what I ended up doing:

I didn't realize this, but "user" in this case is another JSONObject. Just as you have to define the JSONObject "tweets" you also have to define a JSONObject "user", that comes from within "tweets". You do this like so:

JSONObject user = tweets.getJSONObject("user"); 

From there you can get access to anything within user by simply using user.getString("x");

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top