0

All,

I have following JSON response after request get list of users. I want to pull just only one user's id using userName. For example if i want id of userName Test1, how to do that? Any help will be appreciated.

      {
         "displayLength": "4",
         "iTotal": "20",
         "users": [
         {
            "id": "2",
            "userName": "Test1",
            "Group": {   id:1
                         name:"Test-Admin"
                         }
        },
        {
            "id": "17",
            "userName": "Test2",
            "Group": {   id:1
                         name:"Test-Admin"
                         }
        },
        {
            "id": "32",
            "userName": "Test3",
            "Group": {   id:1
                         name:"Test-Admin"
                         }
        },
        {
            "id": "35",
            "userName": "Test4",
            "Group": {   id:1
                         name:"Test-Admin"
                         }
        }  

    ]
   }

Thanks,

3 Answers3

1

See if this below code helps. Just pass user name to userName variable and let the code find the userId for you.

JSONObject json = new JSONObject(" {\n" + "         \"displayLength\": \"4\",\n"
        + "         \"iTotal\": \"20\",\n" + "         \"users\": [\n" + "         {\n"
        + "            \"id\": \"2\",\n" + "            \"userName\": \"Test1\",\n"
        + "            \"Group\": {   id:1,\n" + "                         name:\"Test-Admin\"\n"
        + "                         }\n" + "        },\n" + "        {\n" + "            \"id\": \"17\",\n"
        + "            \"userName\": \"Test2\",\n" + "            \"Group\": {   id:1,\n"
        + "                         name:\"Test-Admin\"\n" + "                         }\n" + "        },\n"
        + "        {\n" + "            \"id\": \"32\",\n" + "            \"userName\": \"Test3\",\n"
        + "            \"Group\": {   id:1,\n" + "                         name:\"Test-Admin\"\n"
        + "                         }\n" + "        },\n" + "        {\n" + "            \"id\": \"35\",\n"
        + "            \"userName\": \"Test4\",\n" + "            \"Group\": {   id:1,\n"
        + "                         name:\"Test-Admin\"\n" + "                         }\n" + "        }  \n"
        + "\n" + "    ]\n" + "   }");

JSONArray array = json.getJSONArray("users");

String userName = "Test1";
Integer userId = null;

for (int i = 0; i < array.length() && userId == null; i++) {

    JSONObject jsonIn = (JSONObject) array.get(i);

    if (jsonIn.optString("userName").equals(userName)) {
        userId = jsonIn.optInt("id");
    }
}

System.out.println("User ID for User Name '" + userName + "' is : " + userId);
Hatim Stovewala
  • 1,333
  • 10
  • 19
  • Thank you for your response. Also is there a way to parse those json string user list in tabular format? –  Sep 20 '17 at 22:00
  • JSON is an semi-structured datatype. It is not structured like result set from SQL. JSON may have nested objects or array which won't fit in a tabular format. You can still make tabular format only if the structure is strict that is predefined. – Hatim Stovewala Sep 21 '17 at 00:10
1

I recomend use http-request built on apache http api. You must create class ResponseData to parse response.

 private static final HttpRequest<ResponseData> HTTP_REQUEST = 
        HttpRequestBuilder.createGet(yourUri, ResponseData.class).build();

    public void test() {
    HTTP_REQUEST.execute().ifHasContent(responseData -> {
        Optional<User> foundedUser = responseData.getUsers()
                .stream()
                .filter(user -> "Test1".equals(user.getUserName()))
                .findFirst();
         foundedUser.ifPresent(user -> System.out.println(user.getId()));
       });
    }

  class ResponseData {
    private int displayLength;
    private int iTotal;
    private List<User> users;

    public int getDisplayLength() {
        return displayLength;
    }

    public void setDisplayLength(int displayLength) {
        this.displayLength = displayLength;
    }

    public int getiTotal() {
        return iTotal;
    }

    public void setiTotal(int iTotal) {
        this.iTotal = iTotal;
    }

    public List<User> getUsers() {
        return users;
    }

    public void setUsers(List<User> users) {
        this.users = users;
    }
}

class User {
    private int id;
    private String userName;
    private Group Group;

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getUserName() {
        return userName;
    }

    public void setUserName(String userName) {
        this.userName = userName;
    }

    public Group getGroup() {
        return Group;
    }

    public void setGroup(Group group) {
        Group = group;
    }
}

class Group {
    private int id;
    private String name;

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }
}

Note: Your json is incorrect. See Corrected:

{
  "displayLength": "4",
  "iTotal": "20",
  "users": [
    {
      "id": "2",
      "userName": "Test1",
      "Group": {
        "id": 1,
        "name": "Test-Admin"
      }
    },
    {
      "id": "17",
      "userName": "Test2",
      "Group": {
        "id": 1,
        "name": "Test-Admin"
      }
    },
    {
      "id": "32",
      "userName": "Test3",
      "Group": {
        "id": 1,
        "name": "Test-Admin"
      }
    },
    {
      "id": "35",
      "userName": "Test4",
      "Group": {
        "id": 1,
        "name": "Test-Admin"
      }
    }
  ]
}
Beno
  • 945
  • 11
  • 22
0

jsonObj.getJsonArray("users") and then convert the array to list. Now use Java 8 Stream and Filter api's to extract the desired output.

Vaisakh PS
  • 1,181
  • 2
  • 10
  • 19