Android 解析JSON数据

Android 解析JSON数据使用JSONObject解析JSON数据,通过GSON去解析JSON数据。

Android

第一种使用JSONObject解析JSON数据,这是Android中最基本的数据解析方式

fromJson("{'status':200," +
                    "'message':'ok'," +
                    "'people':{" +
                    "'username':'LiLy'," +
                    "'phone':'13189720413'," +
                    "'headerImg':'https://img0.baidu.com/it/u=1521420901,2700109129&fm=253&fmt=auto&app=138&f=JPEG?w=500&h=313'}}");
private void fromJson(String s) {
        Me me=new Me();
        JSONObject jsonObject= null;
        try {
            jsonObject = new JSONObject(s);
            int status=jsonObject.optInt("status");
            me.setStatus(status);

            String message=jsonObject.optString("message");
            me.setMessage(message);

            JSONObject uesrJson=jsonObject.optJSONObject("people");
            String username=uesrJson.optString("username");
            String phone=uesrJson.optString("phone");
            String headerImg=uesrJson.optString("headerImg");
            me.setPeople(new People(username,phone,headerImg));

            Log.d("","Json======"+status+"\t"+message+"\t"+username+"\t"+phone+"\t"+headerImg);

        } catch (JSONException e) {
            e.printStackTrace();
        }
    }

第二种方式通过GSON去解析JSON数据

 同样的数据格式

objectFromJson("{'status':200," +
                "'message':'ok'," +
                "'people':{" +
                "'username':'LiLy'," +
                "'phone':'13189720413'," +
                "'headerImg':'https://img0.baidu.com/it/u=1521420901,2700109129&fm=253&fmt=auto&app=138&f=JPEG?w=500&h=313'}}");
    private void objectFromJson(String s) {

        Gson gson=new Gson();
        Me me = gson.fromJson(s, Me.class);
        Log.d("","me======="+me.toString());

    }

GSON的数据解析看起来代码量相对小一点,但所创建的类也是必须的

第一个支持的类,此类为自定义名字,为了方便随意起的,但在项目中要正规取名,见名知意

 class Me{
        int status;
        String message;
        People people;
public Me(){}

        public Me(int status, String message, People people) {
            this.status = status;
            this.message = message;
            this.people = people;
        }

        public int getStatus() {
            return status;
        }

        public void setStatus(int status) {
            this.status = status;
        }

        public String getMessage() {
            return message;
        }

        public void setMessage(String message) {
            this.message = message;
        }

        public People getPeople() {
            return people;
        }

        public void setPeople(People people) {
            this.people = people;
        }

        @Override
        public String toString() {
            return "Me{" +
                    "status=" + status +
                    ", message='" + message + '\'' +
                    ", people=" + people +
                    '}';
        }
    }

第二个支持的类

class People {
    private String username;
    private String phone;
    private String headerImg;

    public People(String username, String phone, String headerImg) {
        this.username = username;
        this.phone = phone;
        this.headerImg = headerImg;
    }

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public String getPhone() {
        return phone;
    }

    public void setPhone(String phone) {
        this.phone = phone;
    }

    public String getHeaderImg() {
        return headerImg;
    }

    public void setHeaderImg(String headerImg) {
        this.headerImg = headerImg;
    }

    @Override
    public String toString() {
        return "People{" +
                "username='" + username + '\'' +
                ", phone='" + phone + '\'' +
                ", headerImg='" + headerImg + '\'' +
                '}';
    }

今天的文章Android 解析JSON数据分享到此就结束了,感谢您的阅读,如果确实帮到您,您可以动动手指转发给其他人。

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。
如需转载请保留出处:https://bianchenghao.cn/32368.html

(0)
编程小号编程小号

相关推荐

发表回复

您的电子邮箱地址不会被公开。 必填项已用 * 标注