public class Student{ String age;//年龄 String name;//姓名 public Student(String age, String name) { this.age = age; this.name = name; } public String getAge() { return age; } public void setAge(String age) { this.age = age; } public String getName() { return name; } public void setName(String name) { this.name = name; } }
Map<String, Object> maps = new HashMap<>(); maps.put("brand","华为"); maps.put("price","3900"); Student student = new Student("18","郑丽丽"); maps.put("student",student); Map<String,String> map1 = new HashMap(); map1.put("user","小花"); map1.put("grade","15"); map1.put("coins","100000"); maps.put("userInfo",map1); JSONObject jsonObject = new JSONObject(maps);//转化为json格式 Log.e("baby",jsonObject.toString()); System.out.print(jsonObject.toString());
打印出来得结果为: