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.
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);
尝试将"[
with [
and ]"
替换为]
json = json.replaceAll("\\"\\\\[","[");
json = json.replaceAll("\\\\]\\"", "]");
Like ObjectMapper
, we also may use Gson
for same purpose.
Gson gson = new Gson();
gson.fromJson(responseBody, Student.class);
public class StringTypeSerializationAdapter extends TypeAdapter<String> {
public String read(JsonReader reader) {
throw new UnsupportedOperationException();
}
public void write(JsonWriter writer, String value) throws IOException {
if (value == null) {
writer.nullValue();
return;
}
writer.jsonValue(value);
}
}
above removes quotes from strings using GSON string adapter
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.