0

在我的 Servlet 中,使用以下值创建了一个 JSONObject:

{'alerts':true}

当我尝试使用在 JSP 页面上打印其值时,它会将 JSON 对象打印为 String 。它打印为

 "{'alerts':true}"

如何以 JSON 格式而不是 String 格式打印?

在 Servlet 中:

public JSONObject getAudioAlerts() {
    JSONObject val = new JSONObject("{'alerts':true}");
     return val;
}

在 JSP 中:

<br><br><json:property name="audioAlerts" value="${myBean.audioAlerts"}" />;
    <br> Expected output: {'alerts':true}
    <br>Acutal output: "{'alerts':true}"
4

1 回答 1

2

根据http://json-taglib.sourceforge.net/tutorial.html

通过在标签上设置 value="..." 属性。 <json:property/>

  • 应用与上述相同的修剪和编码规则。
  • 如果指定的值是布尔值,那么它将被转换为 JSON 布尔值
  • 如果指定的值是一个数字(Integer、Short、Long、Double、Float),那么它将被转换为 JSON 数值。
  • 如果值是字符串,它将被转换为 JSON 字符串。
  • 用于设置值的任何其他 Java 类型都将调用 toString() 并且它们将被视为 JSON 字符串。

根据文档,您的值正在转换为 Json String

所以试着把你<json:property><json:object></json:object>

否则你可以在 javascript 中解析你的 JSON 字符串

var jsonObj = JSON.parse(audioAlerts)
于 2014-05-15T11:00:56.887 回答