سؤال

In my Servlet a JSONObject is created with the value:

{'alerts':true}

When I am trying to print its value on JSP page using , it is printing the JSON object as String . It is printed as

 "{'alerts':true}"

How do I print in JSON format rather than String?

In Servlet:

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

In JSP:

<br><br><json:property name="audioAlerts" value="${myBean.audioAlerts"}" />;
    <br> Expected output: {'alerts':true}
    <br>Acutal output: "{'alerts':true}"
هل كانت مفيدة؟

المحلول

As per http://json-taglib.sourceforge.net/tutorial.html

By setting the value="..." attribute on the tag. <json:property/>

  • The same trimming and encoding rules apply as described above.
  • If the value specified is a Boolean, then it will be converetd to a JSON boolean
  • If the value specified is a number (Integer, Short, Long, Double, Float) then it will be - -converted to a JSON numeric value.
  • If the value is a String it will be converted to a JSON string.
  • Any other Java types that are used to set the value will have toString() called on them and they will be treated as JSON strings.

As per documentation your value is getting converted to Json String

so try to put your <json:property> into <json:object></json:object>

or else you can parse your jSON string in javascript

var jsonObj = JSON.parse(audioAlerts)
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top