如何从任何"file.txt"或"file.json"轻松提取JSONObject?[假设文件已在系统中]



假设我在我的Android Asset文件夹中有" file.txt"或" file.json"。如何从文件中进行jsonobject?

我最终需要这样的东西: jsonObject json = new JSONOBJECT(JSONTXT);

我浏览了很多Stackoverflow问题,没有找到答案

您需要先获取文件内容。
您可能需要使用FileReader

new BufferedReader(new FileReader("path-to-file"));

或该文件在您的 classpath中

final InputStream inputStream = 
           ClassLoader.getSystemClassLoader()
                      .getResourceAsStream("path-to-file");
new BufferedReader(new InputStreamReader(inputStream , "UTF-8"));

然后,您可以使用组合的String构造新的JSONObject


要获取用于文件的InputStream,这是资产,您可以利用AssetManager。伪代码

InputStream is = AssetManager#open(fileName)

相关内容

最新更新