日期:2014-05-20 浏览次数:20960 次
import java.util.Iterator; import org.json.JSONArray; import org.json.JSONException; public class Test{ public static void main(String[] args) throws JSONException{ String s="[{\"id\":\"329\",\"txt\":\"IT\",\"items\":[{\"id\":\"337\",\"txt\":\"机构\"}," + "{\"id\":\"338\",\"txt\":\"机构2\",\"items\":[{id:\"887\",txt:\"内部\"}," + "{id:\"888\",txt:\"内部2\"}]}]},{\"id\":\"345\",\"txt\":\"IT2\"}]"; jsonToList(s); } public static void jsonToList(String str) throws JSONException{ JSONArray ja=new JSONArray(str); for(int i=0;i<ja.length();i++){ Iterator<?> items=ja.getJSONObject(i).keys(); while(items.hasNext()){ String keyName=items.next().toString(); System.out.println(keyName+": "+ja.getJSONObject(i).getString(keyName)); if("items".equals(keyName)) jsonToList(ja.getJSONObject(i).getString(keyName)); } } } }
------解决方案--------------------
public String test3(String arg0) {
List<ATreeClass> objt= JSonUtil.jsonStrToList(arg0,ATreeClass.class);
List<ATreeClass> rslist=new ArrayList<ATreeClass>();
actJson(objt,rslist);
return "";
}
private void actJson(List<ATreeClass> items,List<ATreeClass> rslist){
for(ATreeClass o:items){