Suppose I have "file.txt" or "file.json" in my android asset folder. How can I make a JSONObject from the file?
I need something like this finally : JSONObject json = new JSONObject(jsontxt);
I have browsed a lot of stackoverflow questions , didn't find answer
You need to obtain the file content first.
You might want to use a FileReader
for that.
new BufferedReader(new FileReader("path-to-file"));
Or if the file is in your classpath
final InputStream inputStream =
ClassLoader.getSystemClassLoader()
.getResourceAsStream("path-to-file");
new BufferedReader(new InputStreamReader(inputStream , "UTF-8"));
Then you can use the composed String
to construct a new JSONObject
.
To obtain an InputStream
for your File, which is an asset, you can leverate the AssetManager
. Pseudocode
InputStream is = AssetManager#open(fileName)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.