Writing JSONObject to a file

I am using the Play platform. I have a JSONObject that has a structure similar to the one below (like in the console it printed)

{ "rows_map":{ "220":["mahesh", "outfit:bmtech,app:salesreport,uuname,ffname,llname", "Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.9.0.5", null ], "221":["mahesh", "outfit:bmtech,app:salesreport,uuname,ffname,llname", "Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.9.0.5", null ], "222":["mahesh", "outfit:bmtech,app:salesreport,uuname,ffname,llname", "Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.9.0.5", null ], "223":["mahesh", "outfit:bmtech,app:salesreport,uuname,ffname,llname", "Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.9.0.5", null ] }, "columns_map":["Sender", "Message Received", "Device", "Time" ] } 

I want to write this JSONObject to a file. Here is the code

 String path = "/var/www/html/Prj/public/CacheLayer/Incoming_Cache/CacheFileMgr.cache"; ObjectOutputStream outputStream = null; try{ outputStream = new ObjectOutputStream(new FileOutputStream(path)); System.out.println("Start Writings"); outputStream.writeObject(object); outputStream.flush(); outputStream.close(); }catch (Exception e){ System.err.println("Error: " + e); } 

The above does not successfully write to the file. A serialization error has occurred.

+6
source share
2 answers

Call toString in a JSONObject, and then serialize the string. JSONObject itself is not serializable.

 String jsonString = jsonObject.toString(); 
+13
source

JSON is serialization, it does not implement serializable, just converts it to a string and saves the string in a file (as text).

+6
source

Source: https://habr.com/ru/post/897318/


All Articles