通过fastjson解析json数据工具

Abina ·
更新时间:2024-11-10
· 820 次阅读

目录
一、原始数据内容
二、通过fastjson解析json数据
一、原始数据内容eventLogJson.txt
{
“u”: {
“cookieid”: “HsOorABPB”,
“account”: “05289”,
“email”: “Fh8h@G4hbi.com”,
“phoneNbr”: “20096655112”,
“birthday”: “2002-01-17”,
“isRegistered”: true,
“isLogin”: true,
“addr”: “qBvdcTyPO”,
“gender”: “F”,
“phone”: {
“imei”: “NNVGIZ”,
“osName”: “ios”,
“osVer”: “10.05”,
“resolution”: “1356*768”,
“androidId”: “”,
“manufacture”: “apple”,
“deviceId”: “0XYAUq”
},
“app”: {
“appid”: “com.51doit.mall”,
“appVer”: “2.0.1”,
“release_ch”: “百度手机助手”,
“promotion_ch”: “网易”
},
“loc”: {
“areacode”: 532324203,
“longtitude”: 100.80869161002043,
“latitude”: 25.134465800837827,
“carrier”: “中国移动”,
“netType”: “3G”,
“cid_sn”: “eCHFxiH4ZPPZ”,
“ip”: “189.60.43.153”
},
“sessionId”: “aNHKnDe44hS0”
},
“logType”: “pg_view”,
“commit_time”: 1560672636617,
“event”: {
“ad_trace_id”: “”,
“is_rec”: “0”,
“pgid”: “001”,
“referal”: “”,
“title”: “搞IT,就要往死里学”,
“skuid”: “”,
“url”: “http://www.51doit.com”
}
}

二、通过fastjson解析json数据
package cn.doitedu.profile.idmp

import com.alibaba.fastjson.{JSON, JSONObject}

import scala.io.Source

/**

@author: 余辉 @blog: https://blog.csdn.net/silentwolfyh @create: 2019-12-15 20:03 @description: 需求: 通过fastjson解析json数据工具
**/
object TestJson {

def main(args: Array[String]): Unit = {
// 读取数据转为字符串
val file = Source.fromFile(“user_profile/dataparse/eventLogJson.txt”).mkString
// 将数据解析成Json对象
val job: JSONObject = JSON.parseObject(file)
// 获取Json中的key-value值
println(job.get(“commit_time”))
// 获取u中的值和Json对象
val u = job.getJSONObject(“u”)
println(u.getString(“cookieid”))
println(u.getJSONObject(“phone”))
// 将event的json对象转为Map
val event = job.getJSONObject(“event”)
import scala.collection.JavaConversions._
val enentMap: Map[String, String] = event.getInnerMap.map(tp => (tp._1, tp._2.toString)).toMap
enentMap.foreach(println)
}
}

————————————————


作者:司马彭于晏



json数据 工具 fastjson

需要 登录 后方可回复, 如果你还没有账号请 注册新账号