V2EX = way to explore
V2EX 是一个关于分享和探索的地方
Sign Up Now
For Existing Member  Sign In
xjtlujoe
V2EX  ›  问与答

GSON 解析 json 数组

  •  
  •   xjtlujoe · Jul 25, 2017 · 2356 views
    This topic created in 3208 days ago, the information mentioned may be changed or developed.
    [  
       {  
          "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 replies    2017-07-28 14:42:49 +08:00
    xjtlujoe
        1
    xjtlujoe  
    OP
       Jul 25, 2017
    懂了 gson 的 mapping 只能用来解析两层内的数据,多层不适用
    cassia
        2
    cassia  
       Jul 26, 2017
    多层没问题
    cassia
        3
    cassia  
       Jul 26, 2017
    你的 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
       Jul 28, 2017
    @cassia 用 Gsformat 自动写得 bean 这样写没试过
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   1287 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 30ms · UTC 17:15 · PVG 01:15 · LAX 10:15 · JFK 13:15
    ♥ Do have faith in what you're doing.