3

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.

Phil
  • 157,677
  • 23
  • 242
  • 245
gaity
  • 73
  • 1
  • 2
  • 6
  • What if there is a data in any field containing a backslash like: `studentNumber: 2016\287` ? Still will you string replace? – Shafi Mar 06 '19 at 05:36
  • 1
    use JSON parsing instead. It should create the object from the json string. https://stackoverflow.com/questions/2591098/how-to-parse-json-in-java – Shafi Mar 06 '19 at 05:38
  • There won't be any data with backslash. main concern here is " before [ and after ]. – gaity Mar 06 '19 at 05:39
  • Answer of this question will help you. https://stackoverflow.com/questions/10308452/how-to-convert-the-following-json-string-to-java-object – Shafi Mar 06 '19 at 05:40

4 Answers4

5

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);
Alok Dubey
  • 419
  • 4
  • 13
  • I did the same thing but because of extra \ and quotes before [ and after ] it's giving error. I removed \ using replace method but not able to remove quotes before [ and after ]. – gaity Mar 06 '19 at 05:41
  • Below string works fine with my code: { "students" : [ {"studentId" : "A1","studentNumber" : "287","studentType" : "FullTime"} ] } – gaity Mar 06 '19 at 05:43
1

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

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

Abhishek Patyal
  • 514
  • 4
  • 8
  • This is working fine. Just one question.. why do we add \\ before [ and ]. – gaity Mar 06 '19 at 06:09
  • 1
    This is a hack that will likely break for nontrivial cases. I suspect the deserialized data will be wrong if anything in the student-objects contains json-escaped characters (e.g. quotes `"`, which will be doubly escaped) or `"[` or `"]`. – Felk Mar 07 '19 at 16:59
  • This should not have been the accepted answer – Ashwin Prabhu Jun 21 '22 at 07:06
0

Like ObjectMapper, we also may use Gson for same purpose.

Gson gson = new Gson();
gson.fromJson(responseBody, Student.class);
Tayyab Razaq
  • 348
  • 2
  • 11
0
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

Bhukailas
  • 47
  • 7