Generation and parsing of JSON data in Java __java

Source: Internet
Author: User
Tags object object
A. JSON understanding

1.json (JavaScript Object notation): JavaScript Object notation, which uses the syntax of JavaScript objects to represent object data, is a format for storing and transferring data, and its advantages are easy to read, easy to parse, It has the advantage of being smaller, faster, and easier to parse than traditional XML. -See also the two for more details. How to generate JSON data using Java

1. Using the Jsonobject class in the JSON API in the Org.json jar package
The Maven dependencies of the jar package are as follows:

        <dependency>
            <groupId>org.json</groupId>
            <artifactId>json</artifactId>
            <version>20160810</version>
        </dependency>

The Java code is as follows:

        Object nullobject = null;
        Jsonobject wangxiaoer = new Jsonobject ();
        Wangxiaoer.put ("name", "Wangxiao");
        Wangxiaoer.put ("Age", 25.2);
        Wangxiaoer.put ("Birthday", "1990-01-01");
        Wangxiaoer.put ("School", "Blue Xiang");
        Wangxiaoer.put ("Major", new string[] {"Barber", "Excavator"});
        Wangxiaoer.put ("Has_girlfriend", false);
        Wangxiaoer.put ("House", Nullobject);
        Wangxiaoer.put ("Car", nullobject);
        Wangxiaoer.put ("comment", "This is an annotation");
        System.out.println (Wangxiaoer.tostring ());

The screenshot of the running effect in Eclipse is as follows:

After the JSON online editor has been formatted, the effect looks like this:

2. Use HashMap to generate JSON data, which uses one of the constructor methods of the Jsonobject class, New Jsonobject (), which is the interface of the map type for this constructed method
The Java code is as follows:

        Map<string,object> wangxiaoer=new hashmap<string,object> ();
        Object nullobject = null;
        Wangxiaoer.put ("name", "Wangxiao");
        Wangxiaoer.put ("Age", 25.2);
        Wangxiaoer.put ("Birthday", "1990-01-01");
        Wangxiaoer.put ("School", "Blue Xiang");
        Wangxiaoer.put ("Major", new string[] {"Barber", "Excavator"});
        Wangxiaoer.put ("Has_girlfriend", false);
        Wangxiaoer.put ("House", Nullobject);
        Wangxiaoer.put ("Car", nullobject);
        Wangxiaoer.put ("comment", "This is an annotation");
        System.out.println (New Jsonobject (wangxiaoer). toString ());

The results of the final eclipse run the same.

3. Using JavaBean to generate JSON data, this method also uses a constructor method of the Jsonobject class, New Jsonobject (), which is an instance of the JavaBean type.
The code is as follows:

        Diaosi wangxiaoer=new Diaosi ();
        Wangxiaoer.setname ("Wangxiao");
        Wangxiaoer.setage (25.2);
        Wangxiaoer.setbirthday ("1990-01-01");
        Wangxiaoer.setschool ("Blue Xiang");
        Wangxiaoer.setmajor (new string[] {"Barber", "Excavator"});
        Wangxiaoer.sethas_girlfriend (false);
        Wangxiaoer.setcar (null);
        Wangxiaoer.sethouse (null);
        Wangxiaoer.setcomment ("This is a note");
        System.out.println (New Jsonobject (wangxiaoer). toString ());

The final operating effect of the above three. JSON Data parsing

The Commons-io jar package under the Apache project group is used, and MAVEN relies on the following:

        <dependency>
            <groupId>commons-io</groupId>
            <artifactid>commons-io</ artifactid>
            <version>2.0</version>
        </dependency>

The code is as follows:
Diaosi JavaBean:

Package Bean;

Import java.io.Serializable; public class Diaosi implements Serializable {/** * */private static final long Serialversionuid = 1L
    ;
    private String name;
    private double age;
    Private String birthday;
    Private String School;
    Private Boolean has_girlfriend;
    Private Object car;
    Private Object House;
    Private string[] major;

    Private String comment;
    Public String GetName () {return name;
    public void SetName (String name) {this.name = name;
    Public double Getage () {return age;
    public void Setage (double age) {this.age = age;
    Public String Getbirthday () {return birthday;
    } public void Setbirthday (String birthday) {this.birthday = birthday;
    Public String Getschool () {return school;
    } public void Setschool (String school) {This.school = school; } public Boolean Ishas_girlfriend () {return has_girlfriend;
    } public void Sethas_girlfriend (Boolean has_girlfriend) {this.has_girlfriend = Has_girlfriend;
    Public Object Getcar () {return car;
    public void Setcar (Object car) {This.car = car;
    Public Object Gethouse () {return house;
    public void Sethouse (Object house) {this.house = house;
    String[] Getmajor () {return major;
    } public void Setmajor (string[] major) {this.major = major;
    Public String getcomment () {return comment;
    } public void Setcomment (String comment) {this.comment = comment;
 }

}

Wangxiaoer.json JSON data file:

{
    "name": "Wangxiao", "Age
    ": 25.2,
    "Birthday": "1990-01-01",
    "school": "Blue Xiang",
    "major": ["Barber", "excavator"],
    "Has_girlfriend": false,
    "house": null,
    "car": null, "
    comment": "This is a comment"
}

Read the content from the JSON data file and parse:

Returns the start of the package where the current class is located,
        file = new file (ReadJsonSample.class.getResource ("Wangxiaoer.json"). GetFile ());
        String content = fileutils.readfiletostring (file);
        Jsonobject jsonobject = new Jsonobject (content);
        if (!jsonobject.isnull ("name")) {
            System.out.println ("name is:" + jsonobject.getstring ("name"));
        }
        if (!jsonobject.isnull ("Age")) {
            System.out.println ("Ages is:" + jsonobject.getdouble);
        }
        if (!jsonobject.isnull ("major")) {
            Jsonarray Majorarray = Jsonobject.getjsonarray ("major");
            for (Object object:majorarray) {
                System.out.println (object.tostring ());
            }
        }

The results of the parsing are as follows:

Related Article

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.