ThinkSSL🔒 一键申购 5分钟快速签发 30天无理由退款 购买更放心 广告
[TOC] ## 1. JSON基础 JSON(JavaScript Object Notation, JS 对象简谱) 是一种轻量级的数据交换格式。它基于 ECMAScript (欧洲计算机协会制定的js规范)的一个子集,采用完全独立于编程语言的文本格式来存储和表示数据。简洁和清晰的层次结构使得 JSON 成为理想的数据交换语言。 易于人阅读和编写,同时也易于机器解析和生成,并有效地提升网络传输效率。 ### 1.1 JSON数据类型 1、数字(整型、浮点数、定点数); 2、字符和字符串; 3、布尔类型。 还有其他数据类型: 一、对象; 二、null; 三、数组。 ## 2. fastjson ### 2.1 FastJson的特点: 1.FastJson数度快,无论序列化和反序列化,都是当之无愧的fast 2.功能强大(支持普通JDK类包括任意Java Bean Class、Collection、Map、Date或enum) 3.零依赖(没有依赖其它任何类库) 1.3.FastJson的简单说明: **FastJson对于json格式字符串的解析主要用到了下面三个类:** 1.JSON:fastJson的解析器,用于JSON格式字符串与JSON对象及javaBean之间的转换 2.JSONObject:fastJson提供的json对象,可以将java字符串转化成json对象 3.JSONArray:fastJson提供json数组对象 ### 2.2 使用 #### 2.2.1 String转Json对象 依赖: ``` ``` 1. 字符串 ~~~ private static final String JSON_OBJ_STR = "{\"studentName\":\"tuna\",\"studentAge\":28}"; ~~~ 2. JSONObject.parseObject(String) 转换 ~~~ @Test public void testJSONStrToJSONObject() { JSONObject jsonObject = JSONObject.parseObject(JSON_OBJ_STR); System.out.println("================将字符串转成json对象================"); System.out.println("studentName: " + jsonObject.getString("studentName") + ":" + " studentAge: " + jsonObject.getInteger("studentAge")); String jsonString = jsonObject.toJSONString(); System.out.println("================将json对象转成字符串================"); System.out.println(jsonString); } ~~~ #### 2.2.2 String转成javabean 1. 实体类 ~~~ package com.aixin.tuna.json; /** * Created by dailin on 2018/7/10. */ public class Student { private String studentName; private int studentAge; public String getStudentName() { return studentName; } public void setStudentName(String studentName) { this.studentName = studentName; } public int getStudentAge() { return studentAge; } public void setStudentAge(int studentAge) { this.studentAge = studentAge; } @Override public String toString() { return "Student{" + "studentName='" + studentName + '\'' + ", studentAge=" + studentAge + '}'; } } ~~~ 2. 转换 JSONObject.parseObject(String,Class); ~~~ private static final String JSON_OBJ_STR = "{\"studentName\":\"tuna\",\"studentAge\":28}"; Student student = JSONObject.parseObject(JSON_OBJ_STR,Student.class); System.out.println(student.toString()); ~~~ #### 2.2.3 String转成Map ~~~ @Test public void test2(){ Map<String, Object> paramsListMap = JSON.parseObject(JSON_OBJ_STR,new TypeReference<Map<String, Object>>(){}); Set<Map.Entry<String, Object>> entries = paramsListMap.entrySet(); for (Map.Entry<String,Object> v:entries){ System.out.println(v.getKey() + ":" + v.getValue()); } } ~~~ 输出 ~~~ {studentAge=28, studentName=tuna} studentAge:28 studentName:tuna ~~~ #### 2.2.4 javabean转Json字符串 ~~~ @Test public void test3(){ Student student = new Student("tuna", 12); String jsonString = JSONObject.toJSONString(student); System.out.println(jsonString); } ~~~ ~~~ {"studentAge":12,"studentName":"tuna"} ~~~ #### 2.2.5 JavaList到JsonArray的转换 数组字符串转JsonArray,J ~~~ /** * JavaList到JsonArray的转换 */ @Test public void testJavaListToJsonArray() { //已知JavaList Student student = new Student("tuna", 12); Student studenttwo = new Student("huanhuan", 15); List<Student> students = new ArrayList<Student>(); students.add(student); students.add(studenttwo); //方式一 String jsonString = JSONArray.toJSONString(students); JSONArray jsonArray = JSONArray.parseArray(jsonString); System.out.println(jsonArray); //方式二 JSONArray jsonArray1 = (JSONArray) JSONArray.toJSON(students); System.out.println(jsonArray1); } ~~~ 输出 ~~~ [{"studentAge":12,"studentName":"tuna"},{"studentAge":15,"studentName":"huanhuan"}] [{"studentAge":12,"studentName":"tuna"},{"studentAge":15,"studentName":"huanhuan"}] ~~~