JSON format data interchange between Java and JavaScript _javascript tips

Source: Internet
Author: User

the interchange of objects and strings in JavaScript

Object to a string: Through the Json.encode method, this is the json.js inside of the method, introduced to the current file can be.

String conversion to object: ① using the Json.decode method, ditto, the introduction of JS can be. There is a method in ②jquery, $.parsejson can also be implemented.

The interaction of JSON strings and objects in Java

Object into a string: There is a jsonutil.serial method in the Struts2-json-plugin.jar. can also be free to customize, through string concatenation, the properties of the JSON string must be double quotes, single quotes or is not applicable will be an error. Eg: "{\" id\ ": 123,\" name\ ": \" wch\ ", \" children\ ": [{\" id\ ": 456}]}",

String conversion to object: Jsonobject.fromobject (), if it is an array, use Jsonarray.fromobject (). Only objects or arrays are in the JSON object. However, this can only transform the current property, if the object has an array or a list of the value of its own conversion. Just like the JSON string above you can't one-step, even if an object has an attribute that is children this list does not automatically convert the string to a list.

Java strings converted to JavaScript objects

Java and JS contact will only happen in the JSP (I currently think so), in the JSP as long as the var obj = <%=javaStr%> on it. You can also assign a value to the string var obj = "<%=javaStr%>" in JS to use the string in the first step to convert to an object.

JavaScript string conversion to Java object

If you want to do it yourself, ① first converts to a string in JS and assigns values to the properties in Java by passing parameters. ② uses the lower second step method to convert. It all seems so easy if you use Struts2, and all you have to do is define an attribute with the same name in the action.
Java objects converted to JavaScript strings and JavaScript objects converted to Java strings, the use of your imagination, should have been clear to the chest, since everyone has been aware of it, I will not waste my breath. If you have any questions, please e-mail:1329555958@qq.com

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.