How js receives and converts array objects in Java

Source: Internet
Author: User
The following small series will bring you a method for js to receive and convert array objects in Java. I think this is quite good. Now I will share it with you and give you a reference. Let's take a look at the small Editor. To issue commands to the ocx control during project development, we need to get the objects in java in js, and then combine them into a format... When an object is an array, it is easy.

At first, I thought there was a simple way to directly convert the content. Later I found that it was not possible. On the Internet, there was no bridge between js and java. So:

My solution is: at the action layer, the java object array is converted into a Json string, and in js, json is converted into an array object.

1. Convert the java object array into a Json string:

Two classes are used:

Net. sf. json. JSONObjectnet. sf. json. JSONArray // concatenates each vehicle object into an object in json format and is used to issue a JSONObject json = JSONObject command. fromObject (v); // v is the jsonArray object. add (json); // System. out. println (jsonArray. toString (); // used to issue the sendCommandList = jsonArray command. toString ();

Put it in the for loop.

2. js converts it to an object array:

// Note: The Json string is returned. // Note: Add escapeJavaScript = "false" escape = "false". Otherwise, the quotation marks in the Json string will be parsed as "var szJsonStr ='
 ';

Finally:

// Convert the Json string to the object array var addVehicleArray = eval (szJsonStr );

When using this function, you can directly use addVehicleArray [I]. vehicleType ......

Extension:

Use of JSONObject and JSONArray

1. JAR package Overview

To run a program, the JSON-lib package must be introduced. The JSON-lib package 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. Use JSONObject

The JSON-lib package is a package that converts beans, collections, maps, java arrays, XML, and JSON. In this example, we will use the JSONObject class to create JSONObject objects, and then print the values of these objects. To use a JSONObject, we need to introduce the "net. sf. json" package. To add elements to an object, we need to use the put () method.

2. 1. instance 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 mood "); Return jsonObject;} public static void main (String [] args) {JSONObject jsonObject = JSONObjectSample. createJSONObject (); // wait for the method to be called directly by class name + method // output jsonobject object System. out. println ("jsonObject:" + jsonObject); // specifies the type of the output object. boolean isArray = jsonObject. isArray (); boolean isEmpty = jsonObject. isEmpty (); boolean isNullObject = jsonObject. isNullObject (); System. out. println ("whether it is an array:" + isArray + ", whether it is null: "+ IsEmpty +", isNullObject: "+ isNullObject); // Add attributes and append elements to jsonObject. JsonObject. element ("address", "Xiamen City, Fujian Province"); System. out. println ("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); // A jsonArray JSONArray array = jsonObject is located behind the jsonObject. getJSONArray ("jsonArray"); System. out. println (jsonObject); System. out. println ("return a JSONArray object:" + array); // value after JSONArray is added // {"username": "huangwuyi", "sex": "male ", "qq":" 413425430 "," Min. score ": 99," nickname ":" Dream mood "," address ":" Xiamen City, Fujian Province "," jsonArray ": [" this is a jsonArray value ", "another jsonArray value"]} System. out. println ("result =" + jsonObject); // returns a String username = jsonObject based on the key. getString ("username"); System. out. println ("username =>" + username); // convert the character to JSONObject String temp = jsonObject. toString (); JSONObject object = JSONObject. fromObject (temp); // After conversion, return the System value based on the Key. out. println ("qq =" + object. get ("QQ "));}}

Output result

JsonObject: {"username": "huangwuyi", "sex": "male", "QQ": "413425430", "Min. score ": 99," nickname ":" Dream mood "} whether it is an array: false, whether it is null: false, isNullObject: false the object after adding the attribute: {" username ": "huangwuyi", "sex": "male", "QQ": "413425430", "Min. score ": 99," nickname ":" Dream mood "," address ":" Xiamen City, Fujian Province "} {" username ":" huangwuyi "," sex ":" male ", "qq":" 413425430 "," Min. score ": 99," nickname ":" Dream mood "," address ":" Xiamen City, Fujian Province "," jsonArray ": [" this is a jsonArray value ", "another jsonArray value"]} returns a JSONArray object: ["this is a jsonArray value", "another jsonArray value"] result = {"username": "huangwuyi ", "sex": "male", "QQ": "413425430", "Min. score ": 99," nickname ":" Dream mood "," address ":" Xiamen City, Fujian Province "," jsonArray ": [" this is a jsonArray value ", "another jsonArray value"]} username => huangwuyiqq = 413425430

2. instance 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 struct ("name0", "zhangsan"); created ("Sex1", "female"); System. out. println ("jsonObj0:" + jsonObj0); // create jsonObj1 jsonObj. put ("name", "xuwei"); jsonObj. put ("sex", "male"); System. out. println ("jsonObj:" + jsonObj); // create jsonObj2, which contains two entries: jsonObj0 and jsonObj1 jsonObj2.put ("item0", jsonObj0 ); jsonObj2.put ("item1", jsonObj); System. out. println ("jsonObj2:" + jsonObj2); // create jsonObj3 with only one entry. The content is jsonObj2 jsonObj3.element ("j3", jsonObj2); System. ou T. println ("jsonObj3:" + jsonObj3); // Add a JSONObject to JSONArray. 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 js method to receive and convert the array objects in Java is all the content shared by the editor. I hope to give you a reference and support for PHP.

For more articles about how js receives and converts array objects in Java, refer to PHP!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.