Android利用Gson库解析复杂结构的JSON数据

来源:互联网 发布:叶子楣 知乎 编辑:程序博客网 时间:2024/06/07 20:16

最近在应用Face++进行人脸识别开发时,经常需要解析Face++返回的结构复杂的JSON数据,于是便决定应用Google开发的Gson库来减轻工作量。

首先给大家看一个比较复杂的JSON数据:

{"face":[{"attribute":{"age":{"range":5,"value":6},"gender":{"confidence":89.7809,"value":"Male"},"race":{"confidence":94.9838,"value":"White"},"smiling":{"value":83.7032}},"face_id":"ae797d3bf784abc8fa27428ec42b19f7","position":{"center":{"x":33.407572,"y":33.333333},"eye_left":{"x":26.288641,"y":35.323167},"eye_right":{"x":34.399777,"y":36.374833},"height":-3.666667,"mouth_left":{"x":27.464365,"y":32.378667},"mouth_right":{"x":33.869265,"y":30.975667},"nose":{"x":27.94343,"y":33.4925},"width":17.371938},"tag":""}],"img_height":600,"img_id":"dc69e22f73aa19d544cd237a4f5ceb15","img_width":449,"session_id":"cd4fa25b83a349a1a52f78789bcfae61","url":null,"response_code":200}

为了让大家更清晰的看清这个层次结构,我把这个JSON数据以树状结构呈现给大家,如下:

{    "face": [        {            "attribute": {                "age": {                    "range": 5,                     "value": 23                },                 "gender": {                    "confidence": 99.9999,                     "value": "Female"                },                 "glass": {                    "confidence": 99.945,                     "value": "None"                },                 "pose": {                    "pitch_angle": {                        "value": 17                    },                     "roll_angle": {                        "value": 0.735735                    },                     "yaw_angle": {                        "value": -2                    }                },                 "race": {                    "confidence": 99.6121,                     "value": "Asian"                },                 "smiling": {                    "value": 4.86501                }            },             "face_id": "17233b4b1b51ac91e391e5afe130eb78",             "position": {                "center": {                    "x": 49.4,                     "y": 37.6                },                 "eye_left": {                    "x": 43.3692,                     "y": 30.8192                },                 "eye_right": {                    "x": 56.5606,                     "y": 30.9886                },                 "height": 26.8,                 "mouth_left": {                    "x": 46.1326,                     "y": 44.9468                },                 "mouth_right": {                    "x": 54.2592,                     "y": 44.6282                },                 "nose": {                    "x": 49.9404,                     "y": 38.8484                },                 "width": 26.8            },             "tag": ""        }    ],     "img_height": 500,     "img_id": "22fd9efc64c87e00224c33dd8718eec7",     "img_width": 500,     "session_id": "38047ad0f0b34c7e8c6efb6ba39ed355",     "url": "http://www.faceplusplus.com.cn/wp-content/themes/faceplusplus/assets/img/demo/1.jpg?v=4"}

接下来就给大家展示如何将JSON数据转换成我们需要的Android对象。

首先我们要定义一个JSON数据对应的POJO,即JavaBeans.java。那么有些人可能不太熟悉JavaBeans的构建,不用担心,只要按以下两个原则,即可轻松构建:

1、[ ]前面的元素定义成List< B > b形式,如,public List< Face > face;而{ }前面的元素定义为 public C c形式即可,如,public String img_id 或 public Attribute attribute。
2、类里面的属性名必须跟JSON字段里面的Key是一模一样的;

所有根据上述规则,我们建立出的JavaBeans.java如下:

public class JavaBeans{    private int img_height;    private int img_width;    private String img_id;    private String session_id;    private String url;    private List<Face> face;  //face属于包含多个元素的JSONArray,所以定义为List,存储这多个元素。而且Face中包含多个属性,故声明为一个内部类,见下方    public int getImg_height() {        return img_height;    }    public int getImg_width() {        return img_width;    }    public String getImg_id() {        return img_id;    }    public String getSession_id() {        return session_id;    }    public String getUrl() {        return url;    }    public List<Face> getFace() {        return face;    }    public class Face {        private Attribute attribute;   //Attribute中包含多个属性,故声明为一个内部类,见下方        private String face_id;        private Position postion;        private String tag;        public String getFace_id() {            return face_id;        }        public Attribute getAttribute() {            return attribute;        }        public Position getPostion() {            return postion;        }        public String getTag() {            return tag;        }        public class Attribute {            private Age age;            private Gender gender;            private Glass glass;            private Pose pose;            private Race race;            private Smiling smiling;            public Age getAge() {                return age;            }            public Gender getGender() {                return gender;            }            public Glass getGlass() {                return glass;            }            public Pose getPose() {                return pose;            }            public Race getRace() {                return race;            }            public Smiling getSmiling() {                return smiling;            }            public class Age {                private int range;                private int value;                public int getRange() {                    return range;                }                public int getValue() {                    return value;                }            }            public class Gender {                private double confidence;                private String value;                public double getConfidence() {                    return confidence;                }                public String getValue() {                    return value;                }            }            public class Glass {                //同Age理,省略            }            public class Pose {                //同Age理,省略            }            public class Race {                //同Age理,省略            }            public class Smiling {                //同Age理,省略            }        }        public class Position {            //同Attribute理,省略        }    }}

最后,就是利用Gson将JSON数据转换为JavaBeans对象了:

JSONObject result = ....;   //从网站获取的JSON数据Gson gson = new Gson();JavaBeans data = gson.fromJson(result.toString(), JavaBeans.class);//获取第一个脸谱的age值int age=data.getFace().get(0).getAttribute().getAge().getValue();
1 0
原创粉丝点击