如何使用给定链接中的Gson解析json数据

时间:2015-11-02 04:57:11

标签: android gson

链接: - http://api.androidhive.info/contacts/ 问题: - 从给定的链接我能够解析联系人属性值但无法解析电话属性值。 代码: -

class GetGSONdata extends AsyncTask<String, Integer, String> {

        @Override
        protected String doInBackground(String... params) {
            try {
                URL url = new URL("http://api.androidhive.info/contacts/");
                HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
                int responsecode = urlConnection.getResponseCode();
                Log.e("responsecode", responsecode + "");
                InputStreamReader inputStreamReader = new InputStreamReader(urlConnection.getInputStream(), "UTF-8");
                Gson gson = new Gson();
                JsonReader reader = new JsonReader(inputStreamReader);
                reader.beginObject();
                while (reader.hasNext()) {
                    String aname = reader.nextName();
                    Log.e("Array name", aname);
                    if (aname.equalsIgnoreCase("contacts")) {
                        reader.beginArray();
                        while (reader.hasNext()) {
                            Contacts contacts = gson.fromJson(reader, Contacts.class);
                            list.add(contacts);
                        }
                    }
                }
                reader.endObject();
                reader.close();
            } catch (Exception e) {
                Log.e("Exception test", e.toString());
            }

for (int i = 0; i < list.size(); i++) {
                Log.e("json data", list.get(i).getId() + " " + list.get(i).getName());

            }
            return null;
        }
    }


    public class Contacts {
        private String id;
        private String name;
        private String email;

        public String getId() {
            return id;
        }

        public void setId(String id) {
            this.id = id;
        }

        public String getName() {
            return name;
        }

        public void setName(String name) {
            this.name = name;
        }

        public String getEmail() {
            return email;
        }

        public void setEmail(String email) {
            this.email = email;
        }

        class Phone{
            private String mobile;
            private String home;
            private String office;

            public String getMobile() {
                return mobile;
            }

            public void setMobile(String mobile) {
                this.mobile = mobile;
            }

            public String getHome() {
                return home;
            }

            public void setHome(String home) {
                this.home = home;
            }

            public String getOffice() {
                return office;
            }

            public void setOffice(String office) {
                this.office = office;
            }
        }

    }

谢谢。

3 个答案:

答案 0 :(得分:1)

Phone课程中有一个Contacts课程的实例。

public class Contacts{
   private Phone phone;  
   private String id;
   private String name;
   private String email;
   private String address;
   private String gender;

   // Getters & Setters

}  
public class Phone{
        private String mobile;
        private String home;
        private String office;

       // Getters & Setters
  }

这应该有效。

答案 1 :(得分:1)

        result  // result is string Json data which is get from sever(Async Task post Execute result)

            Gson gson = new Gson();

            Reader reader;

            try {
                reader = new InputStreamReader(new ByteArrayInputStream(
                        result.getBytes("UTF-8")));
                Type Collectiontype = new TypeToken<Contact>() {
                }.getType();
        Contact lstContact = gson.fromJson(reader, Collectiontype);
        //Contact is pojo class 

Use this link for create pojo file and then refer this code

答案 2 :(得分:0)

我建议您使用本网站创建您的POJO课程,以便将来不会遇到任何问题 http://www.jsonschema2pojo.org/