1.JSON的String字符串与Java的List列表对象的相互转换
在前端:
1.如果json是List对象转换的,可以直接遍历json,读取数据。
2.如果是需要把前端的List对象转换为json传到后台,param是ajax的参数,那么转换如下所示: var jsonStr = JSON.stringify(list);
var param= {};
param.jsonStr=jsonStr; 在后台: import net.sf.json.JSONObject;
import net.sf.json.JSONArray;
1.把String转换为List(str转换为list) List<T> list = new ArrayList<T>();
JSONArray jsonArray = JSONArray.fromObject(str);//把String转换为json
list = JSONArray.toList(jsonArray,t);//这里的t是Class<T> 在这里,因为得到json字符串的时候用的是 JSONArray.fromObject(collenction),所有,在讲json字符串转换成json对象的时候,
也只能用JSONArray.toCollection,并且,一定要带上后面的class参数。 2.把List转换为json JSONArray json = JSONArray.fromObject(object);
String str = json.toString();//把json转换为String 再如:
Teacher类中,有三个属性。 private String teaId;
private String teaName;
private List<Student> stus; Teacher teacher_1 = new Teacher("编号1", "教师1", stus); JSONObject obj = JSONObject.fromObject(teacher_1); 这一步,将Teacher 对象转换成json字符串的时候,没有任何的问题。下面,将JSONObject 转换成Teacher 对象,如果,不加后面的class参数,也会报这儿错误。加了后,这一步也正常。 Teacher teacherBean = (Teacher) JSONObject.toBean(obj, Teacher.class);
Student studentBean = teacherBean.getStus().get(0); 当从teacherBean 对象中取出stus属性的值时候,就会提示:
Java.lang.ClassCastException:net.sf.ezmorph.bean.MorphDynaBean cannot be cast to com.edu.xukai.Student 用这种方式可以解决:
Map<String, Class> classMap = new HashMap<String, Class>();
classMap.put("stus", Student.class);
Teacher teacherBean = (Teacher) JSONObject.toBean(obj, Teacher.class, classMap); 其中map对象是teacherBean对象中各个属性的类型,map额key是属性每次,value是属性的类型。 JSONArray.toArray(jsonArray, objectClass, classMap)
JSONArray.toList(jsonArray, objectClass, classMap) 也支持这样的方式。
2. JSON 集合数据字符串转对象集合 及 对象集合转 JSON 字符串
import org.codehaus.jackson.JsonParseException;
import org.codehaus.jackson.map.JsonMappingException;
import org.codehaus.jackson.map.ObjectMapper;
import org.codehaus.jackson.type.TypeReference; // 集合转 json
ArrayList<Student> list=new ArrayList<Student>();
StringWriter str=new StringWriter();
ObjectMapper objectMapper=new ObjectMapper();
objectMapper.writeValue(str, list);
System.out.println(str); // json 转对象集合
ObjectMapper mapper = new ObjectMapper();
List<Student> list = mapper.readValue(str.toString(),
new TypeReference<List<Student>>() {});
3.示例
public static void main(String[] args) {
ArrayList<Student> list=new ArrayList<Student>();
Student s1=new Student();
s1.setName("leilei");
s1.setAge(23);
Student s2=new Student();
s2.setName("leilei02");
s2.setAge(23);
list.add(s1);
list.add(s2); StringWriter str=new StringWriter(); ObjectMapper objectMapper=new ObjectMapper();
try {
objectMapper.writeValue(str, list);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println(str);
} public class JsonToJavaBean {
public static void main(String[] args) {
String str="{\"student\":[{\"name\":\"leilei\",\"age\":23},{\"name\":\"leilei02\",\"age\":23}]}";
Student stu = null;
List<Student> list = null;
try {
ObjectMapper objectMapper=new ObjectMapper();
StudentList studentList=objectMapper.readValue(str, StudentList.class); list=studentList.getStudent();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
for(Student s:list){
System.out.println(s.getName()+" "+s.getAge());
}
}
}