Java解析JSON数组匹配角色名获取邮箱的问题求助
Got it, let's break down what's going wrong here and fix it quickly!
What's Causing the Error
That JsonSyntaxException is happening for two key reasons:
- Your API returns a JSON array (starts with
[), but you tried parsing the entire response into a singleRolesobject—Gson expects a single object (starts with{) here, hence the "Expected BEGIN_OBJECT but was BEGIN_ARRAY" error. - You already correctly parsed the response into a
List<Users>! TheRolesclass is just a nested property inside eachUsersobject, so there's no need to parse the entire response separately forRoles.
Step 1: Fix Your Entity Classes
First, make sure your Users and Roles classes match the exact structure of the JSON (pay attention to field spellings like middieInitial—don't misspell this!):
public class Users { private String id; private String eauthId; private String firstName; private String middieInitial; // Match the JSON field name exactly private String lastName; private String email; private List<Roles> roles; // Generate getters (and setters if needed) public String getEmail() { return email; } public List<Roles> getRoles() { return roles; } } public class Roles { private String id; private String name; private String treatmentName; // Generate getter for the name field we need to match public String getName() { return name; } }
Step 2: Fix the Parsing & Matching Logic
Delete that broken Roles roles = new Gson().fromJson(...) line, then add logic to iterate through the parsed users and match the role name you're targeting:
// Your existing valid code to fetch and parse the user list CloseableHttpResponse closeableHttpResponse = restClient.get(ConfigurationReader.get("base_url") + ConfigurationReader.get("user_endpoint")); int statusCode = closeableHttpResponse.getStatusLine().getStatusCode(); System.out.println("statusCode = " + statusCode); String responseString = EntityUtils.toString(closeableHttpResponse.getEntity(), "UTF-8"); // Correctly parse the full response into a list of Users Type userListType = new TypeToken<List<Users>>(){}.getType(); List<Users> users = new Gson().fromJson(responseString, userListType); // Define the role name you want to match (adjust this as needed) String targetRoleName = "A test role for CTIS"; String matchedEmail = null; // Loop through users and their roles to find a match for (Users user : users) { for (Roles role : user.getRoles()) { if (targetRoleName.equals(role.getName())) { matchedEmail = user.getEmail(); break; // Exit inner loop once we find a match } } if (matchedEmail != null) { break; // Exit outer loop once we find the user } } // Output the result if (matchedEmail != null) { System.out.println("Found matching email for role '" + targetRoleName + "': " + matchedEmail); } else { System.out.println("No user found with the role name: " + targetRoleName); }
Why This Works
- We only parse the response once, into the correct type (
List<Users>) since the API returns an array of users. - Each
Usersobject already contains its own list ofRoles, so we just iterate through that nested list to check for the role name we need. - We removed the unnecessary (and error-causing) attempt to parse the entire array into a single
Rolesobject.
内容的提问来源于stack exchange,提问作者Sherinay Abdukerim
相关产品推荐
相关产品推荐

