0

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

NinjaStar
  • 1
  • 6
  • You can use gson. Unless there is something else to your question I believe the answer is here: https://stackoverflow.com/a/28392599/1978785 – Charlie Wallace Feb 18 '19 at 21:06
  • @CharlieWallace I wanted String to Json (write on a file) and Json to String (read from a file) , In your given link i don't understand what is the use of Myclass. – NinjaStar Feb 18 '19 at 22:57
  • In the example MyClass will be the java class that the JSON is either being serialized into or created from. You will want to read the documentation at https://github.com/google/gson/blob/master/UserGuide.md. If you have a specific question you can either edit this question or ask a new question and we will be glad to help. For example what is the structure of file.json? Give me a simple example of what it looks like and I can show you what the java class will look like and how you can use gson to turn it into a java object of that class. – Charlie Wallace Feb 18 '19 at 23:16
  • If you want to use JSONObject instead of gson see: https://stackoverflow.com/a/5245881/1978785 – Charlie Wallace Feb 19 '19 at 02:18
  • Possible duplicate of [How to convert jsonString to JSONObject in Java](https://stackoverflow.com/questions/5245840/how-to-convert-jsonstring-to-jsonobject-in-java) – Charlie Wallace Feb 19 '19 at 02:20
  • @CharlieWallace lets say my project is like that : i fetch data from online ,write it to it offline , and if i disconnect (or not) user can get the data from the file . that's i am trying to do. Could you help me to find a better way to do that? – NinjaStar Feb 19 '19 at 17:14
  • @CharlieWallace I made the same question with brief details here this time. Would you please check it? https://stackoverflow.com/questions/54767447/reading-and-writing-from-and-to-a-json-file – NinjaStar Feb 19 '19 at 17:16

1 Answers1

0

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)
LppEdd
  • 20,274
  • 11
  • 84
  • 139
  • Thanks for trying to help me . but maybe this is not i asked , not worked for me. Would you please look at that ? I made the same question with full details https://stackoverflow.com/questions/54767447/reading-and-writing-from-and-to-a-json-file/54769138#54769138 – NinjaStar Feb 19 '19 at 17:11