Java passes JSON data to JS in JSP

Source: Internet
Author: User

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.