Jackson adds backslash in json

后端 未结 8 1037
伪装坚强ぢ
伪装坚强ぢ 2020-12-31 00:55

I\'m building REST service on Jersey and using Jackson to produce JSON from java classes of my model. Model with absolutely simple values, I think

8条回答
  •  Happy的楠姐
    2020-12-31 01:30

    I have also the same problem and tried different solutions, but non works. The problem is not with the mapper, but with the input to the mapper. As in your case:
    jsonInString = mapper.writeValueAsString(users);
    'users' is a collection. You need to convert each user to JSONObject, add it to JSONArray and then use the mapper on the array: like this
    JSONArray users = new JSONArray(); for (Collection user : usersCollection) { JSONObject user = new JSONObject(mapper.writeValueAsString(user)); users.put(user); } mapper.writeValueAsString(user));

提交回复
热议问题