您的位置:首页 > 编程语言 > Java开发

JAVA-使用FastJson解析复杂JSON数据

2014-03-06 11:04 951 查看
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:
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();
}

}
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.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"}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: