【发布时间】:2023-04-04 00:05:02
【问题描述】:
在此代码部分中,我需要根据数据库记录从动态 json 对象中获取值。我在数据库上有两个表,称为通知和占位符多对多关系,通知可以包含动态数量的占位符,一个服务将向该服务发送 json 发布请求,以便用实际价值替换占位符,但由于该服务不知道关于动态占位符的数量应该从动态 json 数组中获取实际值,但是 json 对象包含通知 id,使用通知 id 我可以获取通知对象和相应的占位符,然后我可以从 json 对象中获取真实值。
这里是动态 json 对象。
{
"messageId":9,
"msisdn":"94763703355",
"<PACKNAME>":"Youtube",
"<PACKTYPE>":"Social Media",
}
这里是一条与 messageId 相关的通知消息:9
"You have succuessfully subscribe to <PACKNAME> and it will be <PACKTYPE> package"
这应该是输出:
"You have succuessfully subscribed to Youtube and it will be Social
Media package"
所有占位符都应替换为相关值,但我无法获得预期的输出
@服务 公共类 NotificationServiceImpl 实现 NotificationService{
@Autowired
RestTemplate restTemplate;
@Autowired
NotificationRepository notificationRepository;
@Override
public void getdata(String dynamicJson) throws IOException {
// create object mapper instance
ObjectMapper mapper = new ObjectMapper();
// convert JSON string to Java Object
DynamicJson dynamicJson1 = mapper.readValue(dynamicJson, DynamicJson.class);
// print user object
//System.out.println(dynamicJson1);
int id = dynamicJson1.getMessageId();
Optional<Notification> notification = notificationRepository.findById(id);
List<PlaceHolder> placeHolders = notification.get().getPlaceHolders();
Map<String, String> replacements = new HashMap<String, String>();
placeHolders.forEach(i -> replacements.put(i.getValue(), dynamicJson1.getPlaceholders().get(i).toString()));
String message = notification.get().getMessage();
StringBuilder result = new StringBuilder(message.length());
String delimiters = "+-*/(),. ";
StringTokenizer st = new StringTokenizer(message, delimiters, true);
while (st.hasMoreTokens()) {
String w = st.nextToken();
if (replacements.containsKey(w)) {
result.append(replacements.get(w));
} else {
result.append(w);
}
}
System.out.println(result.toString());
}
}
实体类
公共类 DynamicJson {
@Override
public String toString() {
return "DynamicJson{" +
"messageId=" + messageId +
", msisdn='" + msisdn + '\'' +
", placeholders=" + placeholders +
'}';
}
public DynamicJson() {
}
public DynamicJson(int messageId, String msisdn, Map<String, Object> placeholders) {
this.messageId = messageId;
this.msisdn = msisdn;
this.placeholders = placeholders;
}
public int getMessageId() {
return messageId;
}
public void setMessageId(int messageId) {
this.messageId = messageId;
}
public String getMsisdn() {
return msisdn;
}
public void setMsisdn(String msisdn) {
this.msisdn = msisdn;
}
public Map<String, Object> getPlaceholders() {
return placeholders;
}
@JsonAnySetter
public void setAddress(String key, Object value) {
placeholders.put(key, value);
}
private int messageId;
private String msisdn;
private Map<String, Object> placeholders = new HashMap<>();
}
控制器
@自动连线 NotificationService 通知服务;
@RequestMapping(value = "/", method = RequestMethod.POST,
consumes = "application/json", produces = "application/json")
public void getObject(@RequestBody String dynamicJson) throws IOException {
notificationService.getdata(dynamicJson);
}
这是我得到的输出
You have succuessfully subscribed to Youtube and it will be <PACKTYPE> package
You have succuessfully subscribed to <PACKNAME> and it will be Social Media package
我真正需要的是
You have succuessfully subscribed to Youtube and it will be Social Media package
Placeholder 和 realValue 元素计数是动态的,从 1 到 n
【问题讨论】:
-
你试过用调试器单步调试代码吗?
-
是的,但我无法弄清楚出了什么问题
-
这是你的外层 for 循环。你循环你的占位符。您应该将占位符放在 Map 中,其值为 realValue
标签: java json string spring-boot replace