首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何从JSONObject获取文件或流?

如何从JSONObject获取文件或流?
EN

Stack Overflow用户
提问于 2020-02-01 16:32:28
回答 1查看 103关注 0票数 1

如何将来自Twitter4JJSONObject转换为IOFile以与来自BaseXJsonParser一起使用

因此,希望使用Twitter4J来获取文件或流。

JsonParser希望work with a File fine:

代码语言:javascript
复制
JsonParser

public JsonParser(IO source,
          MainOptions opts,
          JsonParserOptions jopts)
           throws java.io.IOException

Constructor.

Parameters:
    source - document source
    opts - database options
    jopts - parser options
Throws:
    java.io.IOException - I/O exception

尽管其他IO可以工作:

代码语言:javascript
复制
org.basex.io
Class IO

    java.lang.Object
        org.basex.io.IO 

    Direct Known Subclasses:
        IOContent, IOFile, IOStream, IOUrl 

在这里,如何从JSONObject获取File

使用Twitter4JSnippet

代码语言:javascript
复制
private JSONObject jsonOps(Status status) throws JSONException, BaseXException {
    String string = TwitterObjectFactory.getRawJSON(status);
    JSONObject json = new JSONObject(string);
    String language = json.getString("lang");
    log.fine(language);
    return json;
}
EN

回答 1

Stack Overflow用户

发布于 2020-02-01 18:41:27

Twitter4J向文件文件写入JSONObject似乎是可行的,至少文件在手动打开时看起来是正确的:

代码语言:javascript
复制
public void writeJsonToFile(String fileName) throws FileNotFoundException, UnsupportedEncodingException, IOException {
    FileOutputStream fileOutputStream = null;
    OutputStreamWriter outputStreamWriter = null;
    BufferedWriter bufferedWriter = null;
    fileOutputStream = new FileOutputStream(fileName);
    outputStreamWriter = new OutputStreamWriter(fileOutputStream, "UTF-8");
    bufferedWriter = new BufferedWriter(outputStreamWriter);
    bufferedWriter.write(tweets.toString());
    bufferedWriter.close();
    outputStreamWriter.close();
    fileOutputStream.close();
}

一旦编写好文件,就可以很容易地“解析”JSON,或者至少实例化一个解析器,如下所示:

代码语言:javascript
复制
private void parseJsonFile(String fileName) throws IOException {
    JsonParser jsonParser = new JsonParser(new IOFile(fileName), new MainOptions());
}

github上的完整代码。

这绝不是一个完整的解决方案。实际上,将JSON写入文件似乎是一件很麻烦的事情--但事实就是如此。

JSON数据从Twitter4J迁移到BaseX似乎是唯一的方法,或者至少是最实用的方法。其他解决方案很受欢迎。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/60015248

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档