用maven开发web时我都用的maven提供的依赖,本地的jar一直导不进去,然后我随便找了个Google的json的jar包,里面的方法少的可怜
pom中写入依赖
<!-- https://mvnrepository.com/artifact/com.googlecode.json-simple/json-simple -->
<dependency>
<groupId>com.googlecode.json-simple</groupId>
<artifactId>json-simple</artifactId>
<version>1.1.1</version>
</dependency>
现在我有的是JavaBean的数组,我需要转成json字符串数组
先说一个JavaBean对象转成一个json字符串
JSONObject.toJSONString(Map map)方法参数是一个map集合,返回一个json字符串{}
如果是JavaBean数组
提供了一个方法JSONArray.toJSONString(List list)方法参数是一个列表,返回json字符串[{},{}],这个list只需要是Map类型的就行
<%
SelectInfo selectInfo = new SelectInfo();
TotalInfo[] totalInfos = selectInfo.selectTotalInfo();
List<Map> list = new ArrayList<>();
for(TotalInfo totalInfo : totalInfos){
Map map = new HashMap();
map.put("specialty",totalInfo.getSpecialty());
map.put("grade",totalInfo.getGrade());
map.put("studentNo",totalInfo.getStudentNo());
map.put("studentName",totalInfo.getStudentName());
map.put("studentSex",totalInfo.getStudentSex());
map.put("subjectName",totalInfo.getSubjectName());
map.put("studentScore",totalInfo.getStudentScore());
list.add(map);
//String strJson = JSONObject.toJSONString(map);
//out.write(strJson);
}
String jsonString = JSONArray.toJSONString(list);
out.write(jsonString);
%>