In the data transfer process, JSON is passed in the form of text, which is a string, and JS operates on a JSON object, so the conversion between the JSON object and the JSON string is key. For example:
JSON string:
var str1 = ' {' name ': ' cxh ', ' sex ': ' Man '} ';
JSON object:
var str2 = {"Name": "Cxh", "Sex": "Man"};
The following is the problem encountered today, Java background delivery parameters to the foreground JSP JS, has been unable to fetch, find the reason to find that is not to write double quotes ""
JS code is written in the JSP, the following code is the wrong code, unable to fetch JSON data, the code is as follows:
The error code is as follows (never write this):
Get the code for the JSON string:
<script type= "Text/javascript" >var aa = ' <%=request.getattribute ("testtypejson")%> ' ; alert (AA);
Run:
Get the code for the JSON object:
<script type= "Text/javascript" >
var AA = <%=request.getattribute ("testtypejson")%>;
alert (AA);
alert (aa.key1);
Run:
Java passes JSON data to JS in JSP