JAVA-使用FastJson解析複雜JSON資料
FastJson解析JSON的核心就是把json資料直接轉換成物件,讓解析複雜的json資料變得非常簡單,較大的降低了出錯的可能性。
使用方法:
1.建立JSON資料物件,所有的json鍵值都使用key作為變數名,並且實現set,get函式。
下面試簡單的demo測試複雜的json資料。
工程結構:
files/json.txt:
{"face":[{"position":{"mouth_right":{"y":21.687167,"x":57.765155},"mouth_left":{"y":20.749833,"x":52.425537},"center":{"y":18.583333,"x":56.205251},"height":10.166667,"width":14.558473,"nose":{"y":18.785333,"x":56.317422},"eye_left":{"y":14.81305,"x":52.951074},"eye_right":{"y":16.054433,"x":60.904535}},"attribute":{"race":{"value":"Asian","confidence":90.348},"gender":{"value":"Female","confidence":99.9999},"smiling":{"value":0.490444},"age":{"value":11,"range":5}},"tag":"","face_id":"403f40726858a195ea3aec08c4f05354"}],"session_id":"6cdc4eac5f86433f9ce041993df1555e","img_height":1001,"img_width":700,"img_id":"dc6e6d69d669e4bf20a111f58361fb3e","url":"http:\/\/d.hiphotos.baidu.com\/image\/w%3D2048\/sign=618830d52c738bd4c421b53195b386d6\/3c6d55fbb2fb4316d61e357222a4462309f7d3b1.jpg","response_code":200}
程式碼Demo.java:
FacesResult.java:package com.jyc; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.IOException; import com.alibaba.fastjson.JSON; public class Demo { public Demo(){ String json = readFile("files/json.txt"); System.out.println(json); FacesResult result = JSON.parseObject(json, FacesResult.class); System.out.println(JSON.toJSONString(result)); } public String readFile(String file){ try { FileInputStream fis = new FileInputStream(file); StringBuffer sb = new StringBuffer(); int len = 0; while(true){ byte[] b = new byte[1024]; len = fis.read(b); if(len<0){ break; } sb.append(new String(b,0, len)); } return sb.toString(); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } return null; } /** * @param args */ public static void main(String[] args) { new Demo(); } }
package com.jyc; import java.util.ArrayList; import java.util.List; /** * @author jiangyuchen * @date 2014-3-6 */ public class FacesResult { private static final String TAG = "JavaTest.Face"; //List對應陣列 List<Face> face = new ArrayList<Face>(); String session_id; int img_height; int img_width; String img_id; String url; String response_code; /* *所有的成員變數都要實現set get */ public List<Face> getFace() { return face; } public void setFace(List<Face> face) { this.face = face; } public String getSession_id() { return session_id; } public void setSession_id(String session_id) { this.session_id = session_id; } public int getImg_height() { return img_height; } public void setImg_height(int img_height) { this.img_height = img_height; } public int getImg_width() { return img_width; } public void setImg_width(int img_width) { this.img_width = img_width; } public String getImg_id() { return img_id; } public void setImg_id(String img_id) { this.img_id = img_id; } public String getUrl() { return url; } public void setUrl(String url) { this.url = url; } public String getResponse_code() { return response_code; } public void setResponse_code(String response_code) { this.response_code = response_code; } }
Face.java:
package com.jyc;
/**
* @author jiangyuchen
* @date 2014-3-6
*/
public class Face {
private static final String TAG = "JavaTest.Face";
FacePositions position;
FaceAttributes attribute;
String tag;
String face_id;
public FacePositions getPosition() {
return position;
}
public void setPosition(FacePositions position) {
this.position = position;
}
public FaceAttributes getAttribute() {
return attribute;
}
public void setAttribute(FaceAttributes attribute) {
this.attribute = attribute;
}
public String getTag() {
return tag;
}
public void setTag(String tag) {
this.tag = tag;
}
public String getFace_id() {
return face_id;
}
public void setFace_id(String face_id) {
this.face_id = face_id;
}
public class FacePositions {
Position mouth_right;
Position mouth_left;
Position center;
Position nose;
Position eye_left;
Position eye_right;
float height;
float width;
public Position getMouth_right() {
return mouth_right;
}
public void setMouth_right(Position mouth_right) {
this.mouth_right = mouth_right;
}
public Position getMouth_left() {
return mouth_left;
}
public void setMouth_left(Position mouth_left) {
this.mouth_left = mouth_left;
}
public Position getCenter() {
return center;
}
public void setCenter(Position center) {
this.center = center;
}
public Position getNose() {
return nose;
}
public void setNose(Position nose) {
this.nose = nose;
}
public Position getEye_left() {
return eye_left;
}
public void setEye_left(Position eye_left) {
this.eye_left = eye_left;
}
public Position getEye_right() {
return eye_right;
}
public void setEye_right(Position eye_right) {
this.eye_right = eye_right;
}
public float getHeight() {
return height;
}
public void setHeight(float height) {
this.height = height;
}
public float getWidth() {
return width;
}
public void setWidth(float width) {
this.width = width;
}
}
public class FaceAttributes {
Attribute race;
Attribute gender;
Attribute smiling;
Attribute age;
public Attribute getRace() {
return race;
}
public void setRace(Attribute race) {
this.race = race;
}
public Attribute getGender() {
return gender;
}
public void setGender(Attribute gender) {
this.gender = gender;
}
public Attribute getSmiling() {
return smiling;
}
public void setSmiling(Attribute smiling) {
this.smiling = smiling;
}
public Attribute getAge() {
return age;
}
public void setAge(Attribute age) {
this.age = age;
}
public class Attribute{
public String getValue() {
return value;
}
public void setValue(String value) {
this.value = value;
}
public float getConfidence() {
return confidence;
}
public void setConfidence(float confidence) {
this.confidence = confidence;
}
public int getRange() {
return range;
}
public void setRange(int range) {
this.range = range;
}
String value;
float confidence;
int range;
}
}
}
Position.java:
package com.jyc;
/**
* @author jiangyuchen
* @date 2014-3-6
*/
public class Position {
private static final String TAG = "JavaTest.Position";
public float getX() {
return x;
}
public void setX(float x) {
this.x = x;
}
public float getY() {
return y;
}
public void setY(float y) {
this.y = y;
}
float x;
float y;
}
執行輸出:
{"face":[{"position":{"mouth_right":{"y":21.687167,"x":57.765155},"mouth_left":{"y":20.749833,"x":52.425537},"center":{"y":18.583333,"x":56.205251},"height":10.166667,"width":14.558473,"nose":{"y":18.785333,"x":56.317422},"eye_left":{"y":14.81305,"x":52.951074},"eye_right":{"y":16.054433,"x":60.904535}},"attribute":{"race":{"value":"Asian","confidence":90.348},"gender":{"value":"Female","confidence":99.9999},"smiling":{"value":0.490444},"age":{"value":11,"range":5}},"tag":"","face_id":"403f40726858a195ea3aec08c4f05354"}],"session_id":"6cdc4eac5f86433f9ce041993df1555e","img_height":1001,"img_width":700,"img_id":"dc6e6d69d669e4bf20a111f58361fb3e","url":"http:\/\/d.hiphotos.baidu.com\/image\/w%3D2048\/sign=618830d52c738bd4c421b53195b386d6\/3c6d55fbb2fb4316d61e357222a4462309f7d3b1.jpg","response_code":200}
{"face":[{"attribute":{"age":{"confidence":0,"range":5,"value":"11"},"gender":{"confidence":99.9999,"range":0,"value":"Female"},"race":{"confidence":90.348,"range":0,"value":"Asian"},"smiling":{"confidence":0,"range":0,"value":"0.490444"}},"face_id":"403f40726858a195ea3aec08c4f05354","position":{"center":{"x":56.20525,"y":18.583332},"eye_left":{"x":52.951073,"y":14.81305},"eye_right":{"x":60.904533,"y":16.054434},"height":10.166667,"mouth_left":{"x":52.425537,"y":20.749832},"mouth_right":{"x":57.765156,"y":21.687166},"nose":{"x":56.31742,"y":18.785334},"width":14.558473},"tag":""}],"img_height":1001,"img_id":"dc6e6d69d669e4bf20a111f58361fb3e","img_width":700,"response_code":"200","session_id":"6cdc4eac5f86433f9ce041993df1555e","url":"http://d.hiphotos.baidu.com/image/w%3D2048/sign=618830d52c738bd4c421b53195b386d6/3c6d55fbb2fb4316d61e357222a4462309f7d3b1.jpg"}
相關推薦
JAVA-使用FastJson解析複雜JSON資料
FastJson解析JSON的核心就是把json資料直接轉換成物件,讓解析複雜的json資料變得非常簡單,較大的降低了出錯的可能性。 使用方法: 1.建立JSON資料物件,所有的json鍵值都使用key作為變數名,並且實現set,get函式。 下面試簡單的demo測試複雜的
java解析複雜json資料
java如何解析複雜的json資料 關於json處理的包有好幾個,比如jackson、Gson、Fastjson。Gson是谷歌做的,功能強大;Fastjson是阿里巴巴做的,效能更快。具體用哪個,開心就好。我這裡兩個都沒用,用的是java的一個類庫—json-
Gson解析複雜Json資料
implementation'com.google.code.gson:gson:2.8.0' 然後根據api獲取到的json資料 { "status": "1", "info": "OK", "infocode": "10000",
通過GSON解析複雜json資料(二)
這裡我們依舊用上文中的 json 字串 , 由於轉換為 map 依舊需要 javaBean , 所有我們的實體物件也不發生改變 這裡我們需要活用 java 反射和型別對比實現需求 先看程式碼 package com.jacx.test.test01.
java dom4j解析複雜xml成json
import com.alibaba.fastjson.JSONArray; import com.alibaba.fastjson.JSONObject; import org.dom4j.Document; import org.dom4j.Elemen
Java解析(讀取)Json資料
以前看過書上說,XML是web service間傳輸資訊的標準格式吧,就看了看XML。最近在做個網站,又說是有了JSON,第一回聽說就看了看,總結總結一下。 1.JSON介紹 JSON比XML簡單,主要體現在傳輸相同資訊的情況下,檔案的大小不同。 JSON只用於傳輸資訊,
Java post請求傳送json資料在filter中解析方法
大家都知道Java的servlet分get和post請求方式,在servlet或者在集成了springMVC、Struts2的框架的情況下獲取請求的引數。那麼有時候我們需要在攔截其中獲取ServletRequest的引數就不那麼容易了。因為在ServletRequst中,如果是get請求我們可以通過reque
android客戶端+JAVA WEB伺服器實現json資料解析
import android.os.Handler; import android.os.Message; import android.support.v7.app.AppCompatActivity; import android.view.View; import android.widget.But
java解析複雜json:JSONObject 和 JSONArray的使用
在正式解析之前,我們需要下載解析Json所需要的jar包,一共有7個。下載地址如下:https://download.csdn.net/download/zai_xia/10374080大家也可以自行找資源下載。然後將這些Jar包 Build Path 進專案就好了。特別注意
java中解析一個json串,本文詳細介紹了安卓原生的解析以及用fastjson解析
在java或安卓中解析JSON有很多種方式,可以用谷歌給安卓提供的原生的解析方式解析安卓,也可以選擇第三方類庫,一些比較流行的解析方式如fastjson,Gson等,本文就簡單介紹一下java工程中fastjson解析json資料。在介紹fastjson解析之前,我想先給大
java遍歷複雜json字串獲取想要的資料
https://blog.csdn.net/qq_34309663/article/details/80508125 java如何解析複雜的json資料關於json處理的包有好幾個,比如jackson、Gson、Fastjson。Gson是谷歌做的,功能強大;Fastjson
複雜json資料的解析
public class JSONDetail { private int state; private List<err> err; private JSONDetial2 data; public int getState() { return state; } publ
Java解析(讀取)Json資料{}、[{}]
以前看過書上說,XML是web service間傳輸資訊的標準格式吧,就看了看XML。最近在做個網站,又說是有了JSON,第一回聽說就看了看,總結總結一下。 1.JSON介紹 JSON比XML簡單,主要體現在傳輸相同資訊的情況下,檔案的大小不同。 JSON只用於傳輸資訊,XML還可以用於配置檔案的使
json資料解析,json資料轉為java物件
在Android開發過程中,經常需要與後臺進行資料的互動,JSON作為一種輕量級的資料格式,經常被後臺作為傳輸資料的格式,將資料傳輸到客戶端。JSON有兩種格式,一種是物件格式的,另一種是陣列格式的。 下面是一組json字串: String json="{"resultc
java使用jackson解析複雜json字串
為什麼要用 jackson jackson 憑藉其簡潔的語法、高效的處理速度、豐富的功能、清晰的文件等眾多優勢,受到廣大開發者的熱愛,成為了java程式設計師處理json資料的不二選擇。 1 實戰需求 在電商專案中,訂單物流查詢模組,需要接入多方快遞 API
FastJson 解析複雜多層資料
工作中遇到了一個關於解析Json串的問題 先上串{ "timestamp": 1520941696, "object": { "_id": { "$oid": "5aa7ba802c668f27832193d0"
fastjson解析超長json串以及轉成list,map等方法實例
log 方法 其他 sonar 分享 分號 mage pla sina 今天在工作中出現了這麽一個問題,在手機桌面的app加密解密後,字符串超長,讀取兩三個都會出現內存溢出的現象,最後只能根據id累加來一個個解析.解析出來的數據最後在進行處理. 轉換成對象使用的是阿裏巴
Json串的解析—將Json資料歸納格式
Json串如下所示 { "dataId": 123, "dataType": "mysql", "resultData": [{ "binlog": "mysql_binlog.000", "column": [{ "columnname": "single_clou
Android 解析本地Json資料
文章目錄 1、簡介 2、Json 環境的搭建 3、檔案結構 1、簡介 1)解析本地的JSON資料 2)對Json 資料的解析方法有所瞭解 2、Json 環
Android Retrofit2框架的使用,以及解析複雜Json(其實也不算太複雜)
Retrofit是Square公司的開源專案,是基於OKHttp進行的應用層封裝 Retrofit官方文件:http://square.github.io/retrofit/ Retrofit GitHub地址:https://github.com/square/retro