//创建学生对象
Student student=new Student();
student.setAge(23);
student.setClazz("六年级");
student.setName("王二麻子");
//创建JSONObject
JSONObject jsonObject=new JSONObject();
//键值对赋值
jsonObject.put("age",student.getAge());
jsonObject.put("name",student.getName());
jsonObject.put("clazz",student.getClazz());
//转化成json字符串
String json=jsonObject.toString();
//输出日志
Log.e("ObjectToJson",json);
JSONObject jsonObject=new JSONObject(json);
Student student=new Student();
student.setName(jsonObject.getString("name"));
student.setClazz(jsonObject.getString("clazz"));
student.setAge(jsonObject.getInt("age"));
Log.e("JsonToObject",student.getName()+"======"+student.getClazz()+"===="+student.getAge());
//创建一个集合
List<Student> students=new ArrayList<Student>();
students.add(student);
students.add(student);
//创建一个JSONArray
JSONArray jsonArray=newJSONArray();
//遍历学生集合
for(inti=0;i<students.size();i++){
//获取学生对象
Studentstu=students.get(i);
//创建JSONObject
JSONObject jo=newJSONObject();
//赋值
jo.put("age",stu.getAge());
jo.put("name",stu.getName());
jo.put("clazz",stu.getClazz());
//把JSONObject 添加到JSONArray
jsonArray.put(jo);
}
//toString转为json
String json=jsonArray.toString();
Log.e("ListToJson",json);
//创建JSONArray把json传入
JSONArray jsonArray=new JSONArray(json);
//创建学生集合
Student students=new ArrayList<Student>();
Log.e("BeforeJsonToList","集合长度"+students.size());
//遍历jsonArray
for(inti=0;i<jsonArray.length();i++){
//获取JSONObject对象
JSONObject jsonObject=jsonArray.getJSONObject(i);
//创建学生对象
Student stu=new Student();
//赋值
jsonObject.put("age",stu.getAge());
jsonObject.put("name",stu.getName());
jsonObject.put("clazz",stu.getClazz());
//把学生对象添加到集合中
students.add(stu);
}
Log.e("AfterJsonToList","集合长度"+students.size());
//创建学生对象
Student student=new Student();
student.setClazz("一班");
student.setAge(23);
student.setName("李四");
//将对象转为json串
String json=JSON.toJSONString(student);
Log.e("ObjectToJson",json);
//将json转为对象 参数1json 参数2对象类型
Student student=JSON.parseObject(json,Student.class);
Log.e("JsonToObject","=========="+student.getName());
List<Student>stuList=new ArrayList<Student>();
stuList.add(student);
stuList.add(student);
stuList.add(student);
//List集合转json
json=JSON.toJSONString(stuList);
Log.e("ListToJson","=========="+json);
stuList=JSON.parseArray(json,Student.class);
Student student1=stuList.get(0);
Log.e("JsonToList","====================="+student1.getName());
//过滤字段 属性过滤器PropertyFilter
json=JSON.toJSONString(stuList, new PropertyFilter() {
@Override//参数1 正在被过滤的对象 参数2 过滤的属性名 参数3 属性值
public boolean apply(Object o, String s, Object o1) {
Log.e("PropertyFilter",o+"======"+s+"==============="+o1);
if (s.equals("name")){
return false;
}else{
return true;
}
}
});
Log.e("PropertyFilter",json);
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有