简体   繁体   中英

Retrieving single items from a volley jsonArray

I have the following which is part of my response from my API server using volley.

      "products": [
{
  "seller": "Geff Baraka Hardware",
  "sellerId": "57",
  "category": "Hardware",
  "id": "794",
  "code": null,
  "name": "Devki Barbed Wire 610m (16G X 25kg)",
  "desc": "<p style=\"margin-bottom: 1.3em; color: rgb(51, 51, 51); font-family: Lato, sans-serif; overflow-wrap: break-word;\">Shop for this Devki Barbed Wire 610m (16G X 25kg) on Kenya’s local business hosting platform. For All your Quality Assured Trusted Brands. Long-lasting barbed wire is constructed with sharp edges or points arranged at intervals along the strands.</p><p style=\"margin-bottom: 1.3em; color: rgb(51, 51, 51); font-family: Lato, sans-serif; overflow-wrap: break-word;\">Order for&nbsp;Devki Barbed Wire 610m (16G X 25kg) on Swiva hardware partners.&nbsp;</p><p style=\"margin-bottom: 1.3em; color: rgb(51, 51, 51); font-family: Lato, sans-serif; overflow-wrap: break-word;\">Details</p><p style=\"margin-bottom: 1.3em; color: rgb(51, 51, 51); font-family: Lato, sans-serif; overflow-wrap: break-word;\">&gt;<span style=\"font-size: 1rem;\">Stainless Steel Fencing Wire</span></p><p style=\"overflow-wrap: break-word; margin-bottom: 1.3em; color: rgb(51, 51, 51); font-family: Lato, sans-serif;\">&gt; Constructed With Sharp Edges Or Point Arranged At Intervals Along The Strands<br>&gt; Low Cost<br>&gt; Easy To Install<br>&gt; 610m<br>&gt; High Quality Fence</p><p style=\"overflow-wrap: break-word; margin-bottom: 1.3em; color: rgb(51, 51, 51); font-family: Lato, sans-serif;\">Package Contents</p><p style=\"overflow-wrap: break-word; margin-bottom: 1.3em; color: rgb(51, 51, 51); font-family: Lato, sans-serif;\">&gt; Devki Barbed Wire 610m (16G X 25kg)</p>",
  "cost": "5.00",
  "discounted_cost": "0.00",
  "quantity": "10",
  "sku": "Item",
  "owner_id": "57",
  "cat_id": "25",
  "sub_cat_id": "115",
  "color": "Silver",
  "size": "610M",
  "brand": "Devki",
  "path": "[\"swiva_product2211062201.jpg\",\"swiva_product960899477.jpg\"]",
  "avg_rate": "1",
  "top_rate": "1",
  "low_rate": "1",
  "status_id": "7",
  "created_at": "2022-07-16 19:14:36",
  "updated_at": "2022-07-19 07:11:58"
}]

I am interested in path which contains links to images, I would like to display only the first image as the icon but do not know how to go about it. I have tried looping through each of the jsonarray elements and retrieved path as a jsonArray from which I'm attempting to getString of index 0 as follows

      for(int i=0; i<productArray.length();i++){
          jsonObject = productArray.getJSONObject(i);
          id= jsonObject.getInt("id");
      .
      .
      .
         image = jsonObject.getJSONArray("path").getString(0);
     }

but that is now working

I have tried Log.d("TAG", "retrievedProductPhotos: "+ jsonObject.getString("path")); and that gives me ["swiva_product2211062201.jpg","swiva_product960899477.jpg"] . But on trying Log.d("TAG", "retrieveProductPhotos: "+ jsonObject.getJSONArray("path")); I get nothing Any idea on how to go about it?

It looks like your JSON array was saved as a string, so

"path": "[\"a\", \"b\"]"

instead of

"path": ["a", "b"]

(note the extra quotes). To parse that you would need to read it as a string first, then parse that as a JSONArray:

String pathStr = jsonObject.getString("path");
JSONArray pathArr = new JSONArray(pathStr);
String firstPath = pathArr.getString(0);

However, if you control the source of the JSON response, fixing it to properly encode the array would be better, then you could use the getJSONArray approach.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM