对象封装成为List和JSON解析成封装有对象的List1、封装方法类(将JSONArray放入JSONObject中发给客户端)package com.mlp.tools;import java.util.ArrayList;import org.json.JSONArray;import org.json.JSONException;import org.json.JSONObject;import ebean.Messages;import ebean.Resources;import ebean.Types;public class ListToJSONArray {public static JSONArray setListToJR(ArrayList<Resources> list){ JSONObject json=null;JSONArray ja=new JSONArray();for(int i=0;i<list.size();i++){json=new JSONObject();try {json.put("id", list.get(i).getRes_id());json.put("name", list.get(i).getRes_name());json.put("form", list.get(i).getRes_form());json.put("update", list.get(i).getRes_update());json.put("upuser", list.get(i).getRes_upuser());json.put("patch", list.get(i).getRes_patch());json.put("pass", list.get(i).getRes_pass());json.put("downsum", list.get(i).getRes_downsum());json.put("reco", list.get(i).isRes_reco());json.put("remark", list.get(i).getRes_remark());ja.put(i, json);} catch (JSONException e) {// TODO Auto-generated catch blocke.printStackTrace();}}return ja;}public static JSONArray setListToJT(ArrayList<Types> list){ JSONObject json=null;JSONArray ja=new JSONArray();for(int i=0;i<list.size();i++){json=new JSONObject();try {json.put("id", list.get(i).getType_id());json.put("name", list.get(i).getType_name());json.put("num", list.get(i).getType_num());ja.put(i, json);} catch (JSONException e) {// TODO Auto-generated catch blocke.printStackTrace();}}return ja;}public static JSONArray setListToJM(ArrayList<Messages> list){ JSONObject json=null;JSONArray ja=new JSONArray();for(int i=0;i<list.size();i++){json=new JSONObject();try {json.put("id", list.get(i).getMes_id());json.put("reuser", list.get(i).getMes_user());json.put("form", list.get(i).getMes_form());json.put("reto", list.get(i).getMes_to());json.put("date", list.get(i).getMes_date());json.put("content", list.get(i).getMes_content());ja.put(i, json);} catch (JSONException e) {// TODO Auto-generated catch blocke.printStackTrace();}}return ja;}}2、解析方法类package com.chase.db;import java.util.ArrayList;import org.json.JSONArray;import org.json.JSONException;import org.json.JSONObject;import android.util.Log;import com.chase.model.Messages;import com.chase.model.MyRes;import com.chase.model.Types;public class JSONArrayToLsit {public static ArrayList<MyRes> setJRToList(JSONArray ja){ JSONObject json=null;MyRes re=null;ArrayList<MyRes> list=new ArrayList<MyRes>();for(int i=0;i<ja.length();i++){try {re=new MyRes();json=ja.getJSONObject(i);re.setRes_id(json.getInt("id"));re.setRes_name(json.getString("name"));re.setRes_form(json.getString("form"));re.setRes_update(json.getString("update"));re.setRes_upuser(json.getString("upuser"));re.setRes_pass(json.getInt("pass"));re.setRes_downsum(json.getInt("downsum"));re.setRes_remark(json.getString("remark"));re.setRes_patch(json.getString("patch"));re.setRes_reco(json.getBoolean("reco"));Log.e("jiexi", json.toString());} catch (JSONException e) {// TODO Auto-generated catch blocke.printStackTrace();}list.add(re);}Log.e("jiexi", list.get(0).getRes_name());return list;}public static ArrayList<Messages> setJMToList(JSONArray ja){ JSONObject json=null;Messages me=null;ArrayList<Messages> list=new ArrayList<Messages>();for(int i=0;i<ja.length();i++){me=new Messages();try {json=ja.getJSONObject(i);me.setMes_id(json.getInt("id"));me.setMes_user(json.getString("reuser"));me.setMes_form(json.getString("form"));me.setMes_date(json.getString("date"));me.setMes_to(json.getString("reto"));me.setMes_content(json.getString("content"));} catch (JSONException e) {// TODO Auto-generated catch blocke.printStackTrace();}list.add(me);}return list;}public static ArrayList<Types> setJTToList(JSONArray ja){ JSONObject json=null;Types ty=null;ArrayList<Types> list=new ArrayList<Types>();for(int i=0;i<ja.length();i++){ty=new Types();try {json=ja.getJSONObject(i);ty.setType_id(json.getInt("id"));ty.setType_name(json.getString("name"));ty.setType_num(json.getInt("num"));} catch (JSONException e) {// TODO Auto-generated catch blocke.printStackTrace();}list.add(ty);}return list;}}。