将<String, Object>映射到JSON字符串

huangapple go评论78阅读模式
英文:

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&lt;String, Object&gt; ticketSearchResult = new HashMap&lt;String, Object&gt;();

         ticketSearchResult.put(&quot;ticketListJson&quot;, ticketListJson);
         ticketSearchResult.put(&quot;searchCriteriaJson&quot;, searchCrteriaJson);
         ticketSearchResult.put(&quot;count&quot;, 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:

{&quot;count&quot;:7,&quot;searchCriteriaJson&quot;:&quot;{\&quot;startRecord\&quot;:0,\&quot;sortOrder\&quot;:\&quot;DESC\&quot;,\&quot;ticketStatus\&quot;:\&quot;[\\\&quot;Any\\\&quot;]\&quot;,\&quot;pageSize\&quot;:10,\&quot;sortBy\&quot;:\&quot;Default\&quot;,\&quot;customer\&quot;:1599}&quot;,&quot;ticketListJson&quot;:&quot;[{\&quot;id\&quot;:\&quot;30\&quot;,\&quot;subject\&quot;:\&quot;Test\&quot;,\&quot;number\&quot;:\&quot;TIC-30\&quot;,...

How can I have these inner json strings maintain their normal values without it adding a bunch of escape characters.

答案1

得分: 1

searchCriteriatkmTicketList对象直接放入ticketSearchResult映射中,而不是多次通过writeValueAsString方法处理。Jackson会遍历映射并序列化它遇到的对象。

如果你需要在序列化这些其他对象时做一些更复杂的操作,你可以这样做:(这是我首先写的,后来意识到对你的需求来说有点过度了)

searchCriteriatkmTicketList转换为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()。直接将ticketStatustkmTicketListsearchCriteria插入地图中。

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&lt;String, Object&gt; ticketSearchResult = new HashMap&lt;String, Object&gt;();

ticketSearchResult.put(&quot;ticketListJson&quot;, tkmTicketList);
ticketSearchResult.put(&quot;searchCriteriaJson&quot;, searchCriteria);
ticketSearchResult.put(&quot;count&quot;, iResultCt);

return mapper.writeValueAsString(ticketSearchResult);

huangapple
  • 本文由 发表于 2020年4月10日 07:38:25
  • 转载请务必保留本文链接:https://go.coder-hub.com/61131905.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定