java-生成任意格式的json資料
private JSONObject createJSONObject() {
JSONObject result = new JSONObject();
result.put("success", true);
result.put("totalCount", "30");
result.put("okstatus",okreString );
JSONObject user1 = new JSONObject();
user1.put("id", "12");
user1.put("name", "張三");
user1.put("createTime", "2017-11-16 12:12:12");
JSONObject user2 = new JSONObject();
user2.put("id", "13");
user2.put("name", "李四");
user2.put("createTime", "2017-11-16 12:12:15");
JSONObject department = new JSONObject();
department.put("id", 1);
department.put("name","技術部");
user1.put("department", department);
user2.put("department", department);
// 返回一個JSONArray物件
JSONArray jsonArray = new JSONArray();
jsonArray.add(0, user1);
jsonArray.add(1, user2);
result.element("data", jsonArray);
return result;
}
結果是:
{"success":true,"totalCount":"30","okstatus":"登入失敗了","data":[{"id":"12","name":"張三","createTime":"2017-11-16 12:12:12","department":{"id":1,"name":"技術部"}},{"id":"13","name":"李四","createTime":"2017-11-16 12:12:15","department":{"id":1,"name":"技術部"}}]}