恢复响应值 JSON
Restore values of Response JSON
我在获取列表中的值和存储以检查这些值是否存在于另一个列表中时遇到了一些问题。
有人可以帮忙吗?
这是我的代码:(使用 RestAssured 获取响应)
public static ArrayList<String> JSONcounterparties(String date ) {
baseURI = uri;
ArrayList<String> accounts =
given()
.auth().basic(getJiraUser(), getJiraPass())
.param("date", date)
.param("limit", "1")
.param("count", "false")
.param("sort", "accountId")
.when()
.get("/counterparties.json")
.then()
.extract().path("accounts.account.identifiers.identifier.accountId");
return accounts;
}
到目前为止一切顺利,我得到了预期值。
我需要提取一些值。对于这样的每 3 个数组,我需要获取值并将它们放入列表中以与另一个列表进行检查。如果列表中存在值,则“确定”,否则“不确定”。
[[[MHI, AIGSALPPMM, AIGSALPPM, 0, AIGSALPPMM, MORLGB2LXXX, AIGSALPPMM]]]
我试过这样做:
public static void main(String[] args) throws IOException {
ArrayList<String> dataJSON = JSONcounterparties("2021-05-25");
ArrayList<String> getData = new ArrayList<String>();
for(int i = 0; i < dataJSON.size(); i++) {
String data = dataJSON.get(i);
getData.add(data);
System.out.println(getData);
}
但是,我在控制台中收到此异常:
java.lang.ClassCastException: class java.util.ArrayList cannot be cast to class java.lang.String (java.util.ArrayList and java.lang.String are in module java.base of loader 'bootstrap')
获取值后进行检查:
public static void compareAndCheck(List<String> inputs, List<String> outputs) {
int y = 0;
int x = 0;
try {
for (String input : inputs) {
for (String out : outputs) {
if (input.contains(out)) {
System.out.println("ok: " + input + " exist in CDW");
} else {
System.out.println("not Ok: " + input + " don't exist in CDW");
}
}
x = x + 1;
}
y = y + 1;
x = 0;
} catch (Error e) {
System.out.println(e);
}
}
JSON
"accounts": {
"account": [{
"organisationId": {
"#value": "MHI"
},
"accountName": "AVIVA INVESTORS AC ALP PM MAIN",
"identifiers": {
"accountId": "AIGSALPPMM",
"customerId": "AIGSALPPM",
"cedol": "0",
"blockAccountCode": "AIGSALPPMM",
"bic": "MORLGB2LXXX",
"identifier": [{
"accountId": "MHI",
"accountIdType": "REVNCNTR"
},
{
"accountId": "AIGSALPPMM",
"accountIdType": "ACCOUNTID"
},
{
"accountId": "AIGSALPPM",
"accountIdType": "MHICUSTID"
},
{
"accountId": "0",
"accountIdType": "CEDOL"
},
{
"accountId": "AIGSALPPMM",
"accountIdType": "BLOCKACCOUNT"
},
{
"accountId": "MORLGB2LXXX",
"accountIdType": "ACCOUNTBIC"
},
{
"accountId": "AIGSALPPMM",
"accountIdType": "GLOBEOP"
}
]
},
"isBlocAccount": "N",
"accountStatus": "COMPLETE",
"products": {
"productType": [{
"productLineName": "REPO",
"productTypeId": "PRODMHI4",
"productTypeName": "Fixed Income",
"limitQualifier": "GMRA 2000",
"repoWrittenAgreement": "Y",
"repoNetting": "Y",
"repoAgreementDate": "2012-04-04",
"cleared": "N",
"bilateral": "N",
"limitInstructions": {
"limitInstruction": [{
"limitAmount": "0",
"limitCurrency": "USD",
"limitType": "REPEEXLI",
"limitTypeName": "Repo-FI Peak Exposure limit"
},
{
"limitAmount": "0",
"limitCurrency": "USD",
"limitType": "RENCBOLI",
"limitTypeName": "Repo-FI Non Correlated Borrow Limit"
},
{
"limitAmount": "0",
"limitCurrency": "GBP",
"limitType": "REDOTHLI",
"limitTypeName": "Repo-FI Documentated Threshold Limit"
},
{
"limitAmount": "0",
"limitCurrency": "USD",
"limitType": "REPTENLI",
"limitTypeName": "Repo-FI Tenor Limit"
}
]
}
}]
},
"clientBroker": "C",
"traxFlag": "N",
"clientLevel": "SUBAC",
"accountCreationDate": "2014-07-10T00:00:00.000Z",
"accountOpeningDate": "2013-07-10T00:00:00.000Z"
}]
}
问题
因为此代码是 List<List<List<String>>>
,而不是 List<String>
[[[MHI, AIGSALPPMM, AIGSALPPM, 0, AIGSALPPMM, MORLGB2LXXX, AIGSALPPMM]]]
当您尝试从此列表中获取元素时
String data = dataJSON.get(i);
这将抛出 ClassCastException 异常,ArrayList 无法转换为 String。
解决方案
保存对 List<List<List<String>>>
的回复
List<List<List<String>>> accounts = ...extract().path("accounts.account.identifiers.identifier.accountId");
将List<List<List<String>>>
转换为List<String>
List<String> accountIds = accounts.stream()
.flatMap(l -> l.stream().flatMap(Collection::stream)).
collect(Collectors.toList());
我在获取列表中的值和存储以检查这些值是否存在于另一个列表中时遇到了一些问题。
有人可以帮忙吗?
这是我的代码:(使用 RestAssured 获取响应)
public static ArrayList<String> JSONcounterparties(String date ) {
baseURI = uri;
ArrayList<String> accounts =
given()
.auth().basic(getJiraUser(), getJiraPass())
.param("date", date)
.param("limit", "1")
.param("count", "false")
.param("sort", "accountId")
.when()
.get("/counterparties.json")
.then()
.extract().path("accounts.account.identifiers.identifier.accountId");
return accounts;
}
到目前为止一切顺利,我得到了预期值。 我需要提取一些值。对于这样的每 3 个数组,我需要获取值并将它们放入列表中以与另一个列表进行检查。如果列表中存在值,则“确定”,否则“不确定”。
[[[MHI, AIGSALPPMM, AIGSALPPM, 0, AIGSALPPMM, MORLGB2LXXX, AIGSALPPMM]]]
我试过这样做:
public static void main(String[] args) throws IOException {
ArrayList<String> dataJSON = JSONcounterparties("2021-05-25");
ArrayList<String> getData = new ArrayList<String>();
for(int i = 0; i < dataJSON.size(); i++) {
String data = dataJSON.get(i);
getData.add(data);
System.out.println(getData);
}
但是,我在控制台中收到此异常:
java.lang.ClassCastException: class java.util.ArrayList cannot be cast to class java.lang.String (java.util.ArrayList and java.lang.String are in module java.base of loader 'bootstrap')
获取值后进行检查:
public static void compareAndCheck(List<String> inputs, List<String> outputs) {
int y = 0;
int x = 0;
try {
for (String input : inputs) {
for (String out : outputs) {
if (input.contains(out)) {
System.out.println("ok: " + input + " exist in CDW");
} else {
System.out.println("not Ok: " + input + " don't exist in CDW");
}
}
x = x + 1;
}
y = y + 1;
x = 0;
} catch (Error e) {
System.out.println(e);
}
}
JSON
"accounts": {
"account": [{
"organisationId": {
"#value": "MHI"
},
"accountName": "AVIVA INVESTORS AC ALP PM MAIN",
"identifiers": {
"accountId": "AIGSALPPMM",
"customerId": "AIGSALPPM",
"cedol": "0",
"blockAccountCode": "AIGSALPPMM",
"bic": "MORLGB2LXXX",
"identifier": [{
"accountId": "MHI",
"accountIdType": "REVNCNTR"
},
{
"accountId": "AIGSALPPMM",
"accountIdType": "ACCOUNTID"
},
{
"accountId": "AIGSALPPM",
"accountIdType": "MHICUSTID"
},
{
"accountId": "0",
"accountIdType": "CEDOL"
},
{
"accountId": "AIGSALPPMM",
"accountIdType": "BLOCKACCOUNT"
},
{
"accountId": "MORLGB2LXXX",
"accountIdType": "ACCOUNTBIC"
},
{
"accountId": "AIGSALPPMM",
"accountIdType": "GLOBEOP"
}
]
},
"isBlocAccount": "N",
"accountStatus": "COMPLETE",
"products": {
"productType": [{
"productLineName": "REPO",
"productTypeId": "PRODMHI4",
"productTypeName": "Fixed Income",
"limitQualifier": "GMRA 2000",
"repoWrittenAgreement": "Y",
"repoNetting": "Y",
"repoAgreementDate": "2012-04-04",
"cleared": "N",
"bilateral": "N",
"limitInstructions": {
"limitInstruction": [{
"limitAmount": "0",
"limitCurrency": "USD",
"limitType": "REPEEXLI",
"limitTypeName": "Repo-FI Peak Exposure limit"
},
{
"limitAmount": "0",
"limitCurrency": "USD",
"limitType": "RENCBOLI",
"limitTypeName": "Repo-FI Non Correlated Borrow Limit"
},
{
"limitAmount": "0",
"limitCurrency": "GBP",
"limitType": "REDOTHLI",
"limitTypeName": "Repo-FI Documentated Threshold Limit"
},
{
"limitAmount": "0",
"limitCurrency": "USD",
"limitType": "REPTENLI",
"limitTypeName": "Repo-FI Tenor Limit"
}
]
}
}]
},
"clientBroker": "C",
"traxFlag": "N",
"clientLevel": "SUBAC",
"accountCreationDate": "2014-07-10T00:00:00.000Z",
"accountOpeningDate": "2013-07-10T00:00:00.000Z"
}]
}
问题
因为此代码是 List<List<List<String>>>
,而不是 List<String>
[[[MHI, AIGSALPPMM, AIGSALPPM, 0, AIGSALPPMM, MORLGB2LXXX, AIGSALPPMM]]]
当您尝试从此列表中获取元素时
String data = dataJSON.get(i);
这将抛出 ClassCastException 异常,ArrayList 无法转换为 String。
解决方案
保存对 List<List<List<String>>>
List<List<List<String>>> accounts = ...extract().path("accounts.account.identifiers.identifier.accountId");
将List<List<List<String>>>
转换为List<String>
List<String> accountIds = accounts.stream()
.flatMap(l -> l.stream().flatMap(Collection::stream)).
collect(Collectors.toList());