Android Java解析嵌套JSON数组 获取addon items绑定RecyclerView
1. JSON格式排查规则
- 首先确认
addon items键名是否完全匹配,若接口返回的键名实际为驼峰/下划线形式(如addonItems、addon_items)会直接导致解析匹配失败 - 检查嵌套结构完整性:外层JSON数组的每个子对象是否完整闭合
{},addon items对应的子数组是否完整闭合[],所有字符串是否用双引号包裹,无多余逗号、缺失引号等语法问题 - Android端可直接通过
new JSONArray(responseString)捕获JSONException快速判断是否存在格式错误
2. PHP接口脚本常见问题排查
- 检查返回头是否正确设置:必须输出
header('Content-Type: application/json; charset=utf-8');,避免返回值携带HTML、空格、PHP报错信息等多余内容 - 确认
addon items键名输出正确,PHP侧正确构造数据的示例代码:
// 构造返回数据示例 $returnData = []; foreach ($parentList as $item) { $temp = [ 'id' => $item['id'], 'name' => $item['name'], // 带空格的键名必须加引号 'addon items' => $item['addonList'] ]; $returnData[] = $temp; } echo json_encode($returnData, JSON_UNESCAPED_UNICODE); exit;
- 避免
json_encode返回空:确认PHP数组中无UTF-8编码异常的字符,必要时可加JSON_PARTIAL_OUTPUT_ON_ERROR参数过滤异常内容
3. Android端Java解析实现方案
3.1 实体类定义
// 外层父项实体 public class ParentItem { private String id; private String name; // 适配带空格的JSON键名,Gson解析需加该注解 @SerializedName("addon items") private List<AddonItem> addonItems; // 省略getter、setter方法 } // addon子项实体 public class AddonItem { private String addonId; private String addonName; private double addonPrice; // 省略其他自定义字段、getter、setter方法 }
3.2 嵌套JSON解析逻辑
// 原生org.json解析示例 try { JSONArray parentJsonArray = new JSONArray(responseString); List<ParentItem> parentItemList = new ArrayList<>(); for (int i = 0; i < parentJsonArray.length(); i++) { JSONObject parentObj = parentJsonArray.getJSONObject(i); ParentItem parentItem = new ParentItem(); parentItem.setId(parentObj.optString("id")); parentItem.setName(parentObj.optString("name")); // 提取addon items子数组 JSONArray addonJsonArray = parentObj.optJSONArray("addon items"); List<AddonItem> addonItemList = new ArrayList<>(); if (addonJsonArray != null) { for (int j = 0; j < addonJsonArray.length(); j++) { JSONObject addonObj = addonJsonArray.getJSONObject(j); AddonItem addonItem = new AddonItem(); addonItem.setAddonId(addonObj.optString("addonId")); addonItem.setAddonName(addonObj.optString("addonName")); addonItem.setAddonPrice(addonObj.optDouble("addonPrice")); addonItemList.add(addonItem); } } parentItem.setAddonItems(addonItemList); parentItemList.add(parentItem); } // 绑定外层RecyclerView parentRvAdapter.setData(parentItemList); } catch (JSONException e) { e.printStackTrace(); // JSON格式错误处理逻辑 }
3.3 子RecyclerView绑定逻辑
在外层RecyclerView的Adapter的onBindViewHolder中绑定子RecyclerView:
@Override public void onBindViewHolder(@NonNull ParentAdapter.ViewHolder holder, int position) { ParentItem currentItem = parentItemList.get(position); holder.tvParentName.setText(currentItem.getName()); // 初始化子RecyclerView AddonAdapter addonAdapter = new AddonAdapter(context, currentItem.getAddonItems()); holder.rvAddon.setLayoutManager(new LinearLayoutManager(context)); holder.rvAddon.setAdapter(addonAdapter); }
4. 常见问题修复
- 解析返回空:确认
addon items键名完全匹配,包括空格、大小写,JSON中键名带空格必须完全一致才能解析到 - 子RecyclerView不显示:检查子RecyclerView的布局高度不要设置为
match_parent,改为wrap_content,或添加配置holder.rvAddon.setNestedScrollingEnabled(false); - 数据错乱:在外层Adapter的
onBindViewHolder中每次都重新设置子Adapter的数据,不要复用全局子Adapter实例
内容的提问来源于stack exchange,提问作者Ali Habib Haidar
相关产品推荐
相关产品推荐

