Skip to content
This repository has been archived by the owner on Oct 23, 2024. It is now read-only.

诡异的情况..JSON.toString 时,复杂嵌套对象里 List为空。。 #502

Closed
BlankRain opened this issue Feb 18, 2016 · 4 comments
Closed

Comments

@BlankRain
Copy link

预期与结果不一致.

fastjson 的结果: {"data":[{},{},{},{},{}],"head":["姓名","类型","状态","满意度","统计","时间"],"size":5}

预期的结果: {"data":[{"姓名":"A0","时间":"F0","满意度":"D0","状态":"C0","类型":"B0","统计":"E0"},{"姓名":"A1","时间":"F1","满意度":"D1","状态":"C1","类型":"B1","统计":"E1"},{"姓名":"A2","时间":"F2","满意度":"D2","状态":"C2","类型":"B2","统计":"E2"},{"姓名":"A3","时间":"F3","满意度":"D3","状态":"C3","类型":"B3","统计":"E3"},{"姓名":"A4","时间":"F4","满意度":"D4","状态":"C4","类型":"B4","统计":"E4"}],"head":["姓名","类型","状态","满意度","统计","时间"],"size":5}

直接贴代码,代码不复杂,三个类文件

建maven工程,用到的Maven依赖

<dependencies>
        <dependency>
            <groupId>net.sf.json-lib</groupId>
            <artifactId>json-lib</artifactId>
            <version>2.4</version>
            <classifier>jdk15</classifier>
        </dependency>

        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>fastjson</artifactId>
            <version>1.2.6</version>
        </dependency>
    </dependencies>

app.demo
---------FastJsonBugShow.java
---------ClassRoom.java
---------Main.java

放在app.demo包下.

FastJsonBugShow.java

package app.demo;

import java.util.List;

/**
 * 头,长度,数据
 */
public class FastJsonBugShow {
    public List<String> head;
    public int size;
    public List<?> data;

    public FastJsonBugShow() {
    }

    public List<String> getHead() {
        return head;
    }

    public void setHead(List<String> head) {
        this.head = head;
    }

    public int getSize() {
        return size;
    }

    public void setSize(int size) {
        this.size = size;
    }

    public List<?> getData() {
        return data;
    }

    public void setData(List<?> data) {
        this.data = data;
    }

    public FastJsonBugShow(List<String> head, int size, List<?> data) {
        this.head = head;
        this.size = size;
        this.data = data;
    }

    public String netsfJsonStr() {
        return net.sf.json.JSONObject.fromObject(this).toString();
    }

    public String fastJsonStr() {
        return com.alibaba.fastjson.JSON.toJSONString(this);
    }


}


ClassRoom.java

package app.demo;

import java.util.ArrayList;
import java.util.List;

public class ClassRoom {
    List<People> list = null;

    public static class People {

        private String 姓名;
        private String 类型;
        private String 状态;
        private String 满意度;
        private String 统计;
        private String 时间;

        static List<String> head() {
            List<String> h = new ArrayList<String>();

            h.add("姓名");
            h.add("类型");
            h.add("状态");
            h.add("满意度");
            h.add("统计");
            h.add("时间");
            return h;
        }

        public String get姓名() {
            return 姓名;
        }

        public void set姓名(String 姓名) {
            this.姓名 = 姓名;
        }

        public String get类型() {
            return 类型;
        }

        public void set类型(String 类型) {
            this.类型 = 类型;
        }

        public String get状态() {
            return 状态;
        }

        public void set状态(String 状态) {
            this.状态 = 状态;
        }

        public String get满意度() {
            return 满意度;
        }

        public void set满意度(String 满意度) {
            this.满意度 = 满意度;
        }

        public String get统计() {
            return 统计;
        }

        public void set统计(String 统计) {
            this.统计 = 统计;
        }

        public String get时间() {
            return 时间;
        }

        public void set时间(String 时间) {
            this.时间 = 时间;
        }

    }

    public void setList(List<People> list) {
        this.list = list;
    }

    @SuppressWarnings("rawtypes")
    public List<?> getList() {
        return this.list == null ? new ArrayList() : list;
    }

    public int size() {
        return list == null ? 0 : list.size();
    }

    public String fastJSONStr() {
        return new FastJsonBugShow(People.head(), this.size(), this.getList()).fastJsonStr();
    }

    public String netsfJSONStr() {
        return new FastJsonBugShow(People.head(), this.size(), this.getList()).netsfJsonStr();
    }
}

Main.java

package app.demo;

import java.util.ArrayList;
import java.util.List;

public class Main {

    public static void main(String[] args) {
        ClassRoom demoResult = new ClassRoom();
        demoResult.setList(demoData());// 设置数据
        System.out.println(demoResult.fastJSONStr());
        System.out.println(demoResult.netsfJSONStr());
    }

    public static List<app.demo.ClassRoom.People> demoData() {
        List<app.demo.ClassRoom.People> list = new ArrayList<app.demo.ClassRoom.People>();
        for (int i = 0; i < 5; i++) {
            app.demo.ClassRoom.People a1 = new app.demo.ClassRoom.People();
            a1.set姓名("A" + i);
            a1.set类型("B" + i);
            a1.set状态("C" + i);
            a1.set满意度("D" + i);
            a1.set统计("E" + i);
            a1.set时间("F" + i);
            list.add(a1);
        }
        return list;
    }
}


Main.java 运行结果:

{"data":[{},{},{},{},{}],"head":["姓名","类型","状态","满意度","统计","时间"],"size":5}

{"data":[{"姓名":"A0","时间":"F0","满意度":"D0","状态":"C0","类型":"B0","统计":"E0"},{"姓名":"A1","时间":"F1","满意度":"D1","状态":"C1","类型":"B1","统计":"E1"},{"姓名":"A2","时间":"F2","满意度":"D2","状态":"C2","类型":"B2","统计":"E2"},{"姓名":"A3","时间":"F3","满意度":"D3","状态":"C3","类型":"B3","统计":"E3"},{"姓名":"A4","时间":"F4","满意度":"D4","状态":"C4","类型":"B4","统计":"E4"}],"head":["姓名","类型","状态","满意度","统计","时间"],"size":5}

@wenshao
Copy link
Member

wenshao commented Feb 28, 2016

中文方法名,你强!可以使用@JSONField(name="类型")

@wenshao
Copy link
Member

wenshao commented Feb 28, 2016

已经修复,请等待1.2.8版本发布

@wenshao wenshao closed this as completed Feb 28, 2016
@wenshao
Copy link
Member

wenshao commented Feb 28, 2016

965d015

@BlankRain
Copy link
Author

哈哈哈,中文方法咋了? utf8 问题不大吧。。
其实,我一直期待一门汉语风格的编程语言...不是vb翻版出来的易语言那种...

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants