如何使用GET方法检索数组中的数组?

问题描述 投票:0回答:1

我使用Rest Countries API来检索语言数据,https://restcountries.eu/rest/v2/all ..但语言不会显示,因为数据是在数组中。

这是我编写的代码,用于使用Method.Get检索数据

   private void getCountriesList() {

   String url = "https://restcountries.eu/rest/v2/all";

    StringRequest request = new StringRequest(
            Request.Method.GET,
            url,
            new Response.Listener<String>() {

                @Override
                public void onResponse(String response) {

                    if(!response.isEmpty()) {
                        Gson gson = new Gson();
                        JSONObject json = null;
                        // array of country
                        LanguageModel[] countries = gson.fromJson(response, LanguageModel[].class);
                        // add it to adapter
                        for(LanguageModel country: countries) {
                            mAdapter.addItem(country);
                        }


                    }
                }
            },
            new Response.ErrorListener() {

                @Override
                public void onErrorResponse(VolleyError error) {
                    Log.d(TAG, error.getLocalizedMessage());
                }
            }
    );

    Volley.newRequestQueue(getApplicationContext()).add(request);
}

这是模型。当我检索国家名称时代码成功,但是当我检索语言数据时它失败了

public class CountryModel {

private String languages;

public String getLanguages() {
    return languages;
}

public void setLanguages(String languages) {
    this.languages = languages;
}}
android gson android-volley
1个回答
0
投票

我使用以下代码:

if (!response.isEmpty()) {
                            Gson gson = new Gson();

                            try {
                                JSONArray mainArray = new JSONArray(response);
                                for (int i = 0; i < mainArray.length(); i++) {
                                    JSONObject countryObj = mainArray.getJSONObject(i);
                                    JSONArray languageArray = countryObj.getJSONArray("languages");
                                    List<LanguageModel> languageModels = gson.fromJson(languageArray.toString(), new TypeToken<List<LanguageModel>>() {
                                    }.getType());
                                    // add it to adapter
                                    for (LanguageModel languageModel : languageModels) {
                                        //mAdapter.addItem(country);
                                    }
                                }
                            } catch (JSONException e) {
                                e.printStackTrace();
                            }
                            // array of country

                        }

如果您仍然收到错误,请告诉我。

编辑:

但如何同时获得国家名称和语言?

你必须编辑你的CountryModel如下:

public class CountryModel {

    @SerializedName("name")
    String countryName;
    @SerializedName("languages")
    ArrayList<LanguageModel> languages;

    public CountryModel() {
    }
}

LanguageModel如下:

class LanguageModel {


    @SerializedName("iso639_1")
    String iso1;
    @SerializedName("iso639_2")
    String iso2;
    @SerializedName("name")
    String name;
    @SerializedName("nativeName")
    String nativeName;

    public LanguageModel() {
    }
}

现在解析的最后一个变化:

if (!response.isEmpty()) {
                            Gson gson = new Gson();

                            try {
                                JSONArray mainArray = new JSONArray(response);
                                List<CountryModel> countryList = gson.fromJson(mainArray.toString(), new TypeToken<List<CountryModel>>() {
                                }.getType());

                                //do something with your country list
                                Log.d("R-Countries", countryList.toString());

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

虽然这个答案有效,但你应该看到以下链接,以了解我在这里做了什么。

http://www.studytrails.com/java/json/java-google-json-introduction/

© www.soinside.com 2019 - 2024. All rights reserved.