There are two methods for splicing json string,
1. Direct brute splicing string
One should note that:
(1) in a key part of the string concatenation do not have extra spaces.
(2) To "use \ to escape
(3) the last value json array no comma, need to be addressed separately
String json = " { \" code\" : 200 ,"+ "\"data\" : [ ";
int i=0;
for ( i = 0; i < show.size() - 1 ; i++ ) {
String id = show.get(i).get("id");
String name = show.get(i).get("name");
String password = show.get(i).get("password");
json +="{ \"id\" : \" "+ id + " \", ";
json +=" \"name\" : \""+ name +" \", ";
json +=" \"password\" : \""+ password +" \"} , ";
}
json +="{ \"id\" :\" " + show.get(i).get("id") + " \", ";
json +=" \"name\" :\" "+ show.get(i).get("name") +" \", ";
json +=" \"password\" : \""+ show.get(i).get("password") +" \" }] } ";
2. Using the map key: value forms the string loop (this is convenient manner, can be abstracted as a tool);
String json = " { \" code\" : 200 ,"+ "\"data\" : [ ";
for (Map<String, String> map : show) {
System.out.println(map.toString());
json+= "{";
for (String key : map.keySet()) {
json+="\""+key+"\":\""+map.get(key)+"\"," ;
}
json=json.substring(0,json.length()-1);
json+="},";
}
json = json.substring(0,json.length()-1);
json+="]}";