如何在 JSON 解析中添加多个 JSON 对象?我收到一个错误

How do I add multiple JSONObjects in JSON Parsing? I'm getting an error

我想通过从 https://jsonplaceholder.typicode.com/users 获取数据来设置用户列表及其相应的详细信息。但是我不能添加多个对象。

我能够显示 6 个详细信息,例如 ID、姓名、用户名、电子邮件、phone 和网站。但是不知何故,当我在 JsonArrayRequest 下将 "address" 作为新的 JSONObject 集成时,我的模拟器出现了一个空白屏幕。

这是我所做的:


     JsonArrayRequest jar =new JsonArrayRequest(Request.Method.GET, url, null, new Response.Listener<JSONArray>() {
            @Override
            public void onResponse(JSONArray response) {

                for(int i=0; i<response.length(); i++) {

                    try {
                        JSONObject JSobj1 = response.getJSONObject(i); //First object
                        String id = JSobj1.getString("id");
                        String name = JSobj1.getString("name");
                        String username = JSobj1.getString("username");
                        String email = JSobj1.getString("email");
                        String phone =JSobj1.getString("phone");
                        String website =JSobj1.getString("website");

                        JSONObject JSobj2 = JSobj1.getJSONObject("address"); //Second object
                        String street = JSobj2.getString("street");
                        String suite = JSobj2.getString("suite");
                        String city = JSObj2.getString("city");
                        String zipcode = JSObj2.getString("zipcode");

                        Setget sg =new Setget();
                        sg.setId(id);
                        sg.setName(name);
                        sg.setUsername(username);
                        sg.setEmail(email);
                        sg.setPhone(phone);
                        sg.setWebsite(website);
                        sg.setStreet(street);
                        sg.setSuite(suite);
                        sg.setCity(city);
                        sg.setZipcode(zipcode);

                        arrayList.add(sg);

                        if(arrayList.size()>0){
                            listView.setAdapter(new Myadapter()); //Custom Adapter
                        }


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

                }

            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {

                Toast.makeText(MainActivity.this,"Server Error",Toast.LENGTH_SHORT).show();
            }
        });
        Volley.newRequestQueue(MainActivity.this).add(jar);
    }

我期待 https://jsonplaceholder.typicode.com/users 的完整列表作为输出,但我的模拟器上出现空白屏幕。请帮忙。

谢谢

嘿,你需要在下面添加 classes

public class Address {

    @SerializedName("street")
    @Expose
    private String street;
    @SerializedName("suite")
    @Expose
    private String suite;
    @SerializedName("city")
    @Expose
    private String city;
    @SerializedName("zipcode")
    @Expose
    private String zipcode;
    @SerializedName("geo")
    @Expose
    private Geo geo;

    public String getStreet() {
        return street;
    }

    public void setStreet(String street) {
        this.street = street;
    }

    public String getSuite() {
        return suite;
    }

    public void setSuite(String suite) {
        this.suite = suite;
    }

    public String getCity() {
        return city;
    }

    public void setCity(String city) {
        this.city = city;
    }

    public String getZipcode() {
        return zipcode;
    }

    public void setZipcode(String zipcode) {
        this.zipcode = zipcode;
    }

    public Geo getGeo() {
        return geo;
    }

    public void setGeo(Geo geo) {
        this.geo = geo;
    }

}

公司Class

public class Company {

    @SerializedName("name")
    @Expose
    private String name;
    @SerializedName("catchPhrase")
    @Expose
    private String catchPhrase;
    @SerializedName("bs")
    @Expose
    private String bs;

    public String getName() {
        return name;
    }

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

    public String getCatchPhrase() {
        return catchPhrase;
    }

    public void setCatchPhrase(String catchPhrase) {
        this.catchPhrase = catchPhrase;
    }

    public String getBs() {
        return bs;
    }

    public void setBs(String bs) {
        this.bs = bs;
    }

}

地理位置Class

public class Geo {

    @SerializedName("lat")
    @Expose
    private String lat;
    @SerializedName("lng")
    @Expose
    private String lng;

    public String getLat() {
        return lat;
    }

    public void setLat(String lat) {
        this.lat = lat;
    }

    public String getLng() {
        return lng;
    }

    public void setLng(String lng) {
        this.lng = lng;
    }

}

测试Class

public class TestModel {

    @SerializedName("id")
    @Expose
    private String id;
    @SerializedName("name")
    @Expose
    private String name;
    @SerializedName("username")
    @Expose
    private String username;
    @SerializedName("email")
    @Expose
    private String email;
    @SerializedName("address")
    @Expose
    private Address address;
    @SerializedName("phone")
    @Expose
    private String phone;
    @SerializedName("website")
    @Expose
    private String website;
    @SerializedName("company")
    @Expose
    private Company company;

    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 getUsername() {
        return username;
    }

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

    public String getEmail() {
        return email;
    }

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

    public Address getAddress() {
        return address;
    }

    public void setAddress(Address address) {
        this.address = address;
    }

    public String getPhone() {
        return phone;
    }

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

    public String getWebsite() {
        return website;
    }

    public void setWebsite(String website) {
        this.website = website;
    }

    public Company getCompany() {
        return company;
    }

    public void setCompany(Company company) {
        this.company = company;
    }

}

用您的代码替换下面的代码

@注意:将 mJsonArray 替换为您的响应 class

ArrayList<TestModel> listdata = new ArrayList<TestModel>();
        JSONArray mJsonArray = new JSONArray();

        //Replace mJsonArray to response
        for(int i=0; i<mJsonArray.length(); i++) {

            try {
                JSONObject json = mJsonArray.getJSONObject(i); //First object
                String id = json.getString("id");
                String name = json.getString("name");
                String username = json.getString("username");
                String email = json.getString("email");
                String phone =json.getString("phone");
                String website =json.getString("website");



                JSONObject json2 = json.getJSONObject("address"); //Second object
                String street = json2.getString("street");
                String suite = json2.getString("suite");
                String city = json2.getString("city");
                String zipcode = json2.getString("zipcode");

                JSONObject json3 = json2.getJSONObject("geo");
                String lat = json3.getString("lat");
                String lang = json3.getString("lng");

                JSONObject json4 = json.getJSONObject("company");
                String companyName = json4.getString("name");
                String catchPhrase = json4.getString("catchPhrase");
                String bs = json4.getString("bs");

                TestModel model = new TestModel();
                model.setId(id);
                model.setName(name);
                model.setUsername(username);
                model.setEmail(email);
                model.setPhone(phone);
                model.setWebsite(website);

                Address mAddress = new Address();
                mAddress.setStreet(street);
                mAddress.setSuite(suite);
                mAddress.setCity(city);
                mAddress.setZipcode(zipcode);

                Geo mGeo = new Geo();
                mGeo.setLat(lat);
                mGeo.setLng(lang);

                mAddress.setGeo(mGeo);

                model.setAddress(mAddress);

                Company company = new Company();
                company.setBs(bs);
                company.setCatchPhrase(catchPhrase);
                company.setName(companyName);

                model.setCompany(company);


                listdata.add(model);



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

        }

        if(listdata.size()>0){
            //Hope you need pass list to MyAdapter
            listView.setAdapter(new Myadapter()); //Custom Adapter
        }