如何从JSONObject获取文件或流?



如何将Twitter4JJSONObject转换为IOFile以与BaseX中的JsonParser一起使用?

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

JsonParser看起来可以处理File罚款:

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作品:

org.basex.io
Class IO
java.lang.Object
org.basex.io.IO 
Direct Known Subclasses:
IOContent, IOFile, IOStream, IOUrl 

如何从这里的JSONObject获得File

使用Twitter4J的代码段:

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;
}

Twitter4J写入文件JSONObject似乎有效,至少文件在手动打开时看起来正确:

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,或者至少实例化一个解析器,如:

private void parseJsonFile(String fileName) throws IOException {
JsonParser jsonParser = new JsonParser(new IOFile(fileName), new MainOptions());
}

GitHub 上的完整代码。

这绝不是一个完整的解决方案。 事实上,将JSON写入文件似乎很笨拙 - 但它确实存在。

似乎是将JSON数据从Twitter4J移动到BaseX的唯一方法,或者至少是最务实的。 其他解决方案值得赞赏。

最新更新