Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/visual-studio-2012/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何从JSONObject获取文件或流?_Java_Json_Xml_Twitter4j_Basex - Fatal编程技术网

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

Java 如何从JSONObject获取文件或流?,java,json,xml,twitter4j,basex,Java,Json,Xml,Twitter4j,Basex,如何将来自Twitter4J的JSONObject转换为IOFile,以便与来自BaseX的JsonParser一起使用 因此,希望使用Twitter4J获取文件或流 JsonParser查找文件很好: JsonParser public JsonParser(IO source, MainOptions opts, JsonParserOptions jopts) throws java.io.IOException Const

如何将来自Twitter4J的
JSONObject
转换为
IOFile
,以便与来自
BaseX的
JsonParser
一起使用

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

JsonParser
查找
文件
很好:

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
获取
文件

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

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

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());
}
完整的代码

这决不是一个完整的解决方案。事实上,将
JSON
写入一个文件似乎是一件麻烦事——但它确实存在


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

从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());
}
完整的代码

这决不是一个完整的解决方案。事实上,将
JSON
写入一个文件似乎是一件麻烦事——但它确实存在


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

是什么让您认为文件是或可以“获取的”?为什么在使用Twitter提要时需要文件?如何将对象转换为字符串并将该字符串写入文件?使用流?一个
ByteArrayInputStream
来自字符串。@Thufir是的,但我的意思是不先写入
文件。我很确定
JSonParser
可以从
InputStream
读取,您可以从
String.getBytes(charset)
获取
ByteArrayInputStream
。或
读卡器
StringReader
。啊,基本IO,你为什么被遗忘了。是BaseX库在扔东西,但不,你不需要将东西存储到文件中,然后将它们读回你已经在内存中的东西。是什么让你认为文件是或可以“获取的”?为什么在使用Twitter提要时需要文件?如何将对象转换为字符串并将该字符串写入文件?使用流?一个
ByteArrayInputStream
来自字符串。@Thufir是的,但我的意思是不先写入
文件。我很确定
JSonParser
可以从
InputStream
读取,您可以从
String.getBytes(charset)
获取
ByteArrayInputStream
。或
读卡器
StringReader
。啊,基本IO,你为什么被遗忘了呢?是BaseX库把东西扔掉了,但是不,你不需要把东西存储到一个文件中,然后把它们读回你已经在内存中的东西。