Springmvc value (object or string) to the foreground JS

Source: Internet
Author: User

Java object to JS object

1. First use Jackson to convert the object to a JSON string

Objectmapper objectmapper = new Objectmapper ();

String json = objectmapper.writevalueasstring (obj); Obj is the object to be passed

2. Use Model.addattribute ("JSON", JSON) in the controller to upload the JSON string to the foreground

3. Then do the following in the foreground (these two quotes are important)

var jsonstr = '${json}'

Use Json.parse (); Convert JSON to JS object

obj = Json.parse (jsinfo);

// We can print to the console and look at this object.

Console.log (Jsninfo);

List of Java objects to JS arrays (array)

You just have to turn obj in the 1th step into list<t>.

Objectmapper objectmapper = new Objectmapper ();

String json = objectmapper.writevalueasstring (orderlist); OrderList to List<order>

Java nested object to JS object

You just need to turn obj in the 1th step into a map.

Objectmapper objectmapper = new Objectmapper ();

String JSON = objectmapper.writevalueasstring (map); Map is the map type, key is the property name, value is the referenced object


Springmvc value (object or string) to the foreground JS

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.