Json字串转换成Java复杂对象
当Json字串需要生成复杂Java对象时,可参照如下代码映射生成。
需要如下jar包:
commons-beanutils-1.8.3.jar
commons-collections-3.2.1.jar
commons-lang-2.6.jar
commons-logging-1.1.1.jar
ezmorph-1.0.6.jar
json-lib-2.1-jdk15.jar
1 String jsonString = "
2 [
3 {"author":"7","id":358,"title":"More of us","pictures":[{"description":"","imgPath":"/cms/u/cms/www/201203/05150720ii67.jpg"}],"path":"ip"},
4 {"author":"6","id":357,"title":"Video Gallery","pictures":[{"description":"","imgPath":"/cms/u/cms/www/201203/05150657mgpk.jpg"}],"path":"ip"},
5 {"author":"5","id":356,"title":"WATS Fun","pictures":[{"description":"","imgPath":"/cms/u/cms/www/201203/05150608s68h.jpg"}],"path":"ip"},
6 {"author":"4","id":355,"title":"Health and Beauty Tips","pictures":[{"description":"","imgPath":"/cms/u/cms/www/201203/05150146mrqo.jpg"}],"path":"ip"},
7 {"author":"2","id":354,"title":"Watsons Card Members","pictures":[{"description":"","imgPath":"/cms/u/cms/www/201203/05145320s6u8.jpg"}],"path":"ip"},
8 {"author":"1","id":353,"title":"Only @ Watsons","pictures":[{"description":"","imgPath":"/cms/u/cms/www/201203/05145051bn9c.jpg"}],"path":"ip"},
9 {"author":"0","id":351,"title":"WATS HOT","pictures":[{"description":"","imgPath":"/cms/u/cms/www/201203/05143251ewdl.jpg"}],"path":"ip"},
10 {"author":"3","id":350,"title":"Pharmacy Services","pictures":[{"description":"","imgPath":"/cms/u/cms/www/201203/05143239whnz.jpg"}],"path":"ip"}
11 ]";
12
13 JSONArray ja = JSONArray.fromObject(jsonString);
14
15 Map<String, Class<pictures>> classMap = new HashMap<String, Class<pictures>>();
16 classMap.put("pictures", pictures.class);
17 List<Content> list = JSONArray.toList(ja, Content.class, classMap);
2 [
3 {"author":"7","id":358,"title":"More of us","pictures":[{"description":"","imgPath":"/cms/u/cms/www/201203/05150720ii67.jpg"}],"path":"ip"},
4 {"author":"6","id":357,"title":"Video Gallery","pictures":[{"description":"","imgPath":"/cms/u/cms/www/201203/05150657mgpk.jpg"}],"path":"ip"},
5 {"author":"5","id":356,"title":"WATS Fun","pictures":[{"description":"","imgPath":"/cms/u/cms/www/201203/05150608s68h.jpg"}],"path":"ip"},
6 {"author":"4","id":355,"title":"Health and Beauty Tips","pictures":[{"description":"","imgPath":"/cms/u/cms/www/201203/05150146mrqo.jpg"}],"path":"ip"},
7 {"author":"2","id":354,"title":"Watsons Card Members","pictures":[{"description":"","imgPath":"/cms/u/cms/www/201203/05145320s6u8.jpg"}],"path":"ip"},
8 {"author":"1","id":353,"title":"Only @ Watsons","pictures":[{"description":"","imgPath":"/cms/u/cms/www/201203/05145051bn9c.jpg"}],"path":"ip"},
9 {"author":"0","id":351,"title":"WATS HOT","pictures":[{"description":"","imgPath":"/cms/u/cms/www/201203/05143251ewdl.jpg"}],"path":"ip"},
10 {"author":"3","id":350,"title":"Pharmacy Services","pictures":[{"description":"","imgPath":"/cms/u/cms/www/201203/05143239whnz.jpg"}],"path":"ip"}
11 ]";
12
13 JSONArray ja = JSONArray.fromObject(jsonString);
14
15 Map<String, Class<pictures>> classMap = new HashMap<String, Class<pictures>>();
16 classMap.put("pictures", pictures.class);
17 List<Content> list = JSONArray.toList(ja, Content.class, classMap);
1 public class Content {
2 private String author;
3 private String id;
4 private String title;
5 private List<pictures> pictures;
6 private String path;
7 public String getAuthor() {
8 return author;
9 }
10 public void setAuthor(String author) {
11 this.author = author;
12 }
13 public String getId() {
14 return id;
15 }
16 public void setId(String id) {
17 this.id = id;
18 }
19 public String getTitle() {
20 return title;
21 }
22 public void setTitle(String title) {
23 this.title = title;
24 }
25 public List<pictures> getPictures() {
26 return pictures;
27 }
28 public void setPictures(List<pictures> pictures) {
29 this.pictures = pictures;
30 }
31 public String getPath() {
32 return path;
33 }
34 public void setPath(String path) {
35 this.path = path;
36 }
37 }
2 private String author;
3 private String id;
4 private String title;
5 private List<pictures> pictures;
6 private String path;
7 public String getAuthor() {
8 return author;
9 }
10 public void setAuthor(String author) {
11 this.author = author;
12 }
13 public String getId() {
14 return id;
15 }
16 public void setId(String id) {
17 this.id = id;
18 }
19 public String getTitle() {
20 return title;
21 }
22 public void setTitle(String title) {
23 this.title = title;
24 }
25 public List<pictures> getPictures() {
26 return pictures;
27 }
28 public void setPictures(List<pictures> pictures) {
29 this.pictures = pictures;
30 }
31 public String getPath() {
32 return path;
33 }
34 public void setPath(String path) {
35 this.path = path;
36 }
37 }
1 public class pictures {
2 private String description;
3 private String imgPath;
4
5 public String getDescription() {
6 return description;
7 }
8
9 public void setDescription(String description) {
10 this.description = description;
11 }
12
13 public String getImgPath() {
14 return imgPath;
15 }
16
17 public void setImgPath(String imgPath) {
18 this.imgPath = imgPath;
19 }
20 }