Spring Boot:双引号格式错误

Posted

技术标签:

【中文标题】Spring Boot:双引号格式错误【英文标题】:Spring Boot: getting wrong format of double qoutes 【发布时间】:2020-12-29 00:59:44 【问题描述】:

我正在编写一个应用程序,我需要在其中通过 Thymeleaf 将一些数据从 Spring Boot 控制器发送到模板。

在我想发送一些 JSON 之前,我没有遇到任何问题。

双qoute(")改为"

因此我遇到了错误。

控制器:

@GetMapping("/statistics")
public String viewStatistics(Model model) 
    JSONArray jsonArray = statisticsService.getTaskNamePercentageMap();
    System.out.println(jsonArray);
    model.addAttribute("taskNamePercentageMap", jsonArray);
    return "statistics/main";

System.out.println(jsonArray) 输出:

["low":33,"name":"Tenis","low":100,"name":"Rugby"]

统计/主模板中的 JavaScript 代码:

$(document).ready(function () 
        var json =[[$taskNamePercentageMap]];
        /*... TO BE CONTINUED ...*/
    );

Chrome 开发者标签中的变量“a”来源:

var b = JSON.stringify(["low":33,"name":"Tenis","low":100,"name":"Rugby"]);

谁能告诉我问题出在哪里以及如何解决?

【问题讨论】:

看看documentation,8.3部分 【参考方案1】:

试试这个

var json = [($taskNamePercentageMap)];

来自Thymeleaf 3.0 docs

请注意,虽然 [[...]] 对应于 th:text(即结果将被 html 转义),但 [(...)] 对应于 th:utext 并且不会执行任何 HTML 转义。

【讨论】:

以上是关于Spring Boot:双引号格式错误的主要内容,如果未能解决你的问题,请参考以下文章

Spring boot->redis value值出现两个双引号

JSON的键究竟需要用双引号括起来吗

json中可以放中文双引号吗

opencsv写入时去掉双引号

序列化包含转义(反斜杠和双引号)的 JSON 字符串 Swift 返回格式错误的对象

从单词列表中删除引号和双引号