GSON 解析 json 数组

2017-07-25 19:48:59 +08:00
 xjtlujoe
[  
   {  
      "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);

这样的写法也不行。

1620 次点击
所在节点    问与答
4 条回复
xjtlujoe
2017-07-25 21:55:18 +08:00
懂了 gson 的 mapping 只能用来解析两层内的数据,多层不适用
cassia
2017-07-26 10:10:20 +08:00
多层没问题
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
2017-07-28 14:42:49 +08:00
@cassia 用 Gsformat 自动写得 bean 这样写没试过

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/377911

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX