When working on a project, if you want to issue commands to the ocx control, you must get the object in java in js, then put it into a format and send it out. . . It's simple when the object is one, but it's a little more troublesome when the object is an array.
At first I thought there was a simple way to convert the content directly, but later I found out that it was not possible. It was said online that there is no bridge between js and java, so:
My solution is : In the action layer, convert the java object array into a Json string, and in js, convert the json into an array object.
1. Convert the java object array into a Json string:
Two classes are needed:
1 2 3 4 5 6 7 8 9 10 | net.sf.json.JSONObject
net.sf.json.JSONArray
JSONObject json = JSONObject.fromObject(v);
jsonArray.add(json);
sendCommandList = jsonArray.toString();
|
Copy after login
Put them in the for loop middle.
2.js converts it into an object array:
1 2 3 4 5 |
var szJsonStr = '<s:property escapeJavaScript= "false" escape= "false" value= "sendCommandList" />';
|
Copy after login
Finally:
1 2 | var addVehicleArray = eval (szJsonStr);
|
Copy after login
When using it, just use addVehicleArray[i].vehicleType;. . . . . .
Extension:
Use of JSONObject and JSONArray
1.JAR package introduction
To make the program run, the JSON-lib package must be introduced, JSON- The lib package also depends on the following JAR package:
commons-lang.jar
commons-beanutils.jar
commons-collections.jar
commons-logging.jar
ezmorph.jar
json-lib-2.2.2-jdk15.jar
2.JSONObject object uses
The JSON-lib package is a beans, collections, maps, java arrays and A package for converting XML and JSON to and from each other. In this example, we will create JSONObject objects using the JSONObject class and then we print the values of these objects. In order to use JSONObject objects, we need to introduce the "net.sf.json" package. To add elements to an object, we use the put() method.
2.1.Example 1
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 | package jsontest;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
public class JSONObjectSample {
private static JSONObject createJSONObject() {
JSONObject jsonObject = new JSONObject();
jsonObject.put( "username" , "huangwuyi" );
jsonObject.put( "sex" , "男" );
jsonObject.put( "QQ" , "413425430" );
jsonObject.put( "Min.score" , new Integer(99));
jsonObject.put( "nickname" , "梦中心境" );
return jsonObject;
}
public static void main(String[] args) {
JSONObject jsonObject = JSONObjectSample.createJSONObject();
System.out.println( "jsonObject:" + jsonObject);
boolean isArray = jsonObject.isArray();
boolean isEmpty = jsonObject.isEmpty();
boolean isNullObject = jsonObject.isNullObject();
System.out.println( "是否为数组:" + isArray + ", 是否为空:" + isEmpty
+ ", isNullObject:" + isNullObject);
jsonObject.element( "address" , "福建省厦门市" );
System.out.println( "添加属性后的对象:" + jsonObject);
JSONArray jsonArray = new JSONArray();
jsonArray.add(0, "this is a jsonArray value" );
jsonArray.add(1, "another jsonArray value" );
jsonObject.element( "jsonArray" , jsonArray);
JSONArray array = jsonObject.getJSONArray( "jsonArray" );
System.out.println(jsonObject);
System.out.println( "返回一个JSONArray对象:" + array );
System.out.println( "结果=" + jsonObject);
String username = jsonObject.getString( "username" );
System.out.println( "username==>" + username);
String temp = jsonObject.toString();
JSONObject object = JSONObject.fromObject(temp);
System.out.println( "qq=" + object.get( "QQ" ));
}
}
|
Copy after login
Output result
1 2 3 4 5 6 7 8 | jsonObject:{ "username" : "huangwuyi" , "sex" : "男" , "QQ" : "413425430" , "Min.score" :99, "nickname" : "梦中心境" }
是否为数组:false, 是否为空:false, isNullObject:false
添加属性后的对象:{ "username" : "huangwuyi" , "sex" : "男" , "QQ" : "413425430" , "Min.score" :99, "nickname" : "梦中心境" , "address" : "福建省厦门市" }
{ "username" : "huangwuyi" , "sex" : "男" , "QQ" : "413425430" , "Min.score" :99, "nickname" : "梦中心境" , "address" : "福建省厦门市" , "jsonArray" :[ "this is a jsonArray value" , "another jsonArray value" ]}
返回一个JSONArray对象:[ "this is a jsonArray value" , "another jsonArray value" ]
结果={ "username" : "huangwuyi" , "sex" : "男" , "QQ" : "413425430" , "Min.score" :99, "nickname" : "梦中心境" , "address" : "福建省厦门市" , "jsonArray" :[ "this is a jsonArray value" , "another jsonArray value" ]}
username==>huangwuyi
qq=413425430
|
Copy after login
2.2.Example 2.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 | package jsontest;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
public class JSONTest {
public static void main(String args[])
{
JSONObject jsonObj0 = new JSONObject();
JSONObject jsonObj = new JSONObject();
JSONObject jsonObj2 = new JSONObject();
JSONObject jsonObj3 = new JSONObject();
JSONArray jsonArray = new JSONArray();
jsonObj0.put( "name0" , "zhangsan" );
jsonObj0.put( "sex1" , "female" );
System.out.println( "jsonObj0:" +jsonObj0);
jsonObj.put( "name" , "xuwei" );
jsonObj.put( "sex" , "male" );
System.out.println( "jsonObj:" +jsonObj);
jsonObj2.put( "item0" , jsonObj0);
jsonObj2.put( "item1" , jsonObj);
System.out.println( "jsonObj2:" +jsonObj2);
jsonObj3.element( "j3" , jsonObj2);
System.out.println( "jsonObj3:" +jsonObj3);
jsonArray.add(jsonObj);
System.out.println( "jsonArray:" +jsonArray);
JSONObject jsonObj4 = new JSONObject();
jsonObj4.element( "weather" , jsonArray);
System.out.println( "jsonObj4:" +jsonObj4);
}
}
|
Copy after login
Output result:
1 2 3 4 5 6 7 8 9 10 11 | jsonObj0:{ "name0" : "zhangsan" , "sex1" : "female" }
jsonObj:{ "name" : "xuwei" , "sex" : "male" }
jsonObj2:{ "item0" :{ "name0" : "zhangsan" , "sex1" : "female" }, "item1" :{ "name" : "xuwei" , "sex" : "male" }}
jsonObj3:{ "j3" :{ "item0" :{ "name0" : "zhangsan" , "sex1" : "female" }, "item1" :{ "name" : "xuwei" , "sex" : "male" }}}
jsonArray:[{ "name" : "xuwei" , "sex" : "male" }]
jsonObj4:{ "weather" :[{ "name" : "xuwei" , "sex" : "male" }]}
|
Copy after login
The above js method of receiving and converting array objects in Java is all that the editor has shared with you The content is complete, I hope it can give you a reference, and I also hope that everyone will support the PHP Chinese website.
For more articles related to methods of js receiving and converting array objects in Java, please pay attention to the PHP Chinese website!