I want to write the rest response of my endpoint that is a json in a file from controller i'm returning JSONArray OBJ in response
[
{
"_fields": {
"key1":"value1",
"key2","value2"
}
},
{
"_fields": {
"key1":"value1",
"key2","value2"
}
},
{
"_count": 12,
"links": [
{
"rel": "prev",
"href": "http://localhost:8080/someprevurl"
},
{
"rel": "next",
"href": "http://localhost:8080/somenexturl"
},
{
"rel": "self",
"href": "http://localhost:8080/selfurl"
}
]
}
]
I've override the to string methods this is my method where i'm trying to write the jsonarray into the file
public static void generateFile(JSONArray fileContent, String directory, String fileName, String extension) {
logger.info("Writing into the file : {}{} start time : {}", fileName, extension, Instant.now());
try {
FileWriter file = new FileWriter(directory + fileName + extension);
file.write(fileContent.toJSONString());
file.flush();
} catch (IOException e) {
logger.error("Exception generated while generating file : {}", e.getMessage());
}
}
but after using toJSONString the response in file is like this
[
{
"_fields": MyDTONAME [
key1=value1,
key2="value2"
]
},
{
"_fields": MyDTONAME [
key1=value1,
key2="value2"
]
},
[
"_count": 12,
links: [.......]
]
]
do i need to define the complete format like replacing []
to {}
& =
to :
.jsonstring is not suppose to do that "" also missing so what to i do ? Please suggest