如何在Android Studio中用Volley库解析复杂JSON获取同义词反义词
Android Volley解析嵌套JSON获取同义词与反义词
已通过Volley发起GET请求并成功获取JSON数据,但无法解析API返回的嵌套结构JSON,目标是提取词典API中的synonyms(同义词)和antonyms(反义词)数组数据,以下是现有代码及完善方案:
现有MainActivity代码
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++) { try { 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);
现有Model类代码
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; } }
解析逻辑完善方案
1. 修复核心问题与解析步骤
API返回的JSON结构中,单词的释义嵌套在meanings数组(原代码误写为meaning)中,每个释义下的definitions数组包含同义词和反义词数组。需逐层遍历嵌套结构,收集所有数据:
修改MainActivity中的解析部分:
@Override public void onResponse(JSONArray response) { for (int i = 0; i < response.length(); i++) { try { JSONObject responseObj = response.getJSONObject(i); String word = responseObj.getString("word"); String phonetic = responseObj.getString("phonetic"); // 初始化集合存储所有同义词、反义词(支持去重) List<String> allSynonyms = new ArrayList<>(); List<String> allAntonyms = new ArrayList<>(); // 遍历meanings数组(注意字段名是meanings,不是meaning) JSONArray meaningsArray = responseObj.getJSONArray("meanings"); for (int k = 0; k < meaningsArray.length(); k++) { JSONObject meaningObj = meaningsArray.getJSONObject(k); // 遍历当前释义下的definitions数组 JSONArray definitionsArray = meaningObj.getJSONArray("definitions"); for (int j = 0; j < definitionsArray.length(); j++) { JSONObject definitionObj = definitionsArray.getJSONObject(j); // 处理同义词数组,避免字段不存在抛出异常 if (definitionObj.has("synonyms")) { JSONArray synonymsArray = definitionObj.getJSONArray("synonyms"); for (int m = 0; m < synonymsArray.length(); m++) { String synonym = synonymsArray.getString(m); if (!allSynonyms.contains(synonym)) { allSynonyms.add(synonym); } } } // 处理反义词数组 if (definitionObj.has("antonyms")) { JSONArray antonymsArray = definitionObj.getJSONArray("antonyms"); for (int n = 0; n < antonymsArray.length(); n++) { String antonym = antonymsArray.getString(n); if (!allAntonyms.contains(antonym)) { allAntonyms.add(antonym); } } } } } // 将集合转为逗号分隔字符串,适配原Model的String类型 String synonymsStr = TextUtils.join(", ", allSynonyms); String antonymsStr = TextUtils.join(", ", allAntonyms); synonymsList.add(new synonymsModel(word, phonetic, antonymsStr, synonymsStr)); buildRecyclerView(); } catch (JSONException e) { e.printStackTrace(); } } }
2. Model类优化建议
原Model将同义词、反义词定义为String类型,若后续需单独操作每个词汇,建议改为List<String>类型,更灵活:
import java.util.List; // 类名遵循Java规范:首字母大写 public class SynonymsModel { private String word; private String phonetic; private List<String> antonyms; private List<String> synonyms; public SynonymsModel(String word, String phonetic, List<String> antonyms, List<String> synonyms) { this.word = word; this.phonetic = phonetic; this.antonyms = antonyms; this.synonyms = synonyms; } // Getter和Setter方法 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 List<String> getAntonyms() { return antonyms; } public void setAntonyms(List<String> antonyms) { this.antonyms = antonyms; } public List<String> getSynonyms() { return synonyms; } public void setSynonyms(List<String> synonyms) { this.synonyms = synonyms; } }
若使用优化后的Model,添加数据时直接传入集合即可:
synonymsList.add(new SynonymsModel(word, phonetic, allAntonyms, allSynonyms));
内容的提问来源于stack exchange,提问作者Sahil Sharma
相关产品推荐
相关产品推荐

