NutzCN Logo
问答 Json toJson 对深层次解析会出现NULL
发布于 101天前 作者 loveexception 232 次浏览 复制 上一个帖子 下一个帖子
标签:
    @At
    @Ok("json")
    public Location  treeObject(@Param("id") String id) {
        List<Location> locations =  locationService.query();
        //System.out.println(Json.toJson(locations));
         Location root = locationService.zip(locations);

        System.out.println(Json.toJson(root));
        root = root.getChildren().get(0);
        System.out.println(Json.toJson(root));
        root = root.getChildren().get(0);
        System.out.println(Json.toJson(root));
        root = root.getChildren().get(0);
        System.out.println(Json.toJson(root));

        return root;
    }

以下是打印结果
为什么 会有NULL

{
   "deptid": "100",
   "desription": "65432",
   "lng": 1.0,
   "lat": 1.0,
   "parentId": "0",
   "ancestors": "0",
   "level": "1",
   "orderNum": 0,
   "children": [{
      "deptid": "100",
      "desription": "3211",
      "lng": 0.0,
      "lat": 0.0,
      "parentId": "100000",
      "ancestors": "0,100000",
      "level": "2",
      "orderNum": 0,
      "children": null,
      "cnName": "天津",
      "enName": "33",
      "id": "1c519cedda624a4282e4a94a64c06fe6",
      "status": "true",
      "delFlag": "false",
      "createBy": "1",
      "createTime": "2019-07-05 18:35:30",
      "updateBy": "1",
      "updateTime": "2019-07-05 18:35:30"
   }],
   "cnName": "中国",
   "enName": "china",
   "id": "100000",
   "status": "true",
   "delFlag": "false",
   "createBy": "1",
   "createTime": "2019-06-22 10:31:13",
   "updateBy": "1",
   "updateTime": "2019-06-22 10:31:50"
}
{
   "deptid": "100",
   "desription": "3211",
   "lng": 0.0,
   "lat": 0.0,
   "parentId": "100000",
   "ancestors": "0,100000",
   "level": "2",
   "orderNum": 0,
   "children": [null],
   "cnName": "天津",
   "enName": "33",
   "id": "1c519cedda624a4282e4a94a64c06fe6",
   "status": "true",
   "delFlag": "false",
   "createBy": "1",
   "createTime": "2019-07-05 18:35:30",
   "updateBy": "1",
   "updateTime": "2019-07-05 18:35:30"
}
{
   "deptid": "100",
   "desription": "3211",
   "lng": 0.0,
   "lat": 0.0,
   "parentId": "1c519cedda624a4282e4a94a64c06fe6",
   "ancestors": "0,100000,1c519cedda624a4282e4a94a64c06fe6",
   "level": "3",
   "orderNum": 0,
   "children": [null],
   "cnName": "方言公司",
   "enName": "43432",
   "id": "88b83333ea6d4ec9b038584ba8dfa270",
   "status": "true",
   "delFlag": "false",
   "createBy": "1",
   "createTime": "2019-07-05 18:35:44",
   "updateBy": "1",
   "updateTime": "2019-07-05 18:35:44"
}
{
   "deptid": "100",
   "desription": "3211",
   "lng": 0.0,
   "lat": 0.0,
   "parentId": "88b83333ea6d4ec9b038584ba8dfa270",
   "ancestors": "0,100000,1c519cedda624a4282e4a94a64c06fe6,88b83333ea6d4ec9b038584ba8dfa270",
   "level": "4",
   "orderNum": 0,
   "cnName": "会议室二",
   "enName": "43432",
   "id": "7d81a1a70bdd4d669b644106b1c5d14f",
   "status": "true",
   "delFlag": "false",
   "createBy": "1",
   "createTime": "2019-07-05 18:35:56",
   "updateBy": "1",
   "updateTime": "2019-07-05 18:35:56"
}

10 回复
{
   "deptid": "100",
   "desription": "3211",
   "lng": 0.0,
   "lat": 0.0,
   "parentId": "1c519cedda624a4282e4a94a64c06fe6",
   "ancestors": "0,100000,1c519cedda624a4282e4a94a64c06fe6",
   "level": "3",
   "orderNum": 0,
   "children": [null],//这里的NULL 是怎么出来的?
   "cnName": "方言公司",
   "enName": "43432",
   "id": "88b83333ea6d4ec9b038584ba8dfa270",
   "status": "true",
   "delFlag": "false",
   "createBy": "1",
   "createTime": "2019-07-05 18:35:44",
   "updateBy": "1",
   "updateTime": "2019-07-05 18:35:44"
}

是不是同一个对象多次引用了

这个不行么?

例如子对象就是父对象

这个应该没有。
这建的测试数据中没有这种数据。

一共五级数据,
每一级只有一个子

https://ws2.sinaimg.cn/large/006tNc79ly1g4pyl9svvkj31390o0aj9.jpg

一个引用是1454 ,一个引用是1431
内容中也有不一样的。
但还是进入了这个判断。
应该是BUG

{
    "deptid": "100",
    "desription": "65432",
    "lng": 1.0,
    "lat": 1.0,
    "parentId": "0",
    "ancestors": "0",
    "level": "1",
    "orderNum": 0,
    "children": [
    {
        "deptid": "100",
        "desription": "3211",
        "lng": 0.0,
        "lat": 0.0,
        "parentId": "100000",
        "ancestors": "0,100000",
        "level": "2",
        "orderNum": 0,
        "children": [
        {
            "deptid": "100",
            "desription": "3211",
            "lng": 0.0,
            "lat": 0.0,
            "parentId": "1c519cedda624a4282e4a94a64c06fe6",
            "ancestors": "0,100000,1c519cedda624a4282e4a94a64c06fe6",
            "level": "3",
            "orderNum": 0,
            "children": [
            {
                "deptid": "100",
                "desription": "3211",
                "lng": 0.0,
                "lat": 0.0,
                "parentId": "88b83333ea6d4ec9b038584ba8dfa270",
                "ancestors": "0,100000,1c519cedda624a4282e4a94a64c06fe6,88b83333ea6d4ec9b038584ba8dfa270",
                "level": "4",
                "orderNum": 0,
                "cnName": "会议室二",
                "enName": "43432",
                "id": "7d81a1a70bdd4d669b644106b1c5d14f",
                "status": "true",
                "delFlag": "false",
                "createBy": "1",
                "createTime": "Jul 5, 2019 6:35:56 PM",
                "updateBy": "1",
                "updateTime": "Jul 5, 2019 6:35:56 PM"
            }],
            "cnName": "方言公司",
            "enName": "43432",
            "id": "88b83333ea6d4ec9b038584ba8dfa270",
            "status": "true",
            "delFlag": "false",
            "createBy": "1",
            "createTime": "Jul 5, 2019 6:35:44 PM",
            "updateBy": "1",
            "updateTime": "Jul 5, 2019 6:35:44 PM"
        }],
        "cnName": "天津",
        "enName": "33",
        "id": "1c519cedda624a4282e4a94a64c06fe6",
        "status": "true",
        "delFlag": "false",
        "createBy": "1",
        "createTime": "Jul 5, 2019 6:35:30 PM",
        "updateBy": "1",
        "updateTime": "Jul 5, 2019 6:35:30 PM"
    }],
    "cnName": "中国",
    "enName": "china",
    "id": "100000",
    "status": "true",
    "delFlag": "false",
    "createBy": "1",
    "createTime": "Jun 22, 2019 10:31:13 AM",
    "updateBy": "1",
    "updateTime": "Jun 22, 2019 10:31:50 AM"
}

暂时怀疑Location 的hash 是渣写法。。。。把二个不一样的OBJECT 算成一个了。
用了一下GSON 转成字串再转回来就OK 了

    @At
    @Ok("json")
    public Object  treeObject(@Param("id") String id) {
        List<Location> locations =  locationService.query();
         Location root = locationService.zip(locations);


//        System.out.println(Json.toJson(root));
//        root = root.getChildren().get(0);
//        System.out.println(Json.toJson(root));
//        root = root.getChildren().get(0);
//        System.out.println(Json.toJson(root));
//        root = root.getChildren().get(0);
//        System.out.println(Json.toJson(root));

        String json = new Gson().toJson(root);
         Object obj  = Json.fromJson(json);
        return obj;
    }

把hashcode改好呀...

多层继承,
BASE model 里内制的 TOSTRING EQUAL HASHCODE
如果要改可能会大改,
等我验证完再想别的办法。

添加回复
请先登陆
回到顶部