如何在android studio中通过volley库解析Json中的复杂数据?

svmlkihl  于 2023-04-12  发布在  Android
关注(0)|答案(1)|浏览(115)

我想通过volley库从API中获取数据。
我正在通过Android中的Volley类检索JSON Object和JSON Arrray。我知道如何获取请求,但我真的很难解析这个巨大的嵌套JSON。
我想从列表中获取synonyms和反义词
我想从同义词数组反义词数组****数组中获取数据.......这里是API . Link的链接
或Json API图片-enter image description here
我的主要活动

JsonArrayRequest jsonArrayRequest = new JsonArrayRequest(Request.Method.GET, JSON_URL, null, new Response.Listener<JSONArray>() {
        @Override
        public void onResponse(JSONArray response) {
            for (int i = 0; i < response.length(); i++) {
                // creating a new json object and
                // getting each object from our json array.
                try {
                    // we are getting each json object.
                    JSONObject responseObj = response.getJSONObject(i);

                    String word = responseObj.getString("word");
                    String phonetic = responseObj.getString("phonetic");

                    JSONArray jsonArray = responseObj.getJSONArray("meaning");
                    for (int k = 0; k < response.length(); k++) {

                      ?? what to do here ????????

------------------------need help here-------------------------------

                    }

                    synonymsList.add(new synonymsModel(word,phonetic, synonyms,antonyms));
                    buildRecyclerView();
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }
        }
    }, new Response.ErrorListener() {
        @Override
        public void onErrorResponse(VolleyError error) {
            Toast.makeText(getApplicationContext(), error.getMessage(), Toast.LENGTH_LONG).show();
        }
    });
    queue.add(jsonArrayRequest);
}

型号等级

public class synonymsModel {

  String word, phonetic , antonyms, synonyms;

    public synonymsModel(String word, String phonetic, String antonyms, String synonyms) {
        this.word = word;
        this.phonetic = phonetic;
        this.antonyms = antonyms;
        this.synonyms = synonyms;
    }

    public String getWord() {
        return word;
    }

    public void setWord(String word) {
        this.word = word;
    }

    public String getPhonetic() {
        return phonetic;
    }

    public void setPhonetic(String phonetic) {
        this.phonetic = phonetic;
    }

    public String getAntonyms() {
        return antonyms;
    }

    public void setAntonyms(String antonyms) {
        this.antonyms = antonyms;
    }

    public String getSynonyms() {
        return synonyms;
    }

    public void setSynonyms(String synonyms) {
        this.synonyms = synonyms;
    }
}
wpx232ag

wpx232ag1#

基本上你想把你的json转换成你的java对象模型。

首先,你需要一个Map库,这是目前为止最好的Gson
其次,你需要建立一个你的需求模型。

我个人使用这个java class
在所有这2设置所有你要做的转换

Gson gson = new Gson();
 String json = gson.toJson(anything); // serializes target to Json
 YourClass your class = gson.fromJson(json, YourClass.class); //

你也不必为每一个调用loop for i,而你可以Map你的while对象本身。

相关问题