Js receives and converts array objects in Java

Source: Internet
Author: User
When creating a project, to issue a command to the ocx control, you need to get the objects in java in js, and then combine them into a format and issue them... When an object is an array, it is easy. At first, I thought there was a simple way to directly perform content... SyntaxHighlighter. all ();

When creating a project, to issue a command to the ocx control, you need to get the objects in java in js, and then combine them into a format and issue them... 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. JSONObject
Net. sf. json. JSONArray

// Concatenates each vehicle object into an object in json format for command delivery
JSONObject json = JSONObject. fromObject (v); // v is the object
JsonArray. add (json );
// System. out. println (jsonArray. toString ());
// Used for command issuing
SendCommandList = jsonArray. 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"

Var szJsonStr =' ';


Finally:

// Convert a Json string to an 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 a JSONObject
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 the jsonobject object
System. out. println ("jsonObject:" + jsonObject );


// Interpret 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 empty:" + isEmpty
+ ", IsNullObject:" + isNullObject );


// Add attributes and add elements to the end of jsonObject.
JsonObject. element ("address", "Xiamen City, Fujian Province ");
System. out. println ("object after adding attribute:" + 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 );
// Build a jsonArray behind the jsonObject
JSONArray array = 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 based on the key.
String username = jsonObject. getString ("username ");
System. out. println ("username =>" + username );


// Convert the character to JSONObject
String temp = jsonObject. toString ();
JSONObject object = JSONObject. fromObject (temp );
// Return value based on Key after conversion
System. out. println ("qq =" + object. get ("QQ "));


}


}
Output result


JsonObject: {"username": "huangwuyi", "sex": "male", "QQ": "413425430", "Min. score ": 99," nickname ":" Dream mood "}
Array: false, empty: false, isNullObject: false
Object after adding 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 ==> huangwuyi
Qq = 1, 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
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, 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. out. 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"}]}

Author: angus_17
 

 

 

 

 

 

 


 

Related Article

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.