英文:
A map of <String, Object> to JSONString
问题
Here is the translated code portion you provided:
ObjectMapper mapper = new ObjectMapper();
searchCriteria.put(TICKET_STATUS_LIST,
mapper.writeValueAsString(ticketStatus));
String ticketListJson = mapper.writeValueAsString(tkmTicketList);
String searchCriteriaJson = mapper
.writeValueAsString(searchCriteria);
Map<String, Object> ticketSearchResult = new HashMap<String, Object>();
ticketSearchResult.put("ticketListJson", ticketListJson);
ticketSearchResult.put("searchCriteriaJson", searchCriteriaJson);
ticketSearchResult.put("count", iResultCt);
return mapper.writeValueAsString(ticketSearchResult);
Regarding your issue with escaping characters, it seems like your JSON is being HTML-encoded or escaped. To avoid this, you should ensure that when you write the JSON as a string, it doesn't get encoded. You can use the writeValueAsString
method without encoding. Here's how you can modify the code:
ObjectMapper mapper = new ObjectMapper();
searchCriteria.put(TICKET_STATUS_LIST,
mapper.writeValueAsString(ticketStatus));
String ticketListJson = mapper.writeValueAsString(tkmTicketList);
String searchCriteriaJson = mapper
.writer() // Use the writer() method to configure serialization
.writeValueAsString(searchCriteria); // Serialize without HTML encoding
Map<String, Object> ticketSearchResult = new HashMap<String, Object>();
ticketSearchResult.put("ticketListJson", ticketListJson);
ticketSearchResult.put("searchCriteriaJson", searchCriteriaJson);
ticketSearchResult.put("count", iResultCt);
return mapper.writeValueAsString(ticketSearchResult);
By using writer()
and writeValueAsString
without HTML encoding, you should get the JSON without the escape characters for double quotes.
英文:
ObjectMapper mapper = new ObjectMapper();
searchCriteria.put(TICKET_STATUS_LIST,
mapper.writeValueAsString(ticketStatus));
String ticketListJson = mapper.writeValueAsString(tkmTicketList);
String searchCrteriaJson = mapper
.writeValueAsString(searchCriteria);
Map<String, Object> ticketSearchResult = new HashMap<String, Object>();
ticketSearchResult.put("ticketListJson", ticketListJson);
ticketSearchResult.put("searchCriteriaJson", searchCrteriaJson);
ticketSearchResult.put("count", iResultCt);
return mapper.writeValueAsString(ticketSearchResult);
I've come across this delightful code, problem is that the search criteria and ticketListJson end up being treated like strings so I get a crappy json as out:
{"count":7,"searchCriteriaJson":"{\"startRecord\":0,\"sortOrder\":\"DESC\",\"ticketStatus\":\"[\\\"Any\\\"]\",\"pageSize\":10,\"sortBy\":\"Default\",\"customer\":1599}","ticketListJson":"[{\"id\":\"30\",\"subject\":\"Test\",\"number\":\"TIC-30\",...
How can I have these inner json strings maintain their normal values without it adding a bunch of escape characters.
答案1
得分: 1
将searchCriteria
和tkmTicketList
对象直接放入ticketSearchResult
映射中,而不是多次通过writeValueAsString
方法处理。Jackson会遍历映射并序列化它遇到的对象。
如果你需要在序列化这些其他对象时做一些更复杂的操作,你可以这样做:(这是我首先写的,后来意识到对你的需求来说有点过度了)
将searchCriteria
和tkmTicketList
转换为JSON树节点,而不是字符串,然后它们将包含在搜索结果JSON中,而不是重新转义为字符串:
JsonNode searchCriteriaJson = mapper.convertValue(searchCriteria, JsonNode.class);
英文:
Just put the searchCriteria
and tkmTicketList
objects directly into the ticketSearchResult
map rather than going through the writeValueAsString
method multiple times. Jackson will walk through the Map and serialize the objects it encounters.
If you needed to do something more complicated with the serialization of those other objects, you could do this: (which I wrote first, then realised it was overkill for what you need)
Convert the searchCriteria
and tkmTicketList
to JSON tree nodes rather than strings, and then they will be included in the search result JSON rather than being re-escaped as strings:
JsonNode searchCriteriaJson = mapper.convertValue(searchCriteria, JsonNode.class)
答案2
得分: 1
不要多次调用writeValueAsString()
。直接将ticketStatus
、tkmTicketList
和searchCriteria
插入地图中。
ObjectMapper mapper = new ObjectMapper();
searchCriteria.put(TICKET_STATUS_LIST, ticketStatus);
Map<String, Object> ticketSearchResult = new HashMap<String, Object>();
ticketSearchResult.put("ticketListJson", tkmTicketList);
ticketSearchResult.put("searchCriteriaJson", searchCriteria);
ticketSearchResult.put("count", iResultCt);
return mapper.writeValueAsString(ticketSearchResult);
英文:
Don't call writeValueAsString()
more than once. Insert ticketStatus
, tkmTicketList
, and searchCriteria
into the maps directly.
ObjectMapper mapper = new ObjectMapper();
searchCriteria.put(TICKET_STATUS_LIST, ticketStatus);
Map<String, Object> ticketSearchResult = new HashMap<String, Object>();
ticketSearchResult.put("ticketListJson", tkmTicketList);
ticketSearchResult.put("searchCriteriaJson", searchCriteria);
ticketSearchResult.put("count", iResultCt);
return mapper.writeValueAsString(ticketSearchResult);
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论