首页 > 解决方案 > 如何绕过错误并解析 json 字段值

问题描述

如何在 JSON 以下反序列化?

当我使用下面的代码 Response response=engine.getResponse(Method.GET,strURL,strBAUsername,strBAPassword,null,ContentType.JSON); userEdit=response.as(TestUserRoleInfoList.class); // TestUserRoleInfoList 是一个 POJO 类

@JsonIgnoreProperties(ignoreUnknown=true) 公共类 TestUserRoleInfoList {

@JsonProperty("userRoleInfoList")
List<UserRoleInfo> userRoleInfo=new ArrayList<UserRoleInfo>();

public List<UserRoleInfo> getUserRoleInfo() {
    return userRoleInfo;
}

public void setUserRoleInfo(List<UserRoleInfo> userRoleInfo) {
    this.userRoleInfo = userRoleInfo;
}

}

不知何故,它无法解析 Regions 内部类。但我最不担心。我需要 collectionRole 字段。

如何获取集合角色列表?

{
    "id": 5595,
    "userName": "abc",
    "firstName": "abc",
    "lastName": "ijk",
    "additionalName": "Ernest",
    "namePrefix": {
        "id": null,
        "prefix": null,
        "createdAt": null,
        "updatedAt": null
    },
    "nameSuffix": {
        "id": null,
        "suffix": null,
        "createdAt": null,
        "updatedAt": null
    },
    "emplId": "11111",
    "workDayId": "11111",
    "staffEmailAddress": null,
    "facultyEmailAddress": "abc.xyz@xxxx.edu",
    "userRoleInfoList": [
        {
            "userId": 5595,
            "collectionRole": "program-chair",
            "regions": [
                {
                    "id": 1,
                    "region": "Germany 1",
                    "regionalDirectorFirstName": "aaaa",
                    "regionalDirectorLastName": "bbbb",
                    "associateDirectorFirstName": null,
                    "associateDirectorLastName": null,
                    "division": {
                        "id": 2,
                        "name": "Europe",
                        "deletedFlag": false,
                        "createdAt": null,
                        "updatedAt": null
                    },
                    "deletedFlag": false,
                    "createdAt": null,
                    "updatedAt": null
                }
            ],
            "school": {
                "id": 3,
                "name": "Arts and Sciences",
                "dean": null,
                "createdAt": null,
                "updatedAt": null
            },
            "facultyPhoto": null,
            "primary": false
        },
        {
            "userId": 5595,
            "collectionRole": "faculty",
            "regions": [
                {
                    "id": 3,
                    "region": "Germany 1",
                    "regionalDirectorFirstName": "aaaa",
                    "regionalDirectorLastName": "bbbb",
                    "associateDirectorFirstName": null,
                    "associateDirectorLastName": null,
                    "division": {
                        "id": 2,
                        "name": "Europe",
                        "deletedFlag": false,
                        "createdAt": null,
                        "updatedAt": null
                    },
                    "deletedFlag": false,
                    "createdAt": null,
                    "updatedAt": null
                }
            ],
            "school": {
                "id": 3,
                "name": "Arts and Sciences",
                "dean": null,
                "createdAt": null,
                "updatedAt": null
            },
            "facultyPhoto": null,
            "primary": true
        }
    ]
}

标签: rest-assuredrest-assured-jsonpath

解决方案


您可以使用org.json.JSONObject

    Response response = when()
            .get("api_url")
            .then()
            .extract().response();

    JSONObject jsonObject = new JSONObject(response.getBody().asString());
    JSONArray jsonArray = jsonObject.getJSONArray("userRoleInfoList");
    List<String> stringList = new ArrayList<>();

    for (int i = 0; i < jsonArray.length(); i++) {
        stringList.add(jsonArray.getJSONObject(i).getString("collectionRole"));
    }
    System.out.println(stringList);

这将输出[program-chair, faculty]


推荐阅读