user2200561 user2200561 - 1 year ago 74
Java Question

How to read the JSONArray from json file and append new json array elements and again write it to the same json file using java

I have json array in the below shown format in my json file


Now I want to add another array into this through java and my json file should look like


I have tried many examples googling around but cud not get any satisfactory answer. Please Help

Answer Source

You can get array from file, parse it to List, add Java object to it, parse all list back to json array and rewrite all file.

Another approach is to use RandomAccessFile.class First set file-pointer to end of file, read file from end, and when']', append , and your json String.

There is you can find code example of second approach: Append JSON element to JSON array in file using Java

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download