Gson解析_exc1
Posted Tears_fg
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Gson解析_exc1相关的知识,希望对你有一定的参考价值。
1 package cn.kjxy.GSON; 2 3 import java.util.List; 4 5 import com.google.gson.Gson; 6 import com.google.gson.reflect.TypeToken; 7 8 class DevelopmentDep { 9 private int no; 10 private String name; 11 private List<Deper> list; 12 13 public int getNo() { 14 return no; 15 } 16 17 public void setNo(int no) { 18 this.no = no; 19 } 20 21 public String getName() { 22 return name; 23 } 24 25 public void setName(String name) { 26 this.name = name; 27 } 28 29 public List<Deper> getList() { 30 return list; 31 } 32 33 public void setList(List<Deper> list) { 34 this.list = list; 35 } 36 37 @Override 38 public String toString() { 39 return "DevelopmentDep [no=" + no + ", name=" + name + ", list=" + list 40 + "]"; 41 } 42 43 } 44 45 class Deper { 46 private String name; 47 private double salary; 48 49 public String getName() { 50 return name; 51 } 52 53 public void setName(String name) { 54 this.name = name; 55 } 56 57 public double getSalary() { 58 return salary; 59 } 60 61 public void setSalary(double salary) { 62 this.salary = salary; 63 } 64 65 @Override 66 public String toString() { 67 return "Deper [name=" + name + ", salary=" + salary + "]"; 68 } 69 70 } 71 72 public class Demo1 { 73 public static void main(String[] args) { 74 // gson解析json数组 75 String json = "[{no:1001,name:‘研发部‘,list:[{name:‘李晨‘,salary:30000},{name:‘王思远‘,salary:3}]},{no:2,name:‘市场部‘,list:[{name:‘周周‘,salary:19000}]}]"; 76 TypeToken<List<DevelopmentDep>> typeToken = new TypeToken<List<DevelopmentDep>>() { 77 }; 78 Gson gson = new Gson(); 79 List<DevelopmentDep> list = gson.fromJson(json, typeToken.getType()); 80 System.out.println(list); 81 82 } 83 }
以上是关于Gson解析_exc1的主要内容,如果未能解决你的问题,请参考以下文章