java中json数组如何转为对象

举例说明:
1、定义一个变量来接收json数据,getJsonDataOfLine()方法是我项目里边获取数据的方法,大家忽略就好

   String dailyPlankfLineOut = getJsonDataOfLine();

2.新建一个与json数据相匹配的对象,具体怎么建这里就不说了,可以手动,也可以利用工具来生成

public class DayPlanLine {
	private String ID;
    private String RUN_DATE;
    private String BZM;
    private String CC;
    private String BEGIN_TIME;
    private String END_TIME;
    private String SFZ;
    private String ZDZ;
    private String NEXT_CC;
    private String LJM;
    private CreateDateTimeBean CreateDateTime;

    public String getID() {
        return ID;
    }

    public void setID(String ID) {
        this.ID = ID;
    }

    public String getRUN_DATE() {
        return RUN_DATE;
    }

    public void setRUN_DATE(String RUN_DATE) {
        this.RUN_DATE = RUN_DATE;
    }

    public String getBZM() {
        return BZM;
    }

    public void setBZM(String BZM) {
        this.BZM = BZM;
    }

    public String getCC() {
        return CC;
    }

    public void setCC(String CC) {
        this.CC = CC;
    }

    public String getBEGIN_TIME() {
        return BEGIN_TIME;
    }

    public void setBEGIN_TIME(String BEGIN_TIME) {
        this.BEGIN_TIME = BEGIN_TIME;
    }

    public String getEND_TIME() {
        return END_TIME;
    }

    public void setEND_TIME(String END_TIME) {
        this.END_TIME = END_TIME;
    }

    public String getSFZ() {
        return SFZ;
    }

    public void setSFZ(String SFZ) {
        this.SFZ = SFZ;
    }

    public String getZDZ() {
        return ZDZ;
    }

    public void setZDZ(String ZDZ) {
        this.ZDZ = ZDZ;
    }

    public String getNEXT_CC() {
        return NEXT_CC;
    }

    public void setNEXT_CC(String NEXT_CC) {
        this.NEXT_CC = NEXT_CC;
    }

    public String getLJM() {
        return LJM;
    }

    public void setLJM(String LJM) {
        this.LJM = LJM;
    }

    public CreateDateTimeBean getCreateDateTime() {
        return CreateDateTime;
    }

    public void setCreateDateTime(CreateDateTimeBean CreateDateTime) {
        this.CreateDateTime = CreateDateTime;
    }

    public static class CreateDateTimeBean {
        private int TimezoneOffset;
        private boolean IsValidDateTime;
        private int Year;
        private int Month;
        private int Day;
        private int Hour;
        private int Minute;
        private int Second;
        private int Millisecond;
        private int Microsecond;
        private boolean IsNull;
        private String Value;

        public int getTimezoneOffset() {
            return TimezoneOffset;
        }

        public void setTimezoneOffset(int TimezoneOffset) {
            this.TimezoneOffset = TimezoneOffset;
        }

        public boolean isIsValidDateTime() {
            return IsValidDateTime;
        }

        public void setIsValidDateTime(boolean IsValidDateTime) {
            this.IsValidDateTime = IsValidDateTime;
        }

        public int getYear() {
            return Year;
        }

        public void setYear(int Year) {
            this.Year = Year;
        }

        public int getMonth() {
            return Month;
        }

        public void setMonth(int Month) {
            this.Month = Month;
        }

        public int getDay() {
            return Day;
        }

        public void setDay(int Day) {
            this.Day = Day;
        }

        public int getHour() {
            return Hour;
        }

        public void setHour(int Hour) {
            this.Hour = Hour;
        }

        public int getMinute() {
            return Minute;
        }

        public void setMinute(int Minute) {
            this.Minute = Minute;
        }

        public int getSecond() {
            return Second;
        }

        public void setSecond(int Second) {
            this.Second = Second;
        }

        public int getMillisecond() {
            return Millisecond;
        }

        public void setMillisecond(int Millisecond) {
            this.Millisecond = Millisecond;
        }

        public int getMicrosecond() {
            return Microsecond;
        }

        public void setMicrosecond(int Microsecond) {
            this.Microsecond = Microsecond;
        }

        public boolean isIsNull() {
            return IsNull;
        }

        public void setIsNull(boolean IsNull) {
            this.IsNull = IsNull;
        }

        public String getValue() {
            return Value;
        }

        public void setValue(String Value) {
            this.Value = Value;
        }
    }
}

3.新建list集合接收

List list = new ArrayList(JSONArray.parseArray(dailyPlankfLineOut, DayPlanLine.class));

这里jsonArray需要com.alibaba.fastjson.JSONArray的jar包

总结:总之就是使用jsonArray.parseArray将json数据转换为list对象,然后我们可以对其进行操作。

附加内容:json字符串转对象,jar包也是com.alibaba.fastjson.JSONArray

EmerInsyDetailPo emergencyInsyDetailPo = new EmerInsyDetailPo();
emergencyInsyDetailPo = JSON.parseObject(content, EmerInsyDetailPo.class);

你可能感兴趣的:(java基础,json,java,JSONArray)