Jsonobject and Gson detailed

Source: Internet
Author: User

Reference documents: http://www.cnblogs.com/xwdreamer/archive/2011/12/16/2296904.html
First, Jsonobject

1.JAR Package Introduction

To enable the program to run, the Json-lib package must be introduced, and the Json-lib package 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.4-jdk15.jar
    7. Json-lib-2.4-jdk15-javadoc.jar
    8. Json-lib-2.4-jdk15-sources.jar
2.JSONObject Object Use

The Json-lib package is a Beans,collections,maps,java arrays and XML and JSON transforms each other's packages. In this example, we will use the Jsonobject class to create the Jsonobject object, and then we print the values of those objects. In order to use the Jsonobject object, we will introduce the "Net.sf.json" package. To add elements to an object, we use the put () method.

[Java]View Plaincopy
  1. Public static User Getuserfromjson (String json) {
  2. User user = new user ();
  3. try {
  4. Jsonobject obj = Jsonobject.fromobject (JSON);
  5. User.setuserid (Obj.getint (Tables.User.Fields.USERID));
  6. User.setaccount (obj.getstring (Tables.User.Fields.ACCOUNT));
  7. User.setpassword (obj.getstring (Tables.User.Fields.PASSWORD));
  8. User.setgender (Obj.getint (Tables.User.Fields.GENDER));
  9. User.setpermission (Obj.getint (Tables.User.Fields.PERMISSION));
  10. User.setname (obj.getstring (Tables.User.Fields.NAME));
  11. User.setremark (obj.getstring (Tables.User.Fields.REMARK));
  12. User.setcontact (obj.getstring (Tables.User.Fields.CONTACT));
  13. return user;
  14. } catch (Exception ex) {
  15. Ex.printstacktrace ();
  16. }
  17. return null;
  18. }

Second, Gson processing JSON format data

1.Object to JSON format

[Java]View Plaincopy
  1. Public String Getallvideosbyjson () throws Exception {
  2. list<video> videos = Service.getallvideos ();
  3. StringBuilder sb = new StringBuilder ();
  4. //gson Gson = new Gson ();
  5. Gson Gson = new Gson ();
  6. String result = Gson.tojson (videos);
  7. return result;
  8. }

string in 2.json format becomes video object

[Java]View Plaincopy
    1. Public static list<video> getjsonallvideos_2 () throws Exception {
    2. String path = "Http://192.168.2.107:8080/VideoWeb/VideoServlet?format=json&type=get";
    3. URL url = new URL (path);
    4. HttpURLConnection conn = (httpurlconnection) url.openconnection ();
    5. Conn.setreadtimeout (5 * 1000);
    6. Conn.setrequestmethod ("GET");
    7. InputStream instream = Conn.getinputstream ();
    8. byte[] data = Streamtool.readinputstream (instream);
    9. String json = new string (data);
    10. Type typelist = new typetoken<arraylist<video>> () {
    11. }.gettype ();
    12. Gson Gson = new Gson ();
    13. arraylist<video> videos = Gson.fromjson (JSON, typelist);
    14. return videos;
    15. }

Jsonobject and Gson detailed

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.