1.使用JSON时要导入几个包:json-lib-2.2-jdk15.jar,几个依赖包:commons-beanutils.jar,commons-httpclient.jar,commons-lang.jar,ezmorph.jar,
2.JSONArray.fromObject(o)可以将list或object转成json格式。
3.在JFinal中可能不能使用JSONArray.fromObject(Admin),可能是因为jfial的Db+Record模式不支持转换。(遗留问题)
import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; import com.zzu.model.Admin; import net.sf.json.JSONArray; import net.sf.json.JSONObject; public class JasonUnit{ public static void main(String[] args) { List list1 = new ArrayList(); list1.add("first"); list1.add("second"); JSONArray jsonArray1 = JSONArray.fromObject(list1); System.out.println("List list1"); System.out.println(jsonArray1); List<Admin> list2 = new ArrayList<Admin>(); list2.add(new Admin("xiapi1","男","1",1,10)); list2.add(new Admin("xiapi2","女",11)); list2.add(new Admin("xiapi3",12)); JSONArray jsonArray2 = JSONArray.fromObject(list2); System.out.println("List<Student> list2"); System.out.println(jsonArray2); } }原文链接:https://www.f2er.com/json/289765.html