使用jackson解析二维数组

maven依赖

        <dependency>
            <groupId>org.codehaus.jacksongroupId>
            <artifactId>jackson-allartifactId>
            <version>1.8.5version>
        dependency>

java代码

package com.hua.jackson;

import org.codehaus.jackson.JsonNode;
import org.codehaus.jackson.map.ObjectMapper;

import java.io.IOException;

/**
 * 使用jackson解析二维数组
 * Created by limenghua on 2016/6/12.
 */
public class JacksonArrayTest {

    private static String jsonStr = "[[{\"sWeek\":\"sunday\",\"amPm\":\"am\",\"workState\":1,\"hospitalId\":\"H5465464\"},{\"sWeek\":\"monday\",\"amPm\":\"am\",\"workState\":1,\"hospitalId\":\"H5465464\"},{\"sWeek\":\"tuesday\",\"amPm\":\"am\",\"workState\":1,\"hospitalId\":\"H5465464\"},{\"sWeek\":\"wednesday\",\"amPm\":\"am\",\"workState\":1,\"hospitalId\":\"H5465464\"},{\"sWeek\":\"thursday\",\"amPm\":\"am\",\"workState\":1,\"hospitalId\":\"H5465464\"},{\"sWeek\":\"friday\",\"amPm\":\"am\",\"workState\":1,\"hospitalId\":\"H5465464\"},{\"sWeek\":\"saturday\",\"amPm\":\"am\",\"workState\":1,\"hospitalId\":\"H5465464\"}],[{\"sWeek\":\"sunday\",\"amPm\":\"pm\",\"workState\":1,\"hospitalId\":\"H5465464\"},{\"sWeek\":\"monday\",\"amPm\":\"pm\",\"workState\":1,\"hospitalId\":\"H5465464\"},{\"sWeek\":\"tuesday\",\"amPm\":\"pm\",\"workState\":1,\"hospitalId\":\"H5465464\"},{\"sWeek\":\"wednesday\",\"amPm\":\"pm\",\"workState\":1,\"hospitalId\":\"H5465464\"},{\"sWeek\":\"thursday\",\"amPm\":\"pm\",\"workState\":1,\"hospitalId\":\"H5465464\"},{\"sWeek\":\"friday\",\"amPm\":\"pm\",\"workState\":1,\"hospitalId\":\"H5465464\"},{\"sWeek\":\"saturday\",\"amPm\":\"pm\",\"workState\":1,\"hospitalId\":\"H5465464\"}]]";

    public static void main(String[] args) {
        parseArray();
    }

    public static void parseArray() {
        final ObjectMapper mapper = new ObjectMapper();
        try {
            final Scheduling[][] schedulings = mapper.readValue(new ObjectMapper()
                    .readValue(jsonStr, JsonNode.class), Scheduling[][].class);
            for (Scheduling[] dto : schedulings) {

                System.out.println("===========================");

                for (Scheduling scheduling : dto) {
                    // 打印实体类数据
                    System.out.println(scheduling);
                }// second for end

            }// first for end
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

实体类

package com.hua.jackson;

/**
 * Created by limenghua on 2016/6/12.
 */
public class Scheduling {

    private String sWeek;
    private String amPm;
    private String workState;
    private String hospitalId;

    public String getAmPm() {
        return amPm;
    }

    public void setAmPm(String amPm) {
        this.amPm = amPm;
    }

    public String getHospitalId() {
        return hospitalId;
    }

    public void setHospitalId(String hospitalId) {
        this.hospitalId = hospitalId;
    }

    public String getsWeek() {
        return sWeek;
    }

    public void setsWeek(String sWeek) {
        this.sWeek = sWeek;
    }

    public String getWorkState() {
        return workState;
    }

    public void setWorkState(String workState) {
        this.workState = workState;
    }

    @Override
    public String toString() {
        final StringBuffer sb = new StringBuffer("Scheduling{");
        sb.append("amPm='").append(amPm).append('\'');
        sb.append(", sWeek='").append(sWeek).append('\'');
        sb.append(", workState='").append(workState).append('\'');
        sb.append(", hospitalId='").append(hospitalId).append('\'');
        sb.append('}');
        return sb.toString();
    }
}

前端传递的json字符串

[
    [
        {
            "sWeek": "sunday",
            "amPm": "am",
            "workState": 1,
            "hospitalId": "H5465464"
        },
        {
            "sWeek": "monday",
            "amPm": "am",
            "workState": 1,
            "hospitalId": "H5465464"
        },
        {
            "sWeek": "tuesday",
            "amPm": "am",
            "workState": 1,
            "hospitalId": "H5465464"
        },
        {
            "sWeek": "wednesday",
            "amPm": "am",
            "workState": 1,
            "hospitalId": "H5465464"
        },
        {
            "sWeek": "thursday",
            "amPm": "am",
            "workState": 1,
            "hospitalId": "H5465464"
        },
        {
            "sWeek": "friday",
            "amPm": "am",
            "workState": 1,
            "hospitalId": "H5465464"
        },
        {
            "sWeek": "saturday",
            "amPm": "am",
            "workState": 1,
            "hospitalId": "H5465464"
        }
    ],
    [
        {
            "sWeek": "sunday",
            "amPm": "pm",
            "workState": 1,
            "hospitalId": "H5465464"
        },
        {
            "sWeek": "monday",
            "amPm": "pm",
            "workState": 1,
            "hospitalId": "H5465464"
        },
        {
            "sWeek": "tuesday",
            "amPm": "pm",
            "workState": 1,
            "hospitalId": "H5465464"
        },
        {
            "sWeek": "wednesday",
            "amPm": "pm",
            "workState": 1,
            "hospitalId": "H5465464"
        },
        {
            "sWeek": "thursday",
            "amPm": "pm",
            "workState": 1,
            "hospitalId": "H5465464"
        },
        {
            "sWeek": "friday",
            "amPm": "pm",
            "workState": 1,
            "hospitalId": "H5465464"
        },
        {
            "sWeek": "saturday",
            "amPm": "pm",
            "workState": 1,
            "hospitalId": "H5465464"
        }
    ]
]

你可能感兴趣的:(Java,java,二维数组,json)