//假设为Person类geter\seter
JSONArray array = JSONArray.fromObject(jsonString);
Person[] persons = new Person[array.size()];
for(int i = 0; i < array.size(); i++){
JSONObject jsonObject = array.getJSONObject(i);
persons[i] = JSONObject.toBean(jsonObject, Person.class); //转成实体对象
}
return persons;//person对象数组,可以循环拿来取出person,get各个属性了
你试试
在页面上js解析的确要方便很多
responseText字符串可以用JSONArray(貌似是这么写,记不清了)对象来处理为List类型的实例。然后就可以通过toArray()方法转换为数组。
比如要将String cats转为类Cat的对象数组
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.xxx.Cat;
cats的内容为{"cat":[{"name":"huahua","age":77},{"name":"qiqi","age":81}]}
List
if (StringUtils.isNotEmpty(cats)){
JSONObject jsonObject = JSON.parseObject(cats);
catList = jsonObject.getJSONArray("cat").toJavaList(Cat.class);
}
你都用到json了,ajax方式用javascript处理不是更好,直接eval一下就变成object了,多好
Object[] a = [{"name":"小明“,”age“:"18","sex":"男","adress":"北京"},{"name":"小红“,”age“:"18","sex":"女","adress":"北京"},{"name":"小强“,”age“:"20","sex":"男","adress":"南京"}];
Object b = a.get(0)