简体   繁体   English

使用 Java ObjectMapper 获取 JSON 字符串

[英]Using Java ObjectMapper to get JSON String

I am using writeValueAsString of the ObjectMapper .我正在使用ObjectMapperwriteValueAsString However, it's giving me a Java String representation so I get:但是,它给了我一个 Java 字符串表示,所以我得到:

{"network_id":5000370004610700049753}

instead of代替

"{\"network_id\":5000370004610700049753}"

which is failing for other services when deserializing.反序列化时其他服务失败。 How do I get this kind of serialization with the ObjectMapper ?如何使用ObjectMapper获得这种序列化?

To get the second result, send it through the ObjectMapper again.要获得第二个结果,请再次通过ObjectMapper发送它。

Map<String, Object> data = new HashMap<>();
data.put("network_id", new BigInteger("5000370004610700049753"));

ObjectMapper objectMapper = new ObjectMapper();

String plainJson = objectMapper.writeValueAsString(data);
System.out.println(plainJson);

String encodedJson = objectMapper.writeValueAsString(plainJson);
System.out.println(encodedJson);

Output Output

{"network_id":5000370004610700049753}
"{\"network_id\":5000370004610700049753}"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM