public static void main(String[] args) {
System.out.println("生成json字符串");
StringBuffer words = new StringBuffer();
words.append("[");
for (int i = 0; i < 3; i++) {
JSONObject results = new JSONObject();
results.put("id", i);
results.put("name", "a" + i);
results.put("content", "aaaaa" + i);
JSONObject r = (JSONObject) JSONValue.parse(results.toString());
words.append(r.toString());
}
words.append("]");
System.out.println("格式化json字符串");
Object obj = JSONValue.parse(words.toString());
System.out.println("转json对象");
JSONArray array = (JSONArray) obj;
//删除
//array.remove(0);
//添加
/* String content = "qqqqq";
String name = "ppppppp";
Integer id = 11;
array.add("{\"content\":" + content + ",\"id\":" + id + ",\"name\":" + name + "}");*/
//修改
/*Integer updateid=0;
JSONObject resultsupdate = (JSONObject)array.get(updateid);
resultsupdate.put("id", updateid);
resultsupdate.put("name", "nameupdateid" + updateid);
resultsupdate.put("content", "contentupdateid" + updateid);*/
for (int i = 0; i < array.size(); i++) {
System.out.println(array.get(i));
}
}