You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

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 single Roles object—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>! The Roles class is just a nested property inside each Users object, so there's no need to parse the entire response separately for Roles.

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 Users object already contains its own list of Roles, 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 Roles object.

内容的提问来源于stack exchange,提问作者Sherinay Abdukerim

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.08 15:37:48