在Processing中访问嵌套的JSON数组

时间:2014-05-05 18:00:16

标签: javascript json twitter processing

我正在使用Processing来解析我从Twitter的Streaming API下载的数据,我对如何将某些内容从Javascript转换为Processing进行了解答。

基本上,我从Twitter获得的信息大致如下:

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

使用我当前的代码:

JSONObject tweets = values.getJSONObject(i);

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

我可以使用tweets.getString("x");访问第一层括号内的任何内容(请原谅我的非技术术语,我是新手),但我如何进入"用户"阵列

根据this answer,javascript解决方案看起来像这样:

  

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

但我不确定如何将其转换为Processing知道如何解释的代码。我试过这个:

  

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

但那不起作用。有人有建议吗?

1 个答案:

答案 0 :(得分:0)

我自己想通了!谁会想到。如果其他人在这里遇到类似的问题,我最终会做的就是:

我没有意识到这一点,但"用户"在这种情况下是另一个JSONObject。就像你必须定义JSONObject" tweets"你还必须定义一个JSONObject" user",来自 " tweets"。你这样做是这样的:

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

从那里,您只需使用user.getString("x");

即可访问用户内的任何内容