如何填充多个 JSONArrays

How to populate multiple JSONArrays

我收到来自 JSON 的回复,我收到了两个以上的 jSONArray。像这样我拍摄了 logcat 数据 here is logcat data which I am fetching jsonarrays from my response

的快照

现在的问题是如何在单个数组列表中填充所有这些数组列表以及如何在 recyclerView 中显示。这是我的回复代码

JSONObject jsonObject = null;
                    try {
                        jsonObject = new JSONObject(response);
                        JSONArray hotDealProduct =  jsonObject.getJSONArray("ListProduct");
                        setHotDealAdapter(hotDealProduct);
                        Log.e(TAG,hotDealProduct.toString());
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }

这是我的 setHotDealAdapter() 代码:

for (int i=0;i<hotDealProduct.length();i++) {
        try {
            JSONObject object = hotDealProduct.getJSONObject(i);
            final String discount = object.getString("discount");
            final String price = object.getString("Price");
            final String salePrice = object.getString("SalePrice");
            final String image = object.getString("MainImage");
            final String name = object.getString("Name");
            products.add(new HotDeal(1,discount, price ,image,salePrice,name));
            adapter = new HotDealAdapter(context,products);
            rvHotDeal.setLayoutManager(new GridLayoutManager(context,2));
            rvHotDeal.setAdapter(adapter);
            adapter.notifyDataSetChanged();
        } catch (JSONException e) {
            e.printStackTrace();
        }

像这样改变你的setHotDealAdapter()

for (int i=0;i<hotDealProduct.length();i++) {
            try {
                JSONObject object = hotDealProduct.getJSONObject(i);
                final String discount = object.getString("discount");
                final String price = object.getString("Price");
                final String salePrice = object.getString("SalePrice");
                final String image = object.getString("MainImage");
                final String name = object.getString("Name");
                products.add(new HotDeal(1,discount, price ,image,salePrice,name));

            } catch (JSONException e) {
                e.printStackTrace();
            }
                adapter = new HotDealAdapter(context,products);
                rvHotDeal.setLayoutManager(new GridLayoutManager(context,2));
                rvHotDeal.setAdapter(adapter);
                adapter.notifyDataSetChanged();

我看到你做对了,但你需要在 for 循环之外初始化和设置适配器,并且在每次创建新适配器并将新适配器分配给回收器时。

请试试这个代码

for (int i=0;i<hotDealProduct.length();i++) {
                try {
                    JSONObject object = hotDealProduct.getJSONObject(i);
                    final String discount = object.getString("discount");
                    final String price = object.getString("Price");
                    final String salePrice = object.getString("SalePrice");
                    final String image = object.getString("MainImage");
                    final String name = object.getString("Name");
                    products.add(new HotDeal(1,discount, price ,image,salePrice,name));

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

    adapter = new HotDealAdapter(context,products);
                    rvHotDeal.setLayoutManager(new GridLayoutManager(context,2));
                    rvHotDeal.setAdapter(adapter);

这很简单,您只需在循环外设置适配器,

我的意思是以下代码:

adapter = new HotDealAdapter(context,products);
            rvHotDeal.setLayoutManager(new GridLayoutManager(context,2));
            rvHotDeal.setAdapter(adapter);

你必须立即调用它。由于您完全更改了适配器,因此不需要以下行:

adapter.notifyDataSetChanged();

如果要一个一个添加item,先设置adapter,然后获取adapter,添加Item,然后调用adapter.notifyDataSetChanged(); (这个解决方案很耗时,不是首选)。

正确代码:

try {
  for (int i=0;i<hotDealProduct.length();i++) {               
      JSONObject object = hotDealProduct.getJSONObject(i);
      String discount = object.getString("discount");
      String price = object.getString("Price");
      String salePrice = object.getString("SalePrice");
      String image = object.getString("MainImage");
      String name = object.getString("Name");
      products.add(new HotDeal(1,discount, price ,image,salePrice,name));
    }

    adapter = new HotDealAdapter(context,products);
    rvHotDeal.setLayoutManager(new GridLayoutManager(context,2));
    rvHotDeal.setAdapter(adapter);
} catch (JSONException e) {
  e.printStackTrace();
}

最佳实践稳健性能->

A. 为您的 json 响应创建 POJOModel

对于这一步 ->

1) 复制您的 json 回复并粘贴 here.

2) select 来源类型:JSON注释样式:Gson.

3) 根据您的选择点击生成或预览并创建您的模型 class。

B. 在你的 build.gradle(Module:app)

中导入 GSON 依赖项
implementation 'com.google.code.gson:gson:2.8.2'

C. 在你的 activity 或片段 class ->

实现代码如下:

ArrayList<YourPOJO> itemlist = new ArrayList<>();

{
// Your code implementation of binding data to recyclerview : 

GsonBuilder gsonbuilder= new GsonBuilder();

Gson gson=gsonbuilder.create();
//response = its your json string from server
itemList =gson.fromJson(response, new TypeToken<List<YourPOJO>>(){}.getType());

yourAdapter = new YourAdapter(itemList);

yourRecyclerview.setAdapter(yourAdapter);

}

通过这种方法,您不必手动编写代码来解析您的 json 并将其绑定到您的视图。

现在如果你想解析你的 json 的某些对象(在你的情况下你只想解析 JSONArray )在上面的代码中将 response 替换为 yourJsonArray.toString()

希望它能解决您的问题。