Pull information from Json array

╄→尐↘猪︶ㄣ 提交于 2019-12-12 04:38:35

问题


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,


回答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);



回答2:


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



回答3:


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



来源:https://stackoverflow.com/questions/46256585/pull-information-from-json-array

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!