1,先引用maven
<dependency> <groupId>com.alibaba</groupId> <artifactId>fastjson</artifactId> <version>1.2.41</version> </dependency>
2,解析JSON数组
import java.util.Map; import com.alibaba.fastjson.JSON; import com.alibaba.fastjson.JSONArray; import com.alibaba.fastjson.JSONObject; import com.alibaba.fastjson.TypeReference; import com.example.demo.common.Person; public class JsonLib { //json字符串-简单对象型 private static final String JSON_OBJ_STR = "{"studentName":"lily","studentAge":12}"; //json字符串-数组类型 private static final String JSON_ARRAY_STR = "[{"studentName":"lily","studentAge":12},{"studentName":"lucy","studentAge":15}]"; //复杂格式json字符串 private static final String COMPLEX_JSON_STR = "{"teacherName":"crystall","teacherAge":27,"course":{"courseName":"english","code":1270},"students":[{"studentName":"lily","studentAge":12},{"studentName":"lucy","studentAge":15}]}"; @SuppressWarnings("unchecked") public static void main(String[] args) { //demoJson(); //testJSONStrToJSONObject();//json字符串转化对象 //testJSONStrToJSONArray();//json数组转化json对象 testComplexJSONStrToJSONObject();//json对象嵌套json对象 } /** * 复杂json格式字符串与JSONObject之间的转换 */ public static void testComplexJSONStrToJSONObject(){ System.out.println(COMPLEX_JSON_STR); JSONObject jsonObject = JSON.parseObject(COMPLEX_JSON_STR); //JSONObject jsonObject1 = JSONObject.parseObject(COMPLEX_JSON_STR);//因为JSONObject继承了JSON,所以这样也是可以的 System.out.println(jsonObject); String teacherName = jsonObject.getString("teacherName"); Integer teacherAge = jsonObject.getInteger("teacherAge"); JSONObject course = jsonObject.getJSONObject("course"); JSONArray students = jsonObject.getJSONArray("students"); System.out.println(teacherName+"------"+teacherAge+"===json对象===="+course+"----json数组----"+students); JSONArray jsonArray = JSON.parseArray(students.toString()); System.out.println(jsonArray); } /** * json字符串-数组类型与JSONArray之间的转换 */ public static void testJSONStrToJSONArray(){ JSONArray jsonArray = JSON.parseArray(JSON_ARRAY_STR); //JSONArray jsonArray1 = JSONArray.parseArray(JSON_ARRAY_STR);//因为JSONArray继承了JSON,所以这样也是可以的 //遍历方式1 int size = jsonArray.size(); for (int i = 0; i < size; i++){ JSONObject jsonObject = jsonArray.getJSONObject(i); System.out.println(jsonObject.getString("studentName")+":"+jsonObject.getInteger("studentAge")); } //遍历方式2 for (Object obj : jsonArray) { JSONObject jsonObject = (JSONObject) obj; System.out.println(jsonObject.getString("studentName")+":"+jsonObject.getInteger("studentAge")); } } /** * json字符串-简单对象型与JSONObject之间的转换 */ public static void testJSONStrToJSONObject(){ JSONObject jsonObject = JSON.parseObject(JSON_OBJ_STR); //JSONObject jsonObject1 = JSONObject.parseObject(JSON_OBJ_STR); //因为JSONObject继承了JSON,所以这样也是可以的 System.out.println(jsonObject.getString("studentName")+":"+jsonObject.getInteger("studentAge")); } public static void demoJson() { /** * 将 Json 形式的字符串转换为 Map */ String str = "{"name":"Tom","age":90}"; JSONObject jsonObject = JSONObject.parseObject(str); Map<String, String> params = JSONObject.parseObject(jsonObject.toString(), new TypeReference<Map<String, String>>(){}); System.out.println(params); /** * 将 Json 形式的字符串转换为 JavaBean */ str = "{"id":"A001","name":"Jack"}"; jsonObject = JSONObject.parseObject(str); System.out.println(jsonObject); Person person = JSON.parseObject(str, new TypeReference<Person>() {}); System.out.println(person.toString()); } }
3,json数组转map
public static void main(String[] args){ String strArr = "[{"0":"zhangsan","1":"lisi","2":"wangwu","3":"maliu"}," + "{"00":"zhangsan","11":"lisi","22":"wangwu","33":"maliu"}]"; //第一种方式 List<Map<String,String>> listObjectFir = (List<Map<String,String>>) JSONArray.parse(strArr); System.out.println("利用JSONArray中的parse方法来解析json数组字符串"); for(Map<String,String> mapList : listObjectFir){ for (Map.Entry entry : mapList.entrySet()){ System.out.println( entry.getKey() + " " +entry.getValue()); } } //第二种方式 List<Map<String,String>> listObjectSec = JSONArray.parseObject(strArr,List.class); System.out.println("利用JSONArray中的parseObject方法并指定返回类型来解析json数组字符串"); for(Map<String,String> mapList : listObjectSec){ for (Map.Entry entry : mapList.entrySet()){ System.out.println( entry.getKey() + " " +entry.getValue()); } } //第三种方式 JSONArray listObjectThir = JSONArray.parseArray(strArr); System.out.println("利用JSONArray中的parseArray方法来解析json数组字符串"); for(Object mapList : listObjectThir){ for (Object entry : ((Map)mapList).entrySet()){ System.out.println(((Map.Entry)entry).getKey() + " " +((Map.Entry)entry).getValue()); } } //第四种方式 List listObjectFour = JSONArray.parseArray(strArr,Map.class); System.out.println("利用JSONArray中的parseArray方法并指定返回类型来解析json数组字符串"); for(Object mapList : listObjectFour){ for (Object entry : ((Map)mapList).entrySet()){ System.out.println(((Map.Entry)entry).getKey() + " " +((Map.Entry)entry).getValue()); } } //第五种方式 JSONArray listObjectFifth = JSONObject.parseArray(strArr); System.out.println("利用JSONObject中的parseArray方法来解析json数组字符串"); for(Object mapList : listObjectFifth){ for (Object entry : ((Map)mapList).entrySet()){ System.out.println(((Map.Entry)entry).getKey() + " " +((Map.Entry)entry).getValue()); } } //第六种方式 List listObjectSix = JSONObject.parseArray(strArr,Map.class); System.out.println("利用JSONObject中的parseArray方法并指定返回类型来解析json数组字符串"); for(Object mapList : listObjectSix){ for (Object entry : ((Map)mapList).entrySet()){ System.out.println(((Map.Entry)entry).getKey() + " " +((Map.Entry)entry).getValue()); } } //第七种方式 JSONArray listObjectSeven = JSON.parseArray(strArr); System.out.println("利用JSON中的parseArray方法来解析json数组字符串"); for(Object mapList : listObjectSeven){ for (Object entry : ((Map)mapList).entrySet()){ System.out.println(((Map.Entry)entry).getKey() + " " +((Map.Entry)entry).getValue()); } } //第八种方式 List listObjectEigh = JSONObject.parseArray(strArr,Map.class); System.out.println("利用JSON中的parseArray方法并指定返回类型来解析json数组字符串"); for(Object mapList : listObjectEigh){ for (Object entry : ((Map)mapList).entrySet()){ System.out.println(((Map.Entry)entry).getKey() + " " +((Map.Entry)entry).getValue()); } } }
4、map转json字符串
public static void main(String[] args) { Map map = new HashMap(); map.put("msg", "yes");//map里面装有yes JSONObject jsonObject = JSONObject.fromObject(map); System.out.println("输出的结果是:" + jsonObject); //3、将json对象转化为json字符串 String result = jsonObject.toString(); System.out.println(result); }