简体   繁体   中英

Escape quotation marks using Java, JSP, JavaScript

I have the following JavaScript code in a JSP page

<script>
    var myJSVar = jQuery.parseJSON('${ aJSONStringFromJava}');
</script>

The parsed JSON is used to populate a html table and looks like this:

[{column1: "valueColumn1Row1", column2: "valueColumn2Row1"},    {column1: "valueColumn2Row1", column2: "valueColumn2Row2"}, ...]

In my Java code I populate the JSONObject like this:

JSONObject jsonObject = new JSONObject();
for(Bean bean : beans){
  jsonObject.put("colum1", bean.getColumn1());
  jsonObject.put("column2", bean.getColumn2());
}

Now I need to put an image in my table similar to this

 jsonObject.put("column1", "<img src='image.png'>");

However at this point I get mixed up with the quotation marks. Using single quotation marks will interfere with the ones from the JS variable declaration, double quotation marks will interfere with the ones used in the JSON. Can anybody please tell me how I can escape the quotation marks to make this work. Thanks!

像这样转义双引号怎么办:

"<img src=\\"image.png\\">"

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM