Coding With Fun
Home Docker Django Node.js Articles Python pip guide FAQ Policy

Introduction to the interoperability of json format data between java and javascript


May 08, 2021 JSON


Table of contents


The interoperability of objects and strings in javascript

Objects are turned into strings: Through the JSON.encode method, this is the method .js the Json method, which is introduced into the current file.
String conversion to object: (1) Using the JSON.decode method, i.e., the introduction of js is on it. ( 2) There's a method in jQuery, and $.parseJson can do it.

The json string in java intersects with the object

Object conversion to string: There is a JohnsonUtil.serial .jar in the struts2-json-plugin method. Y ou can also freely customize, through string stitching, the properties of the json string must be double quotes, single quotes or not applicable will be false. e g:"{\"id\":123,\"name\":\"wch\",\"children\":[{\"id\":456}]}",
Strings are converted to objects: JasonObject.fromObject(), in the case of arrays, use JasonArray.fromObject().json objects with only objects or arrays. H owever, this can only convert the current property, if there are several groups in the object or the list will assign the conversion itself again. F or example, the jason string above you can't step in place, even if the object has a property that the list won't automatically convert the string to a list.

The java string is converted to a javascript object

The contact between java and js will only occur in jsp (as I currently think), in jsp, as long as the var obj is the only way to do it. Y ou can also assign the string var obj in js first to the object using the string in the first step, "

Javascript strings are converted to java objects

If you want to implement it yourself, (1) convert it to a string in js and assign it to the properties in java by passing arguments. ( 2) Use the low second step method to convert. I f it's all so easy to use struts2, all you have to do is define the properties of the same name in action.