The core of Fastjson parsing JSON is the direct conversion of JSON data into objects, making parsing complex JSON data very simple and significantly reducing the likelihood of error.
How to use:
1. Create the JSON data object, all the JSON key values use key as the variable name, and implement the Set,get function.
Let's try a simple demo to test the complex JSON data.
Engineering Code
Engineering Structure:
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": One, "Range": 5}}, "tag": "", "face_id": "403f40726858a195ea3aec08c4f05354"}], "session_id": " 6cdc4eac5f86433f9ce041993df1555e "," Img_height ": 1001," Img_width ": M," img_id ":" dc6e6d69d669e4bf20a111f58361fb3e "," url ":" http:\/\/d.hiphotos.baidu.com\/image\/w%3d2048\/sign=618830d52c738bd4c421b53195b386d6\/ 3c6d55fbb2fb4316d61e357222a4462309f7d3b1.jpg "," Response_code ": 200}
Code Demo.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 (); c29/>} return
null;
}
/**
* @param args */public
static void Main (string[] args) {
new Demo ();
}
}
Facesresult.java:
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.f
Ace ";
List corresponding array list<face> face = new arraylist<face> ();
String session_id;
int img_height;
int img_width;
String img_id;
String URL;
String Response_code;
* * All member variables are to implement 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;
The public void setimg_height (int img_height) {this.img_height = Img_height;
public int getimg_width () {return img_width;
The 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 Attributes 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;
}
Run the output:
{"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": One, "Range": 5}}, "tag": "", "face_id": "403f40726858a195ea3aec08c4f05354"}], "session_id": " 6cdc4eac5f86433f9ce041993df1555e "," Img_height ": 1001," Img_width ": M," 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 ":" One "}," 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 ":", "Response_code": "", "session_id": " 6cdc4eac5f86433f9ce041993df1555e "," url ":" Http://d.hiphotos.baidu.com/image/w%3D2048/sign= 618830d52c738bd4c421b53195b386d6/3c6d55fbb2fb4316d61e357222a4462309f7d3b1.jpg "}