I have one external .java file which is having below fields,
public class PersonDetails {
private String firstName;
private String lastName;
private Integer hobby;
private List<String> address;
private Map<String, BigDecimal> salary;
private String[] position;
}
I am passing this file as a input to the REST api and trying to converts its contents into json string
@PostMapping(value = "/poc/jsobj", produces = {APPLICATION_JSON_VALUE})
public ResponseEntity<ResponseMessage> convertToJson(@RequestParam("file") MultipartFile file) {
JSONObject javaObjectDetials = new JSONObject();
try {
if (!file.isEmpty()) {
byte[] bytes = file.getBytes();
String completeData = new String(bytes);
System.out.print(completeData);
String pattern = "(\\w*);";
Matcher m = Pattern.compile(pattern).matcher(completeData);
while (m.find()) {
System.out.println(m.group(1));
javaObjectDetials.put(m.group(1), "");
}
}
return ResponseEntity.status(HttpStatus.OK).body(new ResponseMessage(javaObjectDetials.toString()));
} catch (Exception e) {
String str = "";
str = "Could not get the file: " + file.getOriginalFilename() + "!";
return ResponseEntity.status(HttpStatus.EXPECTATION_FAILED).body(new ResponseMessage(str));
}
}
When I run the application, i am getting json string as below
{
"jsonString": "{\"firstName\":\"\",\"lastName\":\"\",\"address\":\"\",\"position\":\"\",\"salary\":\"\",\"hobby\":\"\"}"
}
but depending on the datatype of each field i want json string as below,
{
"firstName":"",
"lastName":"",
"address":[],
"position":[],
"salary": {},
"hobby": 1
}
Can anyone help me on this?