Java Pain Learning Road [II]---jsonobject use

Source: Internet
Author: User

First, Strut2 must introduce the package

To enable a program to run must introduce a json-lib package, the Json-lib package also relies on the following jar packages:

1.commons-lang.jar

2.commons-beanutils.jar

3.commons-collections.jar

4.commons-logging.jar

5.ezmorph.jar

6.json-lib-2.2.2-jdk15.jar

Of course, in addition to these packages, the STRUT2 base package has to be introduced

Struts2-core-2.1.6.jar
Freemarker-2.3.13.jar
Commons-logging-1.0.4.jar
Ognl-2.6.11.jar
Xwork-2.1.2.jar
Commons-fileupload-1.2.1.jar

Note: The package must introduce the full, otherwise the Jsonobject object cannot be created, and there is no exception thrown, the program does not respond.

Second, user class

Create the Com.ljq.action package under the Engineering src directory and place the user class under this package:



third, Jsonaction class

In the Engineering src directory to create com.ljq.action package, put the Jsonaction class under this package, while inheriting the Actionsupport class, the code is as follows:




iv. configuration of Strut2.xml

Strut2.xml placed in the SRC directory, the configuration content is as follows:



Five, index.jsp write, remember to introduce jquery




Six, web.xml configuration



Vii. Other types of usage


import net.sf.json.jsonarray;   import net.sf.json.jsonobject;      public class jsonobjectsample {               //Create Jsonobject Objects        private static JSONObject  Createjsonobject () {           jsonobject jsonobject  = new jsonobject ();           jsonobject.put ( "Name",  "Kevin";           jsonobject.put ("Max.score")  new integer ());           jsonobject.put (" Min.score ",  new integer (m));            Jsonobject.put ("nickname",  "Picglet");           return  jsonObject;       }       public static void main (String[]  args)  {           jsonobject jsonobject =  jsonobjectsample.createjsonobject ();           // Output Jsonobject object            system.out.println ("jsonObject==> "+jsonobject);                   Type of     //interpretation output object            boolean  Isarray = jsonobject.isarray ();           boolean  isempty = jsonobject.isempty ();            Boolean isnullobject = jsonobject.isnullobject ();            systEm.out.println ("IsArray: +isarray+"  isempty: "+isempty+"  isnullobject: "+isnullobject");                      // Add Properties            jsonobject.element ("Address",  "swap  Lake ");           system.out.println (" object ==> after adding attribute "+ Jsonobject);                       //returns a Jsonarray object            jsonarray  jsonarray = new jsonarray ();            Jsonarray.add (0,  "This is a jsonarray value");            jsonarray.add (1, "Another jsonarray value");          &Nbsp; jsonobject.element ("Jsonarray",  jsonarray);            jsonarray array = jsonobject.getjsonarray ("Jsonarray");            system.out.println ("Returns a Jsonarray object:" +array);            //value after adding Jsonarray             //{"name": "Kevin", "Max.score": "," Min.score ":", "nickname": "Picglet", "Address": "Swap lake",            //"Jsonarray": ["this is a jsonarray  Value "," Another jsonarray value "]}            System.out.println (jsonobject);                       //returns a string based on key             string jsonstrIng = jsonobject.getstring ("name");            System.out.println ("jsonstring==>" +jsonstring);       }  }  


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.