导包:FastJson
<dependency><groupId>com.alibaba</groupId><artifactId>fastjson</artifactId><version>1.2.60</version>
</dependency>
测试:偏重于把java转为json
注:有时候JSON出不来,要手动 import com.alibaba.fastjson.JSON;
"java对象,转化为json字符串":Ajax ajax = new Ajax("Ajax=" + "罗德", 11, "男");System.out.println(JSON.toJSONString(ajax));//{"age":11,"name":"Ajax=罗德","sex":"男"}"java对象转化为json对象"Ajax ajax = new Ajax("Ajax=" + "罗德", 11, "男");Object o = JSON.toJSON(ajax);//此时的o就是json对象了"验证一下,==和equals":都是true,toJSON就是直接转化为json对象的JSONObject o1 = (JSONObject) o;System.out.println(o==o1);//trueSystem.out.println(o.equals(o1));//true"万物皆可对象,java对象(集合,数组形式的对象)转化为json数组":必须将先转为json字符串,在转数组
List<Ajax> list = new ArrayList<>();
String jsonString = JSON.toJSONString(list);
JSONArray jsonArray = JSON.parseArray(jsonString);