【问题标题】:Get json user from json tweet从 json 推文中获取 json 用户
【发布时间】:2014-12-06 05:39:34
【问题描述】:

我有一条已存储在文件中的推文。我想将用户提取为 json 对象并将其存储在不同的文件中。以下结果为java.lang.IllegalStateException: Apparently jsonStoreEnabled is not set to true.

String jsonStatus; //Content read from a file
Status status = TwitterObjectFactory.createStatus(jsonStatus); //no problem here
String jsonUser = TwitterObjectFactory.getRawJSON(status.getUser()); //Exception

如何将jsonStoreEnabled 设置为true?或者,还有其他方法吗?我不必坚持使用Twitter4j 来创建jsonUser。我尝试了json-simple,但生成的字符串无法用 Twitter4j 解析。

【问题讨论】:

    标签: java twitter4j


    【解决方案1】:

    TwitterObjectFactory 有一个名为registeredAtleastOnce 的变量,如果为false,您将获得java.lang.IllegalStateException: Apparently jsonStoreEnabled is not set to true.。你可以看到这个页面 TwitterObjectFactory 因此,如果您不希望出现该错误,则必须调用一次 Twitter API,例如文件的第一行

    ConfigurationBuilder cb = new ConfigurationBuilder();
                 cb.setDebugEnabled(true)
                 .setOAuthConsumerKey(Ckey)
                 .setOAuthConsumerSecret(CkeySecret)
                 .setOAuthAccessToken(AToken))
                 .setOAuthAccessTokenSecret(AtokenSecret)
                 .setJSONStoreEnabled(true);
    TwitterFactory tf = new TwitterFactory(cb.build());
    Twitter twitter = tf.getInstance();
    String jsonStatus; //Content read from a file
    Status status = TwitterObjectFactory.createStatus(jsonStatus); //your status
    User u2 = t.showUser(status.getUser().getScreenName()); // you use the twitter object once
    String jsonUser = TwitterObjectFactory.getRawJSON(status.getUser()); //now you won't get the error
    System.out.print(jsonUser); //your happy json
    

    这样你就不会得到那个错误

    【讨论】:

    • json 对象是从文件中读取的,而不是从 Twitter 流式传输的。
    • Twitter4j 在 TwitterObjectFactory 中有一个名为“registeredAtleastOnce”的参数,如果为 False,您将得到“显然 jsonStoreEnabled 未设置为 true。”,因此您必须至少使用一次配置生成器
    • 从API获取推文时该参数设置为true,setJSONStoreEnabled设置为true。前一个条件不适用于我的问题。
    【解决方案2】:

    事实证明,org.json 包在这种情况下很有帮助:

    public static String extractUser(String rawJSON) {
        return new org.json.JSONObject(rawJSON).get("user").toString();
    }
    

    【讨论】:

      猜你喜欢
      • 2016-12-03
      • 2018-01-10
      • 2014-09-19
      • 1970-01-01
      • 2022-10-17
      • 2019-10-09
      • 2020-06-12
      • 2014-02-02
      • 1970-01-01
      相关资源
      最近更新 更多