When working on a project, if you want to issue commands to the ocx control, you need to get the objects in java in js, and then spell them into a format and issue them. . . It is simpler when an object is one, but it is a little troublesome if it is an array.
At first I thought there was a simple way to directly convert content, but later I found that it was not possible. The Internet said that js and java have no bridged things, so:
My solution is: at the action layer, convert the java object array into a Json string, and in js, convert json into an array object.
1. Convert java object array into a Json string:
Two classes are to be used:
net.sf.json.JSONObjectnet.sf.json.JSONArray//Split each vehicle object into an object in json format, and is used to issue JSONObject json = JSONObject.fromObject(v); //v that is, object jsonArray.add(json); //System.out.println(jsonArray.toString()); //Use to sendCommandList = jsonArray.toString();
Put it in a for loop.
2.js converts it into an array of objects:
//Note: What you get at this time is the Json string. //Note: EscapeJavaScript="false" escape="false" , otherwise the quotes in the Json string passed over will be parsed into "var szJsonStr = '<s:property escapeJavaScript="false" escape="false" value="sendCommandList" />';
at last:
//Convert Json string into object array var addVehicleArray = eval(szJsonStr);
When using it, just use addVehicleArray[i].vehicleType; it's fine. . . . . .
Extensions:
The use of JSONObject and JSONArray
1. Introduction to JAR package
To make the program run, the JSON-lib package must be introduced, which also depends on the following JAR packages:
commons-lang.jar
commons-beanutils.jar
commons-collections.jar
commons-logging.jar
ezmorph.jar
json-lib-2.2.2-jdk15.jar
2.JSONObject object usage
The JSON-lib package is a package that converts beans, collections, maps, java arrays and XML and JSON. In this case, we will use the JSONObject class to create JSONObject objects, and then we print the values of those objects. In order to use the JSONObject object, we want to introduce the "net.sf.json" package. To add elements to the object, we need to use the put() method.
2.1. Example 1
package jsontest;import net.sf.json.JSONArray;import net.sf.json.JSONObject;public class JSONObjectSample { // Create JSONObject object private static JSONObject createJSONObject() { JSONObject jsonObject = new JSONObject(); jsonObject.put("username", "huangwuyi"); jsonObject.put("sex", "male"); jsonObject.put("QQ", "413425430"); jsonObject.put("Min.score", new Integer(99)); jsonObject.put("nickname", "Dream Center"); return jsonObject; } public static void main(String[] args) { JSONObject jsonObject = JSONObjectSample.createJSONObject();// Wait for the method, call directly through the class name + method // Output jsonobject object System.out.println("jsonObject:" + jsonObject); // interpret the type of output object boolean isArray = jsonObject.isArray(); boolean isEmpty = jsonObject.isEmpty(); boolean isNullObject = jsonObject.isNullObject(); System.out.println("Is it an array:" + isArray + ", isNullObject:" + isNullObject); // Add attributes and append elements after jsonObject. jsonObject.element("address", "Xiamen City, Fujian Province"); System.out.println("A object after adding attributes: " + jsonObject); // Return a JSONArray object JSONArray jsonArray = new JSONArray(); jsonArray.add(0, "this is a jsonArray value"); jsonArray.add(1, "another jsonArray value"); jsonObject.element("jsonArray", jsonArray); //Reside a jsonArray behind jsonObject JSONArray array = jsonObject.getJSONArray("jsonArray"); System.out.println(jsonObject); System.out.println("Return a JSONArray object: " + array); // Add the value after JSONArray// {"username":"huangwuyi","sex":"Male","QQ":"413425430","Min.score":99,"nickname":"Dream center","address":"Xiamen City, Fujian Province","jsonArray":["this is a jsonArray value","another jsonArray value"]} System.out.println("result=" + jsonObject); // Return a string according to the key String username = jsonObject.getString("username"); System.out.println("username==>" + username); // Convert characters to JSONObject String temp = jsonObject.toString(); JSONObject object = JSONObject.fromObject(temp); // After conversion, return value based on Key System.out.println("qq=" + object.get("QQ")); }}Output result
jsonObject: {"username":"huangwuyi","sex":"male","QQ":"413425430","Min.score":99,"nickname":"Dream center"} is an array: false, is it empty: false, isNullObject:falseAddress object after adding attribute: {"username":"huangwuyi","sex":"male","QQ":"413425430","Min.score":99,"nickname":"Dream center","address":"Xiamen City, Fujian Province"}{"username":"huangwuyi","sex":"Male","QQ":"413425430","Min.score":99,"nickname":"Dream center","address":"Xiamen City, Fujian Province","jsonArray":["this is a jsonArray value","another jsonArray value"]} Return a JSONArray object: ["this is a jsonArray value","another jsonArray value"] Result={"username":"huangwuyi","sex":"male","QQ":"413425430","Min.score":99,"nickname":"Dream center","address":"Xiamen City, Fujian Province","jsonArray":["this is a jsonArray value","another jsonArray value"]}username==>huangwuyiqq=4134254302.2. Example 2.
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(); //Create jsonObj0 jsonObj0.put("name0", "zhangsan"); jsonObj0.put("sex1", "female"); System.out.println("jsonObj0:"+jsonObj0); //Create jsonObj1 jsonObj.put("name", "xuwei"); jsonObj.put("sex", "male"); System.out.println("jsonObj:"+jsonObj); //Create jsonObj2, contains two entries, the contents of the entries are jsonObj0, jsonObj1 jsonObj2.put("item0", jsonObj0); jsonObj2.put("item1", jsonObj); System.out.println("jsonObj2:"+jsonObj2); //Create jsonObj3, there is only one entry, the contents are jsonObj2 jsonObj3.element("j3", jsonObj2); System.out.println("jsonObj3:"+jsonObj3); //Add JSONObject object to JSONArray. Found that the difference between JSONArray and JSONObject is that JSONArray has more brackets than JSONObject[] jsonArray.add(jsonObj); System.out.println("jsonArray:"+jsonArray); JSONObject jsonObj4 = new JSONObject(); jsonObj4.element("weather", jsonArray); System.out.println("jsonObj4:"+jsonObj4); }}Output result:
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"}]}The above method of receiving and converting array objects in Java is all the content I share with you. I hope you can give you a reference and I hope you can support Wulin.com more.