Home > Web Front-end > JS Tutorial > Introduction to the mutual transfer of json format data between java and javascript_javascript skills

Introduction to the mutual transfer of json format data between java and javascript_javascript skills

WBOY
Release: 2016-05-16 17:18:37
Original
978 people have browsed it

Interconversion between objects and strings in JavaScript

Convert objects to strings: through the JSON.encode method, this is the method in json.js, and it is introduced into the current file. That's it.

Convert string to object: ① Use JSON.decode method, same as above, just introduce js. ②There is a method in jQuery, $.parseJson can also be implemented.

Interconversion between json strings and objects in java

Conversion of objects into strings: There is a JsonUtil.serial method in struts2-json-plugin.jar. You can also customize it freely by yourself and implement it through string splicing. The attributes of the json string must be double quotes. If single quotes or are not applicable, an error will be reported. eg:"{"id":123,"name":"wch","children":[{"id":456}]}",

Convert string to object: JsonObject.fromObject( ), if it is an array, use JsonArray.fromObject(). There are only objects or arrays in the json object. However, this can only convert the current attributes. If there is an array or list in the object, you must assign and convert it again. For example, you cannot do the above json string in one step. Even if the object has an attribute called children, the string will not be automatically converted into a list for you.

Convert java string to javascript object

The contact between java and js will only happen in jsp (that’s what I think at the moment). In jsp, as long as var obj = <%=javaStr%> is fine. You can also assign it to the string var obj = "<%=javaStr%>" in js first, and then use the string in the first step to convert it into an object.

Convert javascript string to java object

If you want to implement it yourself, ① first convert it to a string in js and assign it to java by passing parameters properties in . ② Use the low second step method for conversion. If you use struts2, all this seems so easy. All you have to do is to define the attribute with the same name in the action.
Convert java objects to javascript strings and javascript objects to java strings. Use your imagination. You should already know it. Now that everyone knows it, I won’t waste my breath anymore. . If you have any questions, please e-mail: 1329555958@qq.com

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template