クライアントは List<Object> を JsonArray および JsonObject に変換する必要があります。
まず、リスト内のオブジェクトのプロパティをパブリックにする必要があります。
クラス 人 { public 文字列名; パブリックストリングセックス。 公的整数年齢。 }
以下では、 List<person> personList = new ArrayList<person>(); データがロードされていると想定しています。
JSONArray jsonArray = new JSONArray(); JSONObject jsonObject = new JSONObject(); JSONObject tmpObj = null; int count = personList.size(); for(int i = 0; i < count; i++) { tmpObj = new JSONObject(); tmpObj.put("名前" , personList.get(i).name); tmpObj.put("性別", personList.get(i).sex); tmpObj.put("年齢", personList.get(i).age); jsonArray.put(tmpObj); tmpObj = null; 文字 列 personInfos = jsonArray.toString(); // 将JSONArray转换得String jsonObject.put("personInfos" , personInfos); // JSONオブジェクトの文字列を取得します
jsonArrayで変換された文字列は以下のようになります。
[{"名前": "mxd", "性別": "男の子", "年齢": 12}, {"名前": "トム", "性別": "男の子", "年齢": 23}, { "名前": "ジム"、"性別": "女の子"、"年齢": 20}]
jsonObjectで変換された文字列は以下のようになります。
{"personInfos": [{"名前": "mxd", "性別": "男の子", "年齢": 12}, {"名前": "トム", "性別": "男の子", "年齢" : 23}, {"名前": "ジム"、"性別": "女の子"、"年齢": 20}]}