-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)
3
  • Go over the JSON Array and check for the property userId and see if it equals 3. Commented Mar 11, 2019 at 11:36
  • how should i do, in java? Commented Mar 11, 2019 at 11:43
  • No it's json already. The case, I just want to get {"userId" : 3, "id" : 3, "title" : "omnis laborum odio"} Commented Mar 11, 2019 at 11:47

2 Answers 2

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));
Sign up to request clarification or add additional context in comments.

Comments

-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"
      }]
}

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.