Logo

解析入参
| Map<String,Object> param = JSON.parseObject(JSON.toJSONString(requestDTO.getBody()), Map.class); |
| icd10Name = (String) param.get(ICD10NAME); |
代码展示
| @Test |
| public void SendCash(){ |
| List<Student> objects = new ArrayList<>(); |
| Collections.addAll(objects, |
| new Student("王通通","男",12,"2班"), new Student("闫晓雨","女",18,"1班"), new Student("高佳琪","男",20,"4班")); |
| |
| |
| |
| |
| |
| String s = JSON.toJSONString(new Student("王步嵩", "男", 26,"8班")); |
| System.out.println("转换对象之后"+s); |
| |
| |
| Student student = JSON.parseObject(s,Student.class); |
| |
| Student student = JSON.parseObject(JSON_OBJ_STR, new TypeReference<Student>() {}); |
| System.out.println("转换对象之前"+student); |
| |
| |
| List<Student> studentList = JSON.parseArray(JSON.toJSONString(objects), Student.class); |
| Iterator<Student> iterator = studentList.iterator(); |
| while(iterator.hasNext()){ |
| Student next = iterator.next(); |
| System.out.println(next.getName()+":"+next.getAge()); |
| } |
| |
| |
| JSONObject jsonObject = (JSONObject)JSON.toJSON(new Student("白志超", "男", 36,"12班")); |
| System.out.println(jsonObject.getString("name")); |
| |
| } |
| |
| class JSONS { |
| static void JS() { |
| List<Student> list = new ArrayList<>(); |
| Student student = new Student("张三", "男", "1"); |
| Collections.addAll(list, student, new Student("李四", "女", "2"), new Student("王五", "女", "2")); |
| |
| System.out.println("*******javaBean to jsonString*******"); |
| String stu = JSON.toJSONString(student); |
| System.out.println(stu); |
| System.out.println("\t" + JSON.toJSONString(list)); |
| |
| System.out.println("******jsonString to javaBean*******"); |
| Student parseClass = JSON.parseObject(stu, Student.class); |
| System.out.println("转换前\t" + parseClass); |
| JSONObject jsonObject = JSON.parseObject(stu); |
| System.out.println("转换后\t" + jsonObject); |
| |
| System.out.println("******javaBean to jsonObject******"); |
| JSONObject o = (JSONObject) JSON.toJSON(student); |
| System.out.println(o); |
| System.out.println(o.getString("name")); |
| |
| System.out.println("******jsonObject to javaBean******"); |
| Student JavaObJ = JSON.toJavaObject(o, Student.class); |
| System.out.println(JavaObJ); |
| |
| System.out.println("*****jsonObject to jsonString*****"); |
| System.out.println(JSON.toJSONString(student)); |
| |
| |
| System.out.println("*******jsonString to jsonObject*****"); |
| JSONObject jso1 = JSON.parseObject(stu); |
| System.out.println(jso1.getString("name")); |
| System.out.println(); |
| } |
| |
| static void jQuery() { |
| System.out.println("*******javaBean to jsonArray******"); |
| List<Student> list = new ArrayList<>(); |
| for (int i = 0; i < 6; i++) { |
| Collections.addAll(list, new Student("姓名" + i, "性别" + i, "年级" + i)); |
| } |
| JSONArray o = (JSONArray) com.alibaba.fastjson.JSON.toJSON(list); |
| for (int i = 0; i < o.size(); i++) { |
| System.out.println(o.get(i)); |
| } |
| System.out.println(); |
| |
| System.out.println("*****jsonArry to javalist******"); |
| List<Student> myList = new ArrayList<>(); |
| for (int i = 0; i < o.size(); i++) { |
| myList.add(com.alibaba.fastjson.JSON.toJavaObject(o.getJSONObject(i), Student.class)); |
| } |
| for (Iterator<Student> iterator = myList.iterator(); iterator.hasNext(); ) { |
| Student next = iterator.next(); |
| System.out.println(next); |
| } |
| |
| } |
| } |
把数组中的值存入到下面List集合中
| |
| |
| |
| class ApprovalReturnInfoDTO implements Serializable { |
| |
| |
| |
| |
| private static final long serialVersionUID = 700674671863229028L; |
| |
| |
| |
| |
| private String uuid; |
| |
| |
| |
| |
| private String code; |
| |
| |
| |
| |
| private String name; |
| |
| |
| |
| |
| private String errorReason; |
| } |
参数拼接
| |
| JSONObject jsonObjectCe = new JSONObject(); |
| JSONObject head = new JSONObject(); |
| head.put("requestType", "090028"); |
| head.put("unitCode", "01"); |
| head.put("userCode", "01040181"); |
| jsonObjectCe.put("head", head); |
| JSONObject body = new JSONObject(); |
| body.put("caseno", requestJson.getString("caseno")); |
| jsonObjectCe.put("body", body); |
格式化展示
- 占位符
| { |
| "resCode":"0000", |
| "resMsg":"成功", |
| "resultContent":{ |
| "userStatus":"0", |
| "userCode":"24584568", |
| "platform":"PC" |
| } |
| } |
超强工具类
| |
| String url = ClaimUrlConfigConstants.SEND_CAR_URL; |
| String body = HttpRequest.post(url + "/sunRTC/request").timeout(2000) |
| .body(JSONObject.toJSONString(headDTO)) |
| .execute() |
| .body(); |
| JSONObject obj = (JSONObject)JSON.parse(body); |
| if(obj != null){ |
| String resCode = obj.getString("resCode"); |
| if("0000".equals(resCode)){ |
| List<UserInfoVo> userList = JSON.parseArray(obj.getString("resultContent"), UserInfoVo.class); |
| for (UserInfoVo userInfo:userInfoVoList) { |
| userInfo.setPostName(userInfo.getPostName()); |
| userList.forEach(userIfo->{ |
| |
| userInfo.setPlatform(userIfo.getPlatform()); |
| |
| if(userIfo.getUserCode().equals(userInfo.getUserCode())){ |
| userInfo.setState("0"); |
| }else { |
| userInfo.setState("1"); |
| } |
| }); |
| } |
| } |
| } |
JSON 集合转换
| |
| List<PolicyInfo> ts = (List<PolicyInfo>) JSONArray.parseArray(JSON.toJSONString(data.getPolicyInfos()), PolicyInfo.class); |
| |
| |
| List<PolicyInfo> policyInfoList = JSONArray.parseArray(JSON.toJSONString(data.getPolicyInfos()), PolicyInfo.class); |
| |
| |
| ArrayList<Student> students = JSON.parseObject(JSON_ARRAY_STR, new TypeReference<ArrayList<Student>>() {}); |
复杂json格式字符串与JSONObject之间的转换

转换为 数组 和 JSONObject
| |
| JSONObject jsonObject1 = JSON.parseObject(JSON_OBJ_STR); |
| |
| |
| JSONArray jsonArray = JSON.parseArray(JSON_ARRAY_STR); |
| |
| |
| int size = jsonArray.size(); |
| for (int i = 0; i < size; i++) { |
| JSONObject jsonObject = jsonArray.getJSONObject(i); |
| } |
| |
| |
| for (Object obj : jsonArray) { |
| JSONObject jsonObject = (JSONObject) obj; |
| } |
outer
- 占位符
- 接下来阐述正文:(其实是copy其他人的博客,主要放在自己博客下面好找...........................)
- 华丽的分割线=====================
- JSON:fastJson的解析器,用于JSON格式字符串与JSON对象及javaBean之间的转换。
- JSONObject:fastJson提供的json对象。
- JSONArray:fastJson提供json数组对象。首先定义了三个json格式的字符串作为我们的数据源
前端传值【FastJson学习:JSON格式字符串、JSON对象及JavaBean之间的相互转换】
- 占位符
- 当前台需要传送一系列相似数据到后端时,可以考虑将其组装成json数组对象,然后转化为json形式的字符串传输到后台
| nodes = $('#PmPbsSelect_tree').tree('getChecked'); |
| var data=[]; |
| for(var i=0;i<nodes.length;i++){ |
| if(!isParentCheck(nodes[i],nodes)){ |
| data.push({"id":nodes[i].id, |
| "pid":node.id}); |
| }else{ |
| data.push({"id":nodes[i].id, |
| "pid":null}); |
| } |
| } |
| dataStr=JSON.stringify(data); |
| $.ajax({ |
| url:ctx+"/PmWbs/savePmWbsByModel", |
| type:"POST", |
| data:{"dataStr":dataStr, |
| "projectId":pmProjectSelect.combobox('getValue')}, |
| success:function(data){ |
| basetree.tree('reload'); |
| }, |
| error:function(){ |
| alert("请求失败"); |
| }, |
| dataType:"json" |
| }); |
JSON 转换集合 and 单个 Bean操作

* JSON转换集合JavaBean

| import com.alibaba.fastjson.JSON; |
| |
| import java.util.ArrayList; |
| import java.util.List; |
| |
| public class Test02 { |
| |
| |
| |
| private static final String JSON_OBJ_STR = "{\"studentName\":\"lily\",\"studentAge\":12}"; |
| |
| private static final String JSON_ARRAY_STR = "[{\"studentName\":\"lily\",\"studentAge\":12},{\"studentName\":\"lucy\",\"studentAge\":15}]"; |
| |
| private static final String STGR01 = "{\"teacherName\":\"张三\",\"teacherAge\":27,\"course\":{\"courseName\":\"english\",\"code\":1270},\"students\":[{\"studentName\":\"lily\",\"studentAge\":12},{\"studentName\":\"lucy\",\"studentAge\":15}]}"; |
| private static final String STGR02 = "{\"teacherName\":\"李四\",\"teacherAge\":27,\"course\":{\"courseName\":\"english\",\"code\":1270},\"students\":[{\"studentName\":\"lily\",\"studentAge\":12},{\"studentName\":\"lucy\",\"studentAge\":15}]}"; |
| |
| |
| public static void main(String[] args) { |
| List<String> list = new ArrayList<>(); |
| list.add(STGR01); |
| list.add(STGR02); |
| |
| |
| Teacher parseObject = JSON.parseObject(String.valueOf(STGR01), Teacher.class); |
| System.out.println(parseObject.toString()); |
| |
| |
| |
| |
| |
| List<Teacher> teacherList = JSON.parseArray(list.toString(), Teacher.class); |
| teacherList.forEach(teacher -> { |
| System.out.println("师傅:" + teacher.getTeacherName() + "徒弟:" + JSON.toJSON(teacher.getStudents())); |
| }); |
| |
| |
| |
| |
| } |
| } |
| |
| |
| class Course { |
| |
| private String courseName; |
| private Integer code; |
| |
| public String getCourseName() { |
| return courseName; |
| } |
| |
| public void setCourseName(String courseName) { |
| this.courseName = courseName; |
| } |
| |
| public Integer getCode() { |
| return code; |
| } |
| |
| public void setCode(Integer code) { |
| this.code = code; |
| } |
| } |
| |
| class Student { |
| |
| private String studentName; |
| private Integer studentAge; |
| |
| public String getStudentName() { |
| return studentName; |
| } |
| |
| public void setStudentName(String studentName) { |
| this.studentName = studentName; |
| } |
| |
| public Integer getStudentAge() { |
| return studentAge; |
| } |
| |
| public void setStudentAge(Integer studentAge) { |
| this.studentAge = studentAge; |
| } |
| } |
| |
| class Teacher { |
| |
| private String teacherName; |
| private Integer teacherAge; |
| private Course course; |
| private List<Student> students; |
| |
| public String getTeacherName() { |
| return teacherName; |
| } |
| |
| public void setTeacherName(String teacherName) { |
| this.teacherName = teacherName; |
| } |
| |
| public Integer getTeacherAge() { |
| return teacherAge; |
| } |
| |
| public void setTeacherAge(Integer teacherAge) { |
| this.teacherAge = teacherAge; |
| } |
| |
| public Course getCourse() { |
| return course; |
| } |
| |
| public void setCourse(Course course) { |
| this.course = course; |
| } |
| |
| public List<Student> getStudents() { |
| return students; |
| } |
| |
| public void setStudents(List<Student> students) { |
| this.students = students; |
| } |
| |
| @Override |
| public String toString() { |
| return "Teacher{" + |
| "teacherName='" + teacherName + '\'' + |
| ", teacherAge=" + teacherAge + |
| ", course=" + course + |
| ", students=" + students + |
| '}'; |
| } |
| } |
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· Manus的开源复刻OpenManus初探
· AI 智能体引爆开源社区「GitHub 热点速览」
· 从HTTP原因短语缺失研究HTTP/2和HTTP/3的设计差异
· 三行代码完成国际化适配,妙~啊~