1. 程式人生 > >Android — Writing a file as a JSON object

Android — Writing a file as a JSON object

Android — Writing a file as a JSON object

There are certain use cases in android where you need to store and retrieve the data as a JSON(JavaScript Object Notation) object from a file.

There is no such defined way of storing a JSON object in a file. Data is stored in a file in the form of a string and that string is parsed into JSON. Hence, there are several challenges in parsing a string into a JSON object.

The first step is to create a variable which represents a file name.

In the second step, create a file object using the File class in java.

In the third step create the object of FileReader, FileWriter, BufferReader and BufferWriter and initialize them with the initial value of null. The FileReader and FileWriter objects are used to open the file in ‘read’ and ‘write’ mode respectively. Similarly, the BufferReader and BufferWriter objects are used to read and write from the file respectively.

In the fourth step, check for the existence of the file. If the file does not exist, then create a new file using the file object. Open the file in write mode using the fileWriter object and initialize it with an empty JSON object(“{}”) as a string using the bufferWriter object and then close the file using the bufferWriter object. This whole process needs to be enclosed in the try-catch block as the process may throw Input-Output exception.

In the fifth step, open the file in the read mode using the bufferReader objectand read the complete file line by line as a string and store the response in the ‘response’ variable.

Now consider a situation in which we are writing into a JSON object which has the username as its key and array of ids as its value.Parse the response into JSON using the JSONObject class in java and check for the existence of the key for a particular username.

If the user does not exist then create a new JSON array and put the id into the array using the put function offered by JSONArray class in java. After creating the array put the complete array into the JSON object using the put function offered by JSONObject class in java which takes two parameters- key and the value. The key will be the username and value will be the array of ids.

The file will look something like this after this step-

Now, if the username already exists in the file and we need to append an id to the existing array of ids then we will simply create a JSONArray object and retrieve the existing array of the user using ‘username’ as key and then putting the new id into the array using the put function.

Now the file will look something like this-

In the last and final step, open the file in write mode using the fileWriter and bufferWriter object. Write the JSON object which you created as a string into the file and then close the file.

The above steps clearly describe how to store a string data as a JSON object into a file and manipulating it accordingly.Hope this article was useful to you.Have a nice day.Goodbye.