Jsonobject in Java

Source: Internet
Author: User

1. Introduction to jar packages

To run a program, the JSON-lib package must be introduced. The JSON-lib package depends on the following jar package:

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


II,

Use jsonobject



JSON-
The LIB package is a bean, collections, maps, Java Arrays
Packages that are converted to 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
Jsonobject. We will introduce the "net. SF. JSON" package. To add elements to an object, we need to use the put () method.

 

Java code
  1. Import
    Net. SF. JSON. jsonarray;
  2. Import
    Net. SF. JSON. jsonobject;
  3. Public
     
    Class
    Jsonobjectsample {
  4. // Create a jsonobject

  5. Private
     
    Static
    Jsonobject createjsonobject (){
  6. Jsonobject = new
    Jsonobject ();
  7. Jsonobject. Put ("name"
    ,
    "Kevin"
    );
  8. Jsonobject. Put ("Max. Score"
    ,
    New
    INTEGER (
    100
    ));
  9. Jsonobject. Put ("Min. Score"
    ,
    New
    INTEGER (
    50
    ));
  10. Jsonobject. Put ("nickname"
    ,
    "Picglet"
    );
  11. Return
    Jsonobject;
  12. }
  13. Public
     
    Static
     
    Void
    Main (string [] ARGs ){
  14. Jsonobject = jsonobjectsample. createjsonobject ();
  15. // Output the jsonobject object

  16. System. Out. println ("jsonobject =>"
    + Jsonobject );
  17. // Interpret the type of the output object

  18. Boolean
    Isarray = jsonobject. isarray ();
  19. Boolean
    Isempty = jsonobject. isempty ();
  20. Boolean
    Isnullobject = jsonobject. isnullobject ();
  21. System. Out. println ("isarray :"
    + Isarray +
    "Isempty :"
    + Isempty +
    "Isnullobject :"
    + Isnullobject );
  22. // Add attributes

  23. Jsonobject. element ("Address"
    ,
    "Swap Lake"
    );
  24. System. Out. println ("object after adding attribute =>"
    + Jsonobject );
  25. // Return a jsonarray object

  26. Jsonarray = new
    Jsonarray ();
  27. Jsonarray. Add (0
    ,
    "This is a jsonarray value"
    );
  28. Jsonarray. Add (1
    ,
    "Another jsonarray value"
    );
  29. Jsonobject. element ("jsonarray"
    , Jsonarray );
  30. Jsonarray array = jsonobject. getjsonarray ("jsonarray"
    );
  31. System. Out. println ("A jsonarray object is returned :"
    + Array );
  32. // Value after jsonarray is added

  33. // {"Name": "Kevin", "Max. score ": 100," Min. score ": 50," nickname ":" picglet "," Address ":" swap Lake ",

  34. // "Jsonarray": ["This Is A jsonarray value", "another jsonarray value"]}

  35. System. Out. println (jsonobject );
  36. // Returns a string based on the key.

  37. String jsonstring = jsonobject. getstring ("name"
    );
  38. System. Out. println ("jsonstring =>"
    + Jsonstring );
  39. }
  40. }
Import net. SF. JSON. jsonarray; import net. SF. JSON. jsonobject; public class jsonobjectsample {// create jsonobject object Private Static jsonobject createjsonobject () {jsonobject = new jsonobject (); jsonobject. put ("name", "Kevin"); jsonobject. put ("Max. score ", new INTEGER (100); jsonobject. put ("Min. score ", new INTEGER (50); jsonobject. put ("nickname", "picglet"); Return jsonobject;} public static void main (string [] ARGs) {jsonobject = jsonobjectsample. createjsonobject (); // output the jsonobject 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 ("isarray:" + isarray + "isempty:" + isempty + "isnullobject:" + isnullobject); // Add the jsonobject attribute. element ("Address", "swap Lake"); system. out. println ("added object =>" + jsonobject); // return a jsonarray object jsonarray = new jsonarray (); jsonarray. add (0, "This is a jsonarray value"); jsonarray. add (1, "another jsonarray value"); jsonobject. element ("jsonarray", jsonarray); jsonarray array = jsonobject. getjsonarray ("jsonarray"); system. out. println ("return a jsonarray object:" + array); // value after jsonarray is added // {"name": "Kevin", "Max. score ": 100," Min. score ": 50," nickname ":" picglet "," Address ":" swap Lake ", //" jsonarray ": [" This Is A jsonarray value ", "Another jsonarray value"]} system. out. println (jsonobject); // returns a string jsonstring = jsonobject based on the key. getstring ("name"); system. out. println ("jsonstring =>" + jsonstring );}}

 

After getting the jsonobject object, we can use its method. You can view its API. I will provide an online API

 

Http://json-lib.sourceforge.net/apidocs/jdk15/index.html

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.