您好,登錄后才能下訂單哦!
FastJson簡介
FastJson三個核心類
Maven
Java API
反序列化一個簡單Json字符串
反序列化一個簡單JSON字符串成Java對象組
反序列化一個復雜的JSON字符串
反序列化
序列化
序列化和反序列化日期
JsonObject的一些操作
jsonArray的一些操作
Scala API
demo日志內容
反序列化簡單json字符串
反序列化簡單json字符串組
String處理
List處理
反序列化
JSON協議在日常開發中很常用,包括前后端的數據接口,日志字段的保存等,通常都采用JSON協議。FastJson是阿里的開源框架,很好用,估計開發的同學都有使用過。這里做一個簡單的用法總結,配一些demo。除了Java版本外,由于在Spark也經常清洗日志,所以配上了Scala版本,方便日后查詢使用。完整代碼可以參考Github:https://github.com/tygxy/BigData
JSON:fastjson的解析器,用于json字符串和javaBean、Json對象的轉換
JSONObject:fastJson提供的json對象
JSONArray:fastJson提供json數組對象
<dependency> <groupId>com.alibaba</groupId> <artifactId>fastjson</artifactId> <version>1.2.47</version> </dependency>
反序列化一個簡單Json字符串
public class User { private String name; private int age; public String getName() { return name; } public void setName(String name) { this.name = name; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } }
String jsonString = "{\"name\":\"張三\",\"age\":50}"; User user= JSON.parseObject(jsonString,User.class); System.out.println("name:"+user.getName()+" age:"+user.getAge());// 輸出結果 name:張三 age:50
反序列化操作
創建JavaBean的User類
反序列化一個簡單JSON字符串成Java對象組
String jsonArrayString = "[{\"name\":\"張三\",\"age\":50},{\"name\":\"李四\",\"age\":51}]"; List<User> userList = JSON.parseArray(jsonArrayString,User.class); Iterator it = userList.iterator();while (it.hasNext()) { User u = (User)it.next(); System.out.println("name:"+u.getName()+" age:"+u.getAge()); }// 輸出結果 name:張三 age:50 name:李四 age:51
反序列化一個復雜的JSON字符串
public class Course { private String courseName; private String code; public Course (String courseName, String code){ this.setCourseName(courseName); this.setCode(code); } public String getCourseName() { return courseName; } public void setCourseName(String courseName) { this.courseName = courseName; } public String getCode() { return code; } public void setCode(String code) { this.code = code; } }public class Student { private int id; private String studentName; private int age; public Student(int id, String studentName, int age) { this.setId(id); this.setStudentName(studentName); this.setAge(age); } public int getId() { return id; } public void setId(int id) { this.id = id; } public String getStudentName() { return studentName; } public void setStudentName(String studentName) { this.studentName = studentName; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } }public class Teacher { private String teacherName; private int age; private Course course; private List<Student> students; public Teacher(String teacherName, int age, Course course, List<Student> students) { this.setTeacherName(teacherName); this.setAge(age); this.setCourse(course); this.setStudents(students); } public String getTeacherName() { return teacherName; } public void setTeacherName(String teacherName) { this.teacherName = teacherName; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } public Course getCourse() { return course; } public void setCourse(Course course) { this.course = course; } public List<Student> getStudents() { return students; } public void setStudents(List<Student> students) { this.students = students; } }
String complexJsonString = "{\"teacherName\":\"crystall\",\"age\":27,\"course\":{\"courseName\":\"english\",\"code\":1270},\"students\":[{\"id\":1,\"studentName\":\"lily\",\"age\":12},{\"id\":2,\"studentName\":\"lucy\",\"age\":15}]}"; Teacher teacher = JSON.parseObject(complexJsonString,Teacher.class);
反序列化操作
分別創建JavaBean的Course類、Student類、Teacher類
序列化一個Java Bean對象
User u = new User(); u.setName("王五"); u.setAge(30); System.out.println(JSON.toJSONString(u));// 輸出結果 {"age":30,"name":"王五"} User u1 = new User(); u1.setAge(30); System.out.println(JSON.toJSONString(u1,SerializerFeature.WriteMapNullValue)); // 輸出null,輸出結果 {"age":30,"name":null} System.out.println(JSON.toJSONString(u1,SerializerFeature.WriteNullStringAsEmpty)); // 輸出"",輸出結果 {"age":30,"name":""}
Date date = new Date();String dateString = JSON.toJSONStringWithDateFormat(date, "yyyy-MM-dd HH:mm:ss"); System.out.println(dateString);// 輸出結果 "2018-08-03 09:44:21"String dateString1 = "{\"time\":\"2018-08-01 22:22:22\"}"; System.out.println(JSON.parseObject(dateString1));// 輸出結果 {"time":"2018-08-01 22:22:22"}
String jsonString1 = "{\"name\":\"張三\",\"age\":50}"; JSONObject jsonObject = JSON.parseObject(jsonString1); System.out.println(jsonObject.keySet()); // 輸出key集合,輸出結果 [name, age]if(jsonObject.containsKey("sex")) { // 判斷key是否存在,輸出結果 false System.out.println(true); } else { System.out.println(false); } jsonObject.put("sex","man"); // 添加k/v鍵值對,輸出結果 {"sex":"man","name":"張三","age":50} System.out.println(jsonObject);if (jsonObject.containsValue("man")) { // 判斷value是否存在,輸出結果 false System.out.println(true); } else { System.out.println(false); }
String jsonArrayString1 = "[{\"id\":1,\"studentName\":\"lily\",\"age\":12},{\"id\":2,\"studentName\":\"lucy\",\"age\":15}]"; JSONArray jsonArray = JSON.parseArray(jsonArrayString1);for (int i = 0; i < jsonArray.size(); i++) { // 遍歷輸出 JSONObject jsonObj= jsonArray.getJSONObject(i); System.out.println(jsonObj.get("id")); } Student s3 = new Student(3,"學生乙",15); jsonArray.add(s3); // 添加新jsonobject對象,輸出結果 3System.out.println(jsonArray.size());if(jsonArray.contains(s3)) { // 判斷是否存在,輸出結果 true System.out.println(true); } else { System.out.println(false); }
demo日志內容
{"name":"張三","age":10} {"name":"李四","age":11} {"name":"李四"} {"age":11}
{"data":[{"label":"123","acc":1,"version":"4.3.1"}]} {"data":[{"label":"789","acc":1,"version":"4.3.1"},{"label":"78","acc":100,"version":"4.3.1"}]} {"data":[{"label":"5356","acc":1,"version":"4.3.1"}]}
data1.log
data.log
反序列化簡單json字符串
val spark = SparkSession.builder().master("local[2]").appName("FastJsonTest").getOrCreate() val input1 = "data.log"val jsonRDD1 = spark.sparkContext.textFile(input1) val dataRDD1 = jsonRDD1.map(json => { val jsonObject = JSON.parseObject(json) val name = jsonObject.getOrDefault("name",null) val age = jsonObject.getOrDefault("age",null) (name,age) }) dataRDD1.foreach(println)// 輸出結果 (李四,null) (null,11) (張三,10) (李四,11)
反序列化簡單json字符串組,實現一行變多行地解析json字符串。這個我也沒找到很好的方法,歡迎讀者指教一下
val input2 = "data1.log"val jsonRDD2 = spark.sparkContext.textFile(input2) val dataRDD2 = jsonRDD2.map(json => { JSON.parseObject(json).getJSONArray("data").toString }).map(x => x.substring(1,x.length-1).replace("},{","}---{")) // 去掉字符串中的[],并替換},{成}---{,目的是用于區分 .flatMap(x => x.split("---")) // 字符串按----拆分 .map(x => JSON.parseObject(x)) val data2 = dataRDD2.map(jsonObject => { val version = jsonObject.getOrDefault("version",null) val label = jsonObject.getOrDefault("label",null) val acc = jsonObject.getOrDefault("acc",null) (version,label,acc) }) data2.foreach(println)// 輸出結果(4.3.1,5356,1) (4.3.1,123,1) (4.3.1,789,1) (4.3.1,78,100)
val dataRDD3 = jsonRDD2.flatMap(json => { val jsonArray = JSON.parseObject(json).getJSONArray("data") var dataList : List[String] = List() // 創建一個List for (i <- 0 to jsonArray.size()-1) { dataList = jsonArray.get(i).toString :: dataList } dataList }).map(x => JSON.parseObject(x)) val data3 = dataRDD3.map(jsonObject => { val version = jsonObject.getOrDefault("version",null) val label = jsonObject.getOrDefault("label",null) val acc = jsonObject.getOrDefault("acc",null) (version,label,acc) }) data3.foreach(println)// 輸出結果(4.3.1,5356,1) (4.3.1,123,1) (4.3.1,789,1) (4.3.1,78,100)
方法二:List
方法一:字符串處理
序列化一個簡單java Bean對象
val arr = Seq("tom:10", "bob:14", "hurry:9") val dataRdd = spark.sparkContext.parallelize(arr) val dataString = dataRdd.map(x => { val arr = x.split(":") val name = arr(0) val age = arr(1).toInt val u = new User(name,age) u }).map(x => { JSON.toJSONString(x,SerializerFeature.WriteMapNullValue) // 這里需要顯示SerializerFeature中的某一個,否則會報同時匹配兩個方法的錯誤}) dataString.foreach(println)// 輸出結果{"age":10,"name":"tom"} {"age":14,"name":"bob"} {"age":9,"name":"hurry"}
https://segmentfault.com/a/1190000011212806
https://www.cnblogs.com/cdf-opensource-007/p/7106018.html
https://github.com/alibaba/fastjson
https://blog.csdn.net/universsky2015/article/details/77965563?locationNum=9&fps=1
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。