采用net.sf.json.JSONObject处理数据时,type字段序列化后能不能和采用Map处理数据时输出的结果一致呢?
@Test
public void testJsonObject() throws JsonProcessingException {
ObjectMapper objectMapper = new ObjectMapper();
List<String> type = Lists.newArrayList("A", "B");
JSONObject jsonObject = new JSONObject();
jsonObject.put("type", objectMapper.writeValueAsString(type));
System.out.println(objectMapper.writeValueAsString(jsonObject));
Map<String, Object> map = new HashMap<>();
map.put("type", objectMapper.writeValueAsString(type));
System.out.println(objectMapper.writeValueAsString(map));
}
输出
{"type":["A","B"]}
{"type":"[\"A\",\"B\"]"}
序列化两次type
jsonObject.put("type", objectMapper.writeValueAsString(objectMapper.writeValueAsString(type)));
输出
{"type":"[\\\"A\\\",\\\"B\\\"]"}
{"type":"[\"A\",\"B\"]"}
输出与采用Map还是不同,Map输出的type可以直接反序列化为字符串数组,但是序列化两次的不能直接反序列化为字符串数组
实在没找到这个
net.sf.json.JSONObject
建议你换个JSON类库。