作为一名安卓程序猿,以前都是调用后台的接口,最近在学javaweb,自己给自己写接口,但是一个json对象我写起来很简单,或者一层数组写起来很简单,那么复杂的接口我就很费劲了,之前不太会,只能自己拼接,有时候还错误一堆堆。经过了群里的指导和自己的摸索,再难的接口我也不怕了,接下来我把代码贴上:
学生实体类:
import java.util.List; public class Student { private String name; private String sex; private int age; private List<GrilFriend> GfList; public String getName() { return name; } public List<GrilFriend> getGfList() { return GfList; } public void setGfList(List<GrilFriend> gfList) { GfList = gfList; } public void setName(String name) { this.name = name; } public String getSex() { return sex; } public void setSex(String sex) { this.sex = sex; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } }
女朋友实体类:
public class GrilFriend { private String name; public String getName() { return name; } public void setName(String name) { this.name = name; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } private int age; }
main代码:
import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; import com.google.gson.Gson; public class TestMap { static List<Map<String, Object>> userList; static List<Student> stulist; static List<GrilFriend> gfList; static List<GrilFriend> gfList2; public static void main(String args[]){ //userList 一个装所有内容的List userList = new ArrayList<Map<String, Object>>(); //stulist 学生属性的List stulist = new ArrayList<Student>(); Student stu;//学生实体类 gfList = new ArrayList<GrilFriend>();//第一个学生拥有的女朋友的list gfList2 = new ArrayList<GrilFriend>();//第二个学生拥有的女朋友的list //给两位同学添加几个女朋友 addGrilFriend(); Map<String, Object> map = new HashMap<String, Object>(); map.put("EmployeeCode", 111); map.put("EmployeeName", 222); map.put("PassWord", 333); map.put("CompanyID", 444); map.put("CompanyName", 555); map.put("BratchName", 666); map.put("MobilePhone", 777); map.put("MemberGuid", 888); if (map.containsKey("PassWord")) { System.out.println("存在PassWord"); }else { System.out.println("不存在PassWord"); } stu = new Student(); stu.setAge(22); stu.setName("Billy Lu"); stu.setSex("M"); stu.setGfList(gfList); stulist.add(stu); stu = new Student(); stu.setAge(23); stu.setName("Bruce Lu"); stu.setSex("M"); stu.setGfList(gfList2); stulist.add(stu); map.put("stu", stulist); userList.add(map); if (map.containsValue("Billy Lu")) { System.out.println("存在Billy Lu"); }else { System.out.println("不存在Billy Lu"); } System.out.println("lsit="+new Gson().toJson(userList)); } private static void addGrilFriend() { GrilFriend gf; gf = new GrilFriend(); gf.setName("鞠婧祎"); gf.setAge(23); gfList.add(gf); gf = new GrilFriend(); gf.setName("杨幂"); gf.setAge(28); gfList.add(gf); gf = new GrilFriend(); gf.setName("唐嫣"); gf.setAge(25); gfList2.add(gf); gf = new GrilFriend(); gf.setName("关晓彤"); gf.setAge(20); gfList2.add(gf); } } 运行结果: 存在PassWord 不存在Billy Lu lsit=[{"CompanyName":555,"EmployeeCode":111,"CompanyID":444,"stu":[{"name":"Billy Lu","sex":"M","age":22,"GfList":[{"name":"鞠婧祎","age":23},{"name":"杨幂","age":28}]},{"name":"Bruce Lu","sex":"M","age":23,"GfList":[{"name":"唐嫣","age":25},{"name":"关晓彤","age":20}]}],"BratchName":666,"MemberGuid":888,"EmployeeName":222,"PassWord":333,"MobilePhone":777}]
格式化一下:
[ { "CompanyName": 555, "EmployeeCode": 111, "CompanyID": 444, "stu": [ { "name": "Billy Lu", "sex": "M", "age": 22, "GfList": [ { "name": "鞠婧祎", "age": 23 }, { "name": "杨幂", "age": 28 } ] }, { "name": "Bruce Lu", "sex": "M", "age": 23, "GfList": [ { "name": "唐嫣", "age": 25 }, { "name": "关晓彤", "age": 20 } ] } ], "BratchName": 666, "MemberGuid": 888, "EmployeeName": 222, "PassWord": 333, "MobilePhone": 777 } ]
好了,楼主已经有这么多漂亮的女朋友了,好开心~~