解析JSON

1.JSON字符串轉List<實體>(背景:接口返回字段和實體屬性不匹配)
String str = {"message":[{"stu_id":10,"stu_mc":"張三"},{"stu_id":20,"stu_mc":"李四"}],"code":"1"}
com.alibaba.fastjson.JSONObject jsonObj = com.alibaba.fastjson.JSON.parseObject(str);
String info = jsonObj.getString("message");
if(StringUtils.isNotEmpty(info)){
    com.alibaba.fastjson.JSONArray array = JSONArray.parseArray(info);
    List<Student> studentList = new ArrayList<>();
    for(int i=0;i < array.size();i++){
        Student student = new Student();//實體
        student.setStuName(JSONObject.parseObject(JSONObject.toJSONString(array.get(i))).getString("stu_mc"));//獲取值
        studentList.add(student);
    }
}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章