如何按指定格式将JSON内容存入String变量?
如何将指定JSON结构存入Java String变量?
嘿,我来帮你搞定这个需求!你想要把包含三个地址对象、customerStatus和动态获取的createdDate的JSON结构存入Java的String变量对吧?我给你两种靠谱的实现思路,按需选择就行~
1. 直接字符串拼接(快速实现)
这种方式直接把JSON结构拼起来,注意所有双引号都要加转义符\",同时要处理globalMap取值可能为null的情况,避免JSON里出现"createdDate":"null"这种无效内容:
// 先从globalMap获取createdDate,处理null值 String createdDate = (String) globalMap.get("JsonCreatedDate"); createdDate = createdDate != null ? createdDate : ""; // 拼接完整JSON字符串 String inputJSON = "{ \"billingAddress\":{\"city\":\"\",\"country\":\"\",\"postalCode\":\"\",\"state\":\"\",\"streetAddress\":\"\"}, " + "\"shippingAddress\":{\"city\":\"\",\"country\":\"\",\"postalCode\":\"\",\"state\":\"\",\"streetAddress\":\"\"}, " + "\"personAddress\":{\"city\":\"\",\"country\":\"\",\"postalCode\":\"\",\"state\":\"\",\"streetAddress\":\"\"}, " + "\"customerStatus\":\"\", " + "\"createdDate\":\"" + createdDate + "\" }";
这种方式简单直接,但如果后续要修改JSON结构(比如加字段、改格式),很容易因为漏写转义符而出错,适合临时快速实现。
2. 使用JSON库构建(推荐,更健壮)
如果是项目中需要长期维护的代码,强烈推荐用JSON库(比如Jackson、Gson)来构建JSON结构,完全不用手动处理转义,还能自动处理各种数据类型和null值问题。这里以Jackson为例:
方式1:用Map构建JSON结构
import com.fasterxml.jackson.databind.ObjectMapper; import java.util.HashMap; import java.util.Map; // 初始化Jackson的ObjectMapper ObjectMapper objectMapper = new ObjectMapper(); // 构建空的地址Map Map<String, String> emptyAddress = new HashMap<>(); emptyAddress.put("city", ""); emptyAddress.put("country", ""); emptyAddress.put("postalCode", ""); emptyAddress.put("state", ""); emptyAddress.put("streetAddress", ""); // 构建整个JSON的顶层Map Map<String, Object> jsonData = new HashMap<>(); jsonData.put("billingAddress", new HashMap<>(emptyAddress)); jsonData.put("shippingAddress", new HashMap<>(emptyAddress)); jsonData.put("personAddress", new HashMap<>(emptyAddress)); jsonData.put("customerStatus", ""); // 处理动态的createdDate String createdDate = (String) globalMap.get("JsonCreatedDate"); jsonData.put("createdDate", createdDate != null ? createdDate : ""); // 序列化为JSON字符串 String inputJSON = objectMapper.writeValueAsString(jsonData);
方式2:用JsonNode构建(更灵活)
如果不想用Map,也可以用Jackson的JsonNode来构建,结构更直观:
import com.fasterxml.jackson.databind.node.ObjectNode; ObjectMapper objectMapper = new ObjectMapper(); ObjectNode rootNode = objectMapper.createObjectNode(); // 创建空地址节点 ObjectNode emptyAddrNode = objectMapper.createObjectNode(); emptyAddrNode.put("city", ""); emptyAddrNode.put("country", ""); emptyAddrNode.put("postalCode", ""); emptyAddrNode.put("state", ""); emptyAddrNode.put("streetAddress", ""); // 给顶层节点添加地址字段 rootNode.set("billingAddress", emptyAddrNode.deepCopy()); rootNode.set("shippingAddress", emptyAddrNode.deepCopy()); rootNode.set("personAddress", emptyAddrNode.deepCopy()); rootNode.put("customerStatus", ""); // 设置动态的createdDate String createdDate = (String) globalMap.get("JsonCreatedDate"); rootNode.put("createdDate", createdDate != null ? createdDate : ""); // 转成字符串 String inputJSON = objectMapper.writeValueAsString(rootNode);
用JSON库的好处是结构清晰,修改字段时不容易出错,而且能自动处理JSON格式的合法性,适合复杂场景或者需要长期维护的代码。
内容的提问来源于stack exchange,提问作者Squeez
相关产品推荐
相关产品推荐

