List<Person> list = new ArrayList<Person>(); list.add(new Person("张三","123")); list.add(new Person("张2三","sdf")); list.add(new Person("张3三","fd")); JSONArray json1 = JSONArray.fromObject(list);原文链接:https://www.f2er.com/json/290398.htmlSystem.out.println(json1.toString());
Person pe = new Person("李四","lal");
JSONObject json3 = JSONObject.fromObject(pe);
System.out.println(json3.toString());
JSONArray.fromObject可以将list或者list<T>直接转换为JSON
JSONObject.fromObjec可以将一个对象直接转换为JSON