The [Json-lib framework] makes conversion between Java objects, JSON, and XML no longer difficult __js

Source: Internet
Author: User

Json-lib can convert a Java object into a JSON-formatted string or convert a Java object into an XML-formatted document, either converting a JSON string into a Java object or converting an XML string into a Java object.

First, preparatory work

 

1, the first to go to the official Download Json-lib Toolkit

Download Address:

http://sourceforge.net/projects/json-lib/files/json-lib/json-lib-2.4/

The latest is the 2.4 version, which is used in the example V2.3;json-lib also requires the following dependent packages:

Jakarta Commons-lang 2.5

Jakarta commons-beanutils 1.8.0

Jakarta commons-collections 3.2.1

Jakarta commons-logging 1.1.1

Ezmorph 1.0.6

Official website: http://json-lib.sourceforge.net/

Then add the following jar package to the project:


Of course, you can also use the 2.4 json-lib library.

You can look at the official examples here:

Http://json-lib.sourceforge.net/usage.html

Because the examples in this presentation require JUnit tools, you will also need to add JUnit jar files, version 4.8.2 version, download address: https://github.com/KentBeck/junit/downloads

If you don't know what JSON is. Then you should be able to see http://www.json.org/json-zh.html.

2, the code to convert the JavaBean is as follows:

Package com.hoo.entity;
public class Student {
    private int id;
    private String name;
    Private String Email;
    Private String address;
    private birthday birthday;
    Setter, getter
    Public String toString () {
        return this.name + "#" + this.id + "#" + this.address + "#" + This.birthday + "#" + this.email;
    }
}

Birthday.java

Package com.hoo.entity;
public class Birthday {
    Private String birthday;
    
    Public birthday (String birthday) {
        Super ();
        This.birthday = Birthday;
    }
    Setter, getter
    Public birthday () {}
    
    @Override
    Public String toString () {
        return this.birthday;
    }
}

Notice that the getter and setter methods above are omitted and built by themselves.

3, the new Jsonlibtest test class, the basic code is as follows:

Package com.hoo.test;
Import java.lang.reflect.InvocationTargetException;
Import java.util.ArrayList;
Import java.util.Collection;
Import Java.util.Date;
Import Java.util.HashMap;
Import Java.util.Iterator;
Import java.util.List;
Import Java.util.Map;
Import Java.util.Set;
import Net.sf.json.JSON; 

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.