String jsonStr = "{'input':[{'userId':'1','userName':'小红'},{'userId':'2','userName':'小刘老师'}],'classType':['Student','Teacher']}";
JSONObject fileJso = JSON.parseObject(jsonStr);
JSONObject input = (JSONObject) fileJso.get("input");
List<String> typeList = (List) fileJso.get("classType");
System.out.println(typeList);
JSONObject valueJson = new JSONObject();
for(int i = 0; i < typeList.size(); i++){
String inputStr = (String) input.get(i);
Object o = new Object();
if("Student".equals(typeList.get(i))){
o = JSONObject.parseObject(inputStr, Student.class);
valueJson.put("value"+(i+1), o);
}else if("Teacher".equals(typeList.get(i))){
o = JSONObject.parseObject(inputStr, Teacher.class);
valueJson.put("value"+(i+1), o);
}
}
能不能不用 if 判断,实现这段代码的功能?
JSONObject fileJso = JSON.parseObject(jsonStr);
JSONObject input = (JSONObject) fileJso.get("input");
List<String> typeList = (List) fileJso.get("classType");
System.out.println(typeList);
JSONObject valueJson = new JSONObject();
for(int i = 0; i < typeList.size(); i++){
String inputStr = (String) input.get(i);
Object o = new Object();
if("Student".equals(typeList.get(i))){
o = JSONObject.parseObject(inputStr, Student.class);
valueJson.put("value"+(i+1), o);
}else if("Teacher".equals(typeList.get(i))){
o = JSONObject.parseObject(inputStr, Teacher.class);
valueJson.put("value"+(i+1), o);
}
}
能不能不用 if 判断,实现这段代码的功能?









