Json工具Demo(一)

之前写过一篇关于json的,但是被csdn搞没了,重新写一篇。。

json概述

json是一种轻量级的数据交换格式。与其说是一种数据交换格式,不如说是他是一种编程语言。因为,它本身是JavaScript编程语言的一个子集。json建构于两种数据结构:
(1)键值对,有点类似map。
(2)有序列表,有点类似数组。
对象是一个无序的“键/值对”集合。一个对象以“{”(左大括号)开始,“}”(右大括号)结束。每个“名称”后跟一个“:”(冒号);“名称/值 对”之间使用“,”(逗号)分隔。
数组是值(value)的有序集合。一个数组以“[”(左中括号)开始,“]”(右中括号)结束。值之间使用“,”(逗号)分隔。
值(value)可以是双引号括起来的字符串(string)、数值(number)、true、false、 null、对象(object)或者数组(array)。这些结构可以嵌套。
一个例子如下:

{"AGE":-26,"BIRTHDAY":{"day":8,"month":8,"year":2000},"MALE":true,"NAME":"宝宝","PHONE":["15888888888","15222222222"]}

由上例可以看出,整个json被一对{}封闭,其中有若干个键值对,有的值本身又是一个json(如BIRTHDAY的值),有的值是一个有序列表(如PHONE的值)。

Jackson

json这么有好的对象表示形式,怎么才能将java对象顺利简单的转换成json串呢?这个过程可逆吗?即,json串可以方便转换成java对象吗?
上面的疑问都是肯定的,这就是Jackson框架做的事情。
Jackson的github:https://github.com/codehaus/jackson
下面直接上代码了。

json Demo

工程结构如下所示:
这里写图片描述

pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>org.fan.learn.json</groupId>
    <artifactId>jsonUtils</artifactId>
    <version>1.0-SNAPSHOT</version>

    <dependencies>

        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-api</artifactId>
            <version>1.7.10</version>
        </dependency>


        <!--json-->
        <dependency>
            <groupId>de.matrixweb.smaller</groupId>
            <artifactId>ant</artifactId>
            <version>0.8.4</version>
        </dependency>

        <dependency>
            <groupId>com.google.guava</groupId>
            <artifactId>guava</artifactId>
            <version>14.0.1</version>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <configuration>
                    <source>1.6</source>
                    <target>1.6</target>
                </configuration>
            </plugin>
        </plugins>
    </build>
</project>

1 Birthday.java

package org.fan.learn.json;

/**
 * Created by fan on 15-12-8.
 */
public class Birthday {
    private int year;
    private int month;
    private int day;

    public Birthday() {
        System.out.println("Birthday()");
    }

    public Birthday(int year, int month, int day) {
        this.year = year;
        this.month = month;
        this.day = day;
    }

    public int getYear() {
        System.out.println("getYear()");
        return year;
    }

    public void setYear(int year) {
        System.out.println("setYear()");
        this.year = year;
    }

    public int getMonth() {
        System.out.println("getMonth()");
        return month;
    }

    public void setMonth(int month) {
        System.out.println("setMonth()");
        this.month = month;
    }

    public int getDay() {
        System.out.println("getDay()");
        return day;
    }

    public void setDay(int day) {
        System.out.println("setDay()");
        this.day = day;
    }
}

2 User.java

package org.fan.learn.json;

import com.fasterxml.jackson.annotation.JsonAutoDetect;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.annotation.JsonRootName;

/**
 * Created by fan on 15-11-18.
 */
@JsonRootName(value = "FAN")
@JsonAutoDetect(fieldVisibility = JsonAutoDetect.Visibility.ANY)
public class User {
    @JsonProperty("NAME")
    private String name;
    @JsonProperty("AGE")
    private int age;
    @JsonProperty("PHONE")
    private String[] phone;
    @JsonProperty("BIRTHDAY")
    private Birthday birthday;
    @JsonProperty("MALE")
    private boolean male;


    //默认构造方法必须要有。如果没有其他构造方法,当然不用写,会自动给你添加默认构造方法
    public User() {
        System.out.println("User()");
    }

    public User(String name, int age, String[] phone) {
        this.name = name;
        this.age = age;
        this.phone = phone;
    }

    public User(String name, int age, String[] phone, Birthday birthday) {
        this.name = name;
        this.age = age;
        this.phone = phone;
        this.birthday = birthday;
    }

    public User(String name, int age, String[] phone, Birthday birthday, boolean male) {
        this.name = name;
        this.age = age;
        this.phone = phone;
        this.birthday = birthday;
        this.male = male;
    }

    //getter、setter方法不是必须要有的
//    public int getAge() {
//        System.out.println("getAge()");
//        return age;
//    }

//    public void setAge(int age) {
//        System.out.println("setAge()");
//        this.age = age;
//    }

    public String getName() {
        System.out.println("getName()");
        return name;
    }

    public void setName(String name) {
        System.out.println("setName()");
        this.name = name;
    }

    public String[] getPhone() {
        System.out.println("getPhone()");
        return phone;
    }

    public void setPhone(String[] phone) {
        System.out.println("setPhone()");
        this.phone = phone;
    }

    public Birthday getBirthday() {
        System.out.println("getBirthday()");
        return birthday;
    }

    public void setBirthday(Birthday birthday) {
        System.out.println("setBirthday()");
        this.birthday = birthday;
    }

    public boolean isMale() {
        System.out.println("isMale()");
        return male;
    }

    public void setMale(boolean male) {
        System.out.println("setMale()");
        this.male = male;
    }
}

3 UserList.java

package org.fan.learn.json;

import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.annotation.JsonRootName;

import java.util.List;

/**
 * Created by fan on 15-11-18.
 */
@JsonRootName(value = "AAAAA")
public class UserList {
    @JsonProperty("USERLIST")
    private List<User> userList;

    public UserList() {
        System.out.println("UserList()");
    }

    public UserList(List<User> userList) {
        this.userList = userList;
    }

    public List<User> getUserList() {
        System.out.println("getUserList()");
        return userList;
    }

    public void setUserList(List<User> userList) {
        System.out.println("setUserList()");
        this.userList = userList;
    }
}

4 UserMap.java

package org.fan.learn.json;

import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.annotation.JsonRootName;

import java.util.Map;

/**
 * Created by fan on 15-11-18.
 */
@JsonRootName(value = "BB")
public class UserMap {
    @JsonProperty("USERMAP")
    Map<String, User> userMap;

    public UserMap() {
        System.out.println("UserMap()");
    }

    public Map<String, User> getUserMap() {
        System.out.println("getUserMap()");
        return userMap;
    }

    public void setUserMap(Map<String, User> userMap) {
        System.out.println("setUserMap()");
        this.userMap = userMap;
    }
}

这个文章太长了,转到下篇Json工具Demo(二) 写重头戏JacksonUtil.java和测试的Main.java。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值