public static void main(String[] args) {
String studentJson="{\"username\":\"xiaoming\",\"age\":20,\"desc\":\"好学生\"}";
String studentJson2="{\"username\":\"xiaohong\",\"age\":20,\"desc\":\"好学生\"}";
String[] json={studentJson,studentJson2};
//Student st=(Student) json2Bean(studentJson, Student.class);
JSONArray jsonArray=JSONArray.fromObject(json);//json对象存放了很多学生数据
for(Object obj:jsonArray){
Student st=(Student) json2Bean(((JSONObject) obj).toString(), Student.class);
System.out.println(st.getUsername()+":"+st.getDesc());
}
}
public static Object json2Bean(String json,Class cla){
JSONObject jsonObject=JSONObject.fromObject(json);
return JSONObject.toBean(jsonObject, cla);
}利用json-lib框架可以将前台的json对象转化为Java对象,需要如下的jar包,可以将多个Java对象的json数据结构存在数据当中,利用JSONArray转变为json数组,然后遍历该数组可以得到每个json数据所对应的Java对象,调用JSONObject的tobean方法既可以获得对象实例
原文:http://breezewindlw.blog.51cto.com/6504579/1694643