自己拼接json字符串,现在用Gson来实现

时间:2021-07-24 17:29:51
 StringBuilder stringBuilder = new StringBuilder("[");
String ssString;
ArrayList<CompetitionsByCity> ccs =competitionMapper.getCmptList(hashMap); for (CompetitionsByCity tmp:ccs)
{ stringBuilder.append("{\"cmptId\":"+"\""+tmp.getCmptId()+"\""+","+"\"cmptName\":"+"\""+tmp.getCmptName()+"\""+","+
"\"duration\":"+"\""+tmp.getDuration()+"\""+","+"\"cmptStarLevel\":"+"\""+tmp.getCmptStarLevel()+"\""+","+"\"cmptCapacity\":"+"\""+tmp.getCmptCapacity()+"\""+","+"\"cmptLength\":"+"\""+tmp.getCmptLength()+"\""+
"\"cmptFavorByMe\":"+"\""+"0"+"\""+","+"\"imgId\":"+"\""+tmp.getImgId()+"\""+"},"); //stringBuilder.append(stringBuilder); }

下面是使用Gson

 String ssString;
ArrayList<CompetitionsByCity> ccs = competitionMapper
.getCmptList(hashMap);
Gson gson = new Gson();
String str = gson.toJson(ccs);
ssString = "\"size\":" + ccs.size() + "," + "\"cmptList\":" + str;
return ssString;