如何将{stuList=[{studentId=2811000226001011760, courseName=课程02, targetList=[{targetName=课程目标1, targetId=5d4d07f9-a541-4ed0-95d5-640c3ddb3c73, dcdVal=1.0}, {targetName=课程目标3, targetId=b32e945f-3009-466c-80a3-ad6663617077, dcdVal=1.0}, {targetName=课程目标2, targetId=c996641d-50b1-468b-84eb-05d6ffd470ca, dcdVal=1.0}], courseCode=0706002, studentName=学生02, studentNo=0717002, dcdVal=1.0, courseId=958625d0b3ca4cc59d10253959baa4c5}, {studentId=2811000226001011761, courseName=课程02, targetList=[{targetName=课程目标1, targetId=5d4d07f9-a541-4ed0-95d5-640c3ddb3c73, dcdVal=0.81}, {targetName=课程目标3, targetId=b32e945f-3009-466c-80a3-ad6663617077, dcdVal=0.93}, {targetName=课程目标2, targetId=c996641d-50b1-468b-84eb-05d6ffd470ca, dcdVal=0.67}], courseCode=0706002, studentName=学生03, studentNo=0717003, dcdVal=0.82, courseId=958625d0b3ca4cc59d10253959baa4c5}, {studentId=2811000226001011845, courseName=课程02, targetList=[{targetName=课程目标1, targetId=5d4d07f9-a541-4ed0-95d5-640c3ddb3c73, dcdVal=0.81}, {targetName=课程目标3, targetId=b32e945f-3009-466c-80a3-ad6663617077, dcdVal=0.93}, {targetName=课程目标2, targetId=c996641d-50b1-468b-84eb-05d6ffd470ca, dcdVal=0.67}], courseCode=0706002, studentName=达成度专业学生3, studentNo=071903, dcdVal=0.82, courseId=958625d0b3ca4cc59d10253959baa4c5}], 使用jsonObject解构
时间: 2024-04-23 09:29:09 浏览: 81
你可以使用 jsonObject 对象解构来将该字符串解析为 JSON 格式。下面是一个示例代码:
```java
import org.json.JSONArray;
import org.json.JSONObject;
public class Main {
public static void main(String[] args) {
String jsonString = "{stuList=[{studentId=2811000226001011760, courseName=课程02, targetList=[{targetName=课程目标1, targetId=5d4d07f9-a541-4ed0-95d5-640c3ddb3c73, dcdVal=1.0}, {targetName=课程目标3, targetId=b32e945f-3009-466c-80a3-ad6663617077, dcdVal=1.0}, {targetName=课程目标2, targetId=c996641d-50b1-468b-84eb-05d6ffd470ca, dcdVal=1.0}], courseCode=0706002, studentName=学生02, studentNo=0717002, dcdVal=1.0, courseId=958625d0b3ca4cc59d10253959baa4c5}, {studentId=2811000226001011761, courseName=课程02, targetList=[{targetName=课程目标1, targetId=5d4d07f9-a541-4ed0-95d5-640c3ddb3c73, dcdVal=0.81}, {targetName=课程目标3, targetId=b32e945f-3009-466c-80a3-ad6663617077, dcdVal=0.93}, {targetName=课程目标2, targetId=c996641d-50b1-468b-84eb-05d6ffd470ca, dcdVal=0.67}], courseCode=0706002, studentName=学生03, studentNo=0717003, dcdVal=0.82, courseId=958625d0b3ca4cc59d10253959baa4c5}, {studentId=2811000226001011845, courseName=课程02, targetList=[{targetName=课程目标1, targetId=5d4d07f9-a541-4ed0-95d5-640c3ddb3c73, dcdVal=0.81}, {targetName=课程目标3, targetId=b32e945f-3009-466c-80a3-ad6663617077, dcdVal=0.93}, {targetName=课程目标2, targetId=c996641d-50b1-468b-84eb-05d6ffd470ca, dcdVal=0.67}], courseCode=0706002, studentName=达成度专业学生3, studentNo=071903, dcdVal=0.82, courseId=958625d0b3ca4cc59d10253959baa4c5}]}}";
JSONObject jsonObject = new JSONObject(jsonString);
JSONArray stuList = jsonObject.getJSONArray("stuList");
for (int i = 0; i < stuList.length(); i++) {
JSONObject student = stuList.getJSONObject(i);
String studentId = student.getString("studentId");
String courseName = student.getString("courseName");
String courseCode = student.getString("courseCode");
String studentName = student.getString("studentName");
String studentNo = student.getString("studentNo");
double dcdVal = student.getDouble("dcdVal");
String courseId = student.getString("courseId");
System.out.println("Student ID: " + studentId);
System.out.println("Course Name: " + courseName);
System.out.println("Course Code: " + courseCode);
System.out.println("Student Name: " + studentName);
System.out.println("Student No: " + studentNo);
System.out.println("dcdVal: " + dcdVal);
System.out.println("Course ID: " + courseId);
}
}
}
```
此代码将打印每个学生的相关信息。你可以根据需要修改代码来处理目标列表等更详细的信息。请确保在使用 JSON 解析器之前,导入正确的 JSON 库(例如 `org.json`)并在项目中添加相关的依赖。
阅读全文