FastJson簡介
創(chuàng)新互聯(lián)建站從2013年創(chuàng)立,是專業(yè)互聯(lián)網(wǎng)技術(shù)服務(wù)公司,擁有項(xiàng)目成都網(wǎng)站建設(shè)、做網(wǎng)站網(wǎng)站策劃,項(xiàng)目實(shí)施與項(xiàng)目整合能力。我們以讓每一個夢想脫穎而出為使命,1280元樂亭做網(wǎng)站,已為上家服務(wù),為樂亭各地企業(yè)和個人服務(wù),聯(lián)系電話:13518219792FastJson三個核心類
Maven
Java API
反序列化一個簡單Json字符串
反序列化一個簡單JSON字符串成Java對象組
反序列化一個復(fù)雜的JSON字符串
反序列化
序列化
序列化和反序列化日期
JsonObject的一些操作
jsonArray的一些操作
Scala API
demo日志內(nèi)容
反序列化簡單json字符串
反序列化簡單json字符串組
String處理
List處理
反序列化
JSON協(xié)議在日常開發(fā)中很常用,包括前后端的數(shù)據(jù)接口,日志字段的保存等,通常都采用JSON協(xié)議。FastJson是阿里的開源框架,很好用,估計(jì)開發(fā)的同學(xué)都有使用過。這里做一個簡單的用法總結(jié),配一些demo。除了Java版本外,由于在Spark也經(jīng)常清洗日志,所以配上了Scala版本,方便日后查詢使用。完整代碼可以參考Github:https://github.com/tygxy/BigData
JSON:fastjson的解析器,用于json字符串和javaBean、Json對象的轉(zhuǎn)換
JSONObject:fastJson提供的json對象
JSONArray:fastJson提供json數(shù)組對象
com.alibaba fastjson 1.2.47
反序列化一個簡單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());// 輸出結(jié)果 name:張三 age:50
反序列化操作
創(chuàng)建JavaBean的User類
反序列化一個簡單JSON字符串成Java對象組
String jsonArrayString = "[{\"name\":\"張三\",\"age\":50},{\"name\":\"李四\",\"age\":51}]"; ListuserList = 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()); }// 輸出結(jié)果 name:張三 age:50 name:李四 age:51
反序列化一個復(fù)雜的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 Liststudents; public Teacher(String teacherName, int age, Course course, List 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 getStudents() { return students; } public void setStudents(List 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);
反序列化操作
分別創(chuàng)建JavaBean的Course類、Student類、Teacher類
序列化一個Java Bean對象
User u = new User(); u.setName("王五"); u.setAge(30); System.out.println(JSON.toJSONString(u));// 輸出結(jié)果 {"age":30,"name":"王五"} User u1 = new User(); u1.setAge(30); System.out.println(JSON.toJSONString(u1,SerializerFeature.WriteMapNullValue)); // 輸出null,輸出結(jié)果 {"age":30,"name":null} System.out.println(JSON.toJSONString(u1,SerializerFeature.WriteNullStringAsEmpty)); // 輸出"",輸出結(jié)果 {"age":30,"name":""}
Date date = new Date();String dateString = JSON.toJSONStringWithDateFormat(date, "yyyy-MM-dd HH:mm:ss"); System.out.println(dateString);// 輸出結(jié)果 "2018-08-03 09:44:21"String dateString1 = "{\"time\":\"2018-08-01 22:22:22\"}"; System.out.println(JSON.parseObject(dateString1));// 輸出結(jié)果 {"time":"2018-08-01 22:22:22"}
String jsonString1 = "{\"name\":\"張三\",\"age\":50}"; JSONObject jsonObject = JSON.parseObject(jsonString1); System.out.println(jsonObject.keySet()); // 輸出key集合,輸出結(jié)果 [name, age]if(jsonObject.containsKey("sex")) { // 判斷key是否存在,輸出結(jié)果 false System.out.println(true); } else { System.out.println(false); } jsonObject.put("sex","man"); // 添加k/v鍵值對,輸出結(jié)果 {"sex":"man","name":"張三","age":50} System.out.println(jsonObject);if (jsonObject.containsValue("man")) { // 判斷value是否存在,輸出結(jié)果 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,"學(xué)生乙",15); jsonArray.add(s3); // 添加新jsonobject對象,輸出結(jié)果 3System.out.println(jsonArray.size());if(jsonArray.contains(s3)) { // 判斷是否存在,輸出結(jié)果 true System.out.println(true); } else { System.out.println(false); }
demo日志內(nèi)容
{"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)// 輸出結(jié)果 (李四,null) (null,11) (張三,10) (李四,11)
反序列化簡單json字符串組,實(shí)現(xiàn)一行變多行地解析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("},{","}---{")) // 去掉字符串中的[],并替換},{成}---{,目的是用于區(qū)分 .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)// 輸出結(jié)果(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() // 創(chuàng)建一個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)// 輸出結(jié)果(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中的某一個,否則會報(bào)同時(shí)匹配兩個方法的錯誤}) dataString.foreach(println)// 輸出結(jié)果{"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
另外有需要云服務(wù)器可以了解下創(chuàng)新互聯(lián)scvps.cn,海內(nèi)外云服務(wù)器15元起步,三天無理由+7*72小時(shí)售后在線,公司持有idc許可證,提供“云服務(wù)器、裸金屬服務(wù)器、高防服務(wù)器、香港服務(wù)器、美國服務(wù)器、虛擬主機(jī)、免備案服務(wù)器”等云主機(jī)租用服務(wù)以及企業(yè)上云的綜合解決方案,具有“安全穩(wěn)定、簡單易用、服務(wù)可用性高、性價(jià)比高”等特點(diǎn)與優(yōu)勢,專為企業(yè)上云打造定制,能夠滿足用戶豐富、多元化的應(yīng)用場景需求。