Json format data transfer between Java and javascript

  • 2020-03-26 21:42:18
  • OfStack

Object and string interactions in javascript

Object to string: through json.encode method, which is json.js method, introduced into the current file can be.

String conversion to objects: (1) use json.decode method, ibid., js can be introduced. (2) jQuery has a method, $.parsejson can also be implemented.

Json string and object interchange in Java

Object to a string: there is a jsonutil.serial method in struts2-json-plugin.jar. You can also customize it by yourself, by concatenating strings, json string attributes must be double quotes, single quotes or is not applicable will report an error. Eg: "{\ \" id ": 123, \" name \ ": \" WCH \ ", \ "children \" : [{\ \ "id" : 456}]} ",

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 property, and if there is an array or a list in the object, you have to assign the conversion again. Just like the json string above, you can't do it all at once, even if the object has a property of children, the list will not automatically convert the string to a list for you.

Java strings are converted into javascript objects

The contact between Java and js will only occur in JSPS (as far as I'm concerned), where as long as var obj; % % = javaStr > That's it. Var obj = "< % % = javaStr >" Use the string in the first step to convert to an object.

Javascript strings are converted to Java objects

If you want to implement it yourself, first converted to a string in js, by the way of parameter transfer to Java properties. (2) use the method of the lower second step to convert. All of this would be very easy to do with struts2. All you have to do is define the properties of the same name in the action.
Converting Java objects to javascript strings and javascript objects to Java strings should be a good idea, but now that everyone knows it, I'm not wasting my breath. If you have any questions, please e-mail:1329555958@qq.com


Related articles: