对json节点进行解析
一、当根节点为“[]”的json
[
{
"id": 1580615,
"name": "皮的嘛",
"packageName": "com.renren.mobile.android",
"iconUrl": "app/com.renren.mobile.android/icon.jpg",
"stars": 2,
"size": 21803987,
"downloadUrl": "app/com.renren.mobile.android/com.renren.mobile.android.apk",
"des": "2011-2017 你的铁头娃一直在这儿。中国最大的实名制SNS网络平台,嫩头青"
},
{
"id": 1540629,
"name": "不存在的",
"packageName": "com.ct.client",
"iconUrl": "app/com.ct.client/icon.jpg",
"stars": 2,
"size": 4794202,
"downloadUrl": "app/com.ct.client/com.ct.client.apk",
"des": "斗鱼271934走过路过不要错过,这里有最好的鸡儿"
}
]
JSONArray jsonArray=new JSONArray(json);
for(int i=0;i<jsonArray.size();i++){
JSONObject object=jsonArray.getJSONObject(i);
String id=object.getString("id");
String name=object.getString("name");
String packageName=object.getString("packageName");
String iconUrl = object.getString("iconUrl");
double stars=Double.parseDouble(object.getString("stars"));
String size=object.getString("size");
String downloadUrl = object.getString("downloadUrl");
String des = object.getString("des");
....
}
二、当跟节点为"{}"的json
{
"name": "ros01",
"stuid": "ros01",
"cosid":"2c9180827a83b16e017a995d090e0048",
"examTime": "2021/07/21",
"examDuration": "1小时30分05秒",
"expScore": "88",
"list": [
{
"projectName": "加工过程中舱门是否关闭",
"assessResult": "舱门关闭",
"escore": "10"
},
{
"projectName": "项目名称描述2",
"assessResult": "撞刀",
"escore": "0"
},
{
"projectName": "项目名称描述3",
"assessResult": "",
"escore": "12"
},
{
"projectName": "项目名称描述4",
"assessResult": "",
"escore": "10"
}
]
}
@PostMapping("tch/saveScore")
public Map tchSavescore(@RequestBody(required = false) String stuScore) throws Exception{
String deurl = URLDecoder.decode(stuScore,"UTF-8");
System.out.println("stuScore1"+deurl);
JSONObject stuobj = (JSONObject) JSONObject.parse(deurl);
String name = stuobj.getString("name");
String stuid = stuobj.getString("stuid");
String cosid = stuobj.getString("cosid");
String examTime = stuobj.getString("examTime");
String examDuration = stuobj.getString("examDuration");
String expScore = stuobj.getString("expScore");
String list = stuobj.getString("list");
JSONArray jsonArray = new JSONArray(JSONArray.parseArray(list));
for (int i=0;i<jsonArray.size();i++){
JSONObject object=jsonArray.getJSONObject(i);
CozExpScore cozExpScore = new CozExpScore();
cozExpScore.setName(name);
cozExpScore.setStuid(stuid);
cozExpScore.setCosid(cosid);
cozExpScore.setExamTime(examTime);
cozExpScore.setExamDuration(examDuration);
cozExpScore.setExpScore(expScore);
String projectName = object.getString("projectName");
String assessResult = object.getString("assessResult");
String escore = object.getString("escore");
System.out.println(projectName);
cozExpScore.setProjectName(projectName);
cozExpScore.setAssessResult(assessResult);
cozExpScore.setEscore(escore);
courseService.saveStuScore(cozExpScore);
}
Map m = super.success();
m.put("msg","");
return m;
}