V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
V2EX 提问指南
xjtlujoe
V2EX  ›  问与答

GSON 解析 json 数组

  •  
  •   xjtlujoe · 2017-07-25 19:48:59 +08:00 · 1594 次点击
    这是一个创建于 2439 天前的主题,其中的信息可能已经有所发展或是发生改变。
    [  
       {  
          "id":"1",
          "cardtype":"",
          "customer":{  
             "id":"11",
             "custnum":"aa"
          },
          "department":{  
             "id":"201",
             "depnum":"dep1"
          }
       },
       {  
          "id":"2",
          "cardtype":"",
          "customer":{  
             "id":"11",
             "custnum":"aa"
          },
          "department":{  
             "id":"201",
             "depnum":"dep1"
          }
       },
       {  
          "id":"3",
          "cardtype":"",
          "customer":{  
             "id":"11",
             "custnum":"aa"
          },
          "department":{  
             "id":"201",
             "depnum":"dep1"
          }
       }
    ]
    

    json 格式如上,试过很多写法,都不大适合,求解。

    Type founder = new TypeToken<ArrayList<bean>>(){}.getType(); List<bean> founder = gson.fromJson(jsonstring, founder);

    这样的写法也不行。

    4 条回复    2017-07-28 14:42:49 +08:00
    xjtlujoe
        1
    xjtlujoe  
    OP
       2017-07-25 21:55:18 +08:00
    懂了 gson 的 mapping 只能用来解析两层内的数据,多层不适用
    cassia
        2
    cassia  
       2017-07-26 10:10:20 +08:00
    多层没问题
    cassia
        3
    cassia  
       2017-07-26 10:12:17 +08:00
    你的 bean 没写对

    import lombok.Getter;
    import lombok.Setter;
    @Getter
    @Setter
    public class OneResult {
    private String id;
    private String cardtype;
    private Dep department;
    private Customer customer;

    @Getter
    @Setter
    public class Dep {
    private String id;
    private String depnum;


    }

    @Getter
    @Setter
    public class Customer {
    private String id;
    private String custnum;
    }

    @Getter
    @Setter
    public class Department {
    private String id;
    private String depnum;
    }
    }
    xjtlujoe
        4
    xjtlujoe  
    OP
       2017-07-28 14:42:49 +08:00
    @cassia 用 Gsformat 自动写得 bean 这样写没试过
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   2784 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 28ms · UTC 11:55 · PVG 19:55 · LAX 04:55 · JFK 07:55
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.