Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Remove double quotes from JSON String

Tags:

java

json

jackson

I've below JSON string:

{ "students" : "[  {\"studentId\" : \"A1\",\"studentNumber\" 
 : \"287\",\"studentType\" : \"FullTime\"} ]"  }

In order to deserialize this string in java object, I've to remove \ which can be done using string replace method. Apart from that there are double quotes also just before [ and after ]. how do I remove these double quotes or allow them while deserializeing using Jackson.

like image 258
gaity Avatar asked Sep 19 '26 02:09

gaity


2 Answers

You don't have to do it yourself, jackson will take care of it. Create a pojo class Student and you can write something like below:

ObjectMapper mapper = new ObjectMapper();
Student student = mapper.readValue(responseBody, Student.class);
like image 199
Alok Dubey Avatar answered Sep 21 '26 15:09

Alok Dubey


Try to replace "[ with [ and ]" with ]

json = json.replaceAll("\"\\[","[");
json = json.replaceAll("\\]\"", "]");

like image 45
Abhishek Patyal Avatar answered Sep 21 '26 17:09

Abhishek Patyal



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!