JS receives and transforms the array object in Java method _javascript technique

Source: Internet
Author: User

In doing projects, to OCX control issued a command, it is necessary to get Java in the JS object, and then spelled a format, issued down ... It's easier when the object is one, but it's a bit of a hassle if the object is an array.

At first I thought there is a simple way, can directly carry out the transformation of content, and later found that not, the internet said JS and Java did not bridge the things, so it:

My solution is to convert the Java object array into a JSON string at the action layer, and in JS, convert JSON to an array object.

1. Converts a Java object array into a JSON string:

There are two classes to use:

Net.sf.json.JSONObject
Net.sf.json.JSONArray


//splicing each vehicle object into JSON-formatted object for command-issuing
jsonobject JSON = Jsonobject.fromobject (v); V is Object
jsonarray.add (JSON);
System.out.println (Jsonarray.tostring ());
Used for ordering
sendcommandlist = jsonarray.tostring ();

Put it in the for loop.

2.js converts it to an array of objects:

Note: The JSON string is available at this time.

//Note: To add escapejavascript= "false" Escape= "false", otherwise the quotes in the JSON string will be parsed as "

var szjsonstr = ' <s:property Escapejavascript= "false" Escape= "false" value= "Sendcommandlist"/>;

At last:

Converts a JSON string to an object array
var Addvehiclearray = eval (szjsonstr);

In the use of the time, directly with Addvehiclearray[i].vehicletype;

Extended:

The use of Jsonobject and Jsonarray

1.JAR Package Introduction

To enable a program to run must introduce a json-lib package, the Json-lib package also relies 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 objects Use

The Json-lib package is a Beans,collections,maps,java arrays and XML and JSON-converted packages. In this case, we'll use the Jsonobject class to create the Jsonobject object, and then we'll print the values of those objects. In order to use the Jsonobject object, we introduce the "Net.sf.json" package. To add elements to an object, we 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 JSON
    Object = 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 state of Mind");
  return jsonobject; public static void Main (string[] args) {Jsonobject jsonobject = Jsonobjectsample.createjsonobject ();//static method, directly through


    Class name + method call//Output Jsonobject object System.out.println ("Jsonobject:" + jsonobject);
    The type of the interpretation output object Boolean IsArray = Jsonobject.isarray ();
    Boolean isempty = Jsonobject.isempty ();
    Boolean isnullobject = Jsonobject.isnullobject ();


    System.out.println ("Whether the array: + IsArray +", is empty: "+ IsEmpty +", Isnullobject: "+ isnullobject);"
    Add an attribute and append the element after jsonobject. Jsonobject.element ("Address", "Xiamen, Fujian Province");


    System.out.println ("Add the object after the attribute:" + jsonobject);
    Returns 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);
    In the back of the Jsonobject a jsonarray jsonarray array = Jsonobject.getjsonarray ("Jsonarray");
    
    
    System.out.println (Jsonobject);
    System.out.println ("Returns a Jsonarray object:" + array); Add Jsonarray Value//{"username": "Huangwuyi", "Sex": "Male", "QQ": "413425430", "Min.score": "," nickname ":" Dream of Mood "," address ": Xiamen, Fujian Province", "Jsonarray": ["This is a Jsonarray value", "another Jsonarray value"]} System.out.println ("result =" + Jsonobject)


    ;
    Returns a string username = jsonobject.getstring ("username") based on key;


    System.out.println ("username==>" + username);
    Converts 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 results

jsonobject:{"username": "Huangwuyi", "Sex": "Male", "QQ": "413425430", "Min.score": "," nickname ":" The Mood of the Dream "}
is an array: False, Null: false, Isnullobject:false
the object after adding the attribute: {"username": "Huangwuyi", "Sex": "Male", "QQ": "413425430", "Min.score ":", "nickname": "Dream Mood", "Address": "Xiamen City, Fujian province"}
{"username": "Huangwuyi", "Sex": "Male", "QQ": "413425430", "Min.score" : "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 ":", "nickname": "Dream Mood", "Address": "Xiamen City, Fujian Province", "Jsonarray": ["This is a Jsonarray value", " Another Jsonarray value "]}
Username==>huangwuyi
qq=413425430

2.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 the JsonObj2, which contains two entries, which are jsonobj0,jsonobj1 jsonobj2.put ("Item0", jsonObj0) respectively;
    Jsonobj2.put ("Item1", jsonobj);
    
    System.out.println ("JsonObj2:" +jsonobj2);
    Create JSONOBJ3, only one entry, content for JsonObj2 jsonobj3.element ("J3", jsonObj2);
  
    System.out.println ("jsonObj3:" +jsonobj3); Add the Jsonobject object to the Jsonarray. Found Jsonarray and Jsonobject.The difference is that Jsonarray is more jsonobject than the middle bracket [] jsonarray.add (jsonobj);
    
    System.out.println ("Jsonarray:" +jsonarray);
    Jsonobject jsonObj4 = new Jsonobject ();
    Jsonobj4.element ("Weather", jsonarray);
  System.out.println ("JsonObj4:" +jsonobj4); }
}

Output results:

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 to receive and transform the array object in Java is a small series to share all the content, hope to give you a reference, but also hope that we support cloud habitat community.

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.