-2

I have a response json like this,

[{
  "userId": 1,
  "id": 1,
  "title": "quidem molestiae enim"
},
{
  "userId": 2,
  "id": 2,
  "title": "sunt qui excepturi placeat culpa"
},
{
  "userId": 3,
  "id": 3,
  "title": "omnis laborum odio"
}]

I want to show detail from userId = 3, I wasn't provided an endpoint to get detail. So I want to get detail from this endpoint.

in App:

  • I've shown the data for dashboard (using recyclerview/list)
  • After that, I want to go to detail from one data that I selected.
  • Case: I'm using 1 endpoint (like above)
yoppie97
  • 177
  • 4
  • 18

2 Answers2

0
        JSONArray jsonArray = (JSONArray) JSON.parse(str);
        Map map = jsonArray.stream().map( obj -> {JSONObject jsonobject = (JSONObject) obj;return jsonobject;}).collect(Collectors.toMap(obj->obj.getIntValue("userId"),obj->obj.toJSONString()));
        System.out.println(map.get(3));
song bo
  • 16
  • 1
-2

the start point of json must always be a jsonObject not jsonArray. head to your backend developer and ask him or her to put your jsonArray inside a jsonObject.

{
    "docs":[{
        "userId": 1,
        "id": 1,
        "title": "quidem molestiae enim"
      },
      {
        "userId": 2,
        "id": 2,
        "title": "sunt qui excepturi placeat culpa"
      },
      {
        "userId": 3,
        "id": 3,
        "title": "omnis laborum odio"
      }]
}
SinaMN75
  • 6,742
  • 5
  • 28
  • 56