反序列化复杂的json主体,验证其中的一个元素,然后通过它,映射到另一个元素(JAVA)是可能的吗?
我需要处理一个json数组,如下所示:反序列化复杂的json主体,验证其中的一个元素,然后通过它,映射到另一个元素(JAVA)是可能的吗?,java,json,json-deserialization,Java,Json,Json Deserialization,我需要处理一个json数组,如下所示: [ { "id": "12345", "eauthId": "123451234512345123451234512345", "firstName": "Jane", "middieInitial": "M",
[
{
"id": "12345",
"eauthId": "123451234512345123451234512345",
"firstName": "Jane",
"middieInitial": "M",
"lastName": "Doe",
"email": "janedoe@usda.gov",
"roles": [
{
"id": "CTIS_ROLE_ID",
"name": "A test role for CTIS",
"treatmentName": "Fumigation"
}
]
},
{
"id": "67890",
"eauthId": "678906789067890678906789067890",
"firstName": "John",
"middieInitial": "Q",
"lastName": "Admin",
"email": "johnadmin@usda.gov",
"roles": [
{
"id": "CTIS_ADMIN",
"name": "An admin role for CTIS",
"treatmentName": "System Administration"
}
]
}
]
我的任务是找出用户的“角色”->“名称”,一旦匹配,获取该用户的电子邮件地址并使用该电子邮件地址登录。这似乎是一项简单的任务,但它真的让我扫兴不已,因为深入API对我来说是一项新任务。我尝试了不同的库(Jackson、restasured、jsonsimple),最后是GSon。我没有时间坐下来从头开始研究一切。我只是需要一个快速的解决方案。但这肯定不是很快。有人能帮我解决这个问题吗。我真的很感激
closeableHttpResponse = restClient.get(ConfigurationReader.get("base_url") + ConfigurationReader.get("user_endpoint"));
//Status code
int statusCode = closeableHttpResponse.getStatusLine().getStatusCode();
System.out.println("statusCode = " + statusCode);
String responseString = EntityUtils.toString(closeableHttpResponse.getEntity(), "UTF-8");
Type userListType = new TypeToken<List<Users>>(){}.getType();
List<Users> users = (List<Users>) new Gson().fromJson(responseString, userListType);
Roles roles = new Gson().fromJson(responseString, Roles.class);
代码的问题是
Type userListType = new TypeToken<List>(){}.getType();
List users = (List) new Gson().fromJson(responseString, userListType);
Type userListType = new TypeToken<List>(){}.getType();
List users = (List) new Gson().fromJson(responseString, userListType);
List[] users = (List[]) new Gson().fromJson(responseString, List[].class);