in Education by
What I'm trying to do here is iterate inside a Json Object obtained from an API Call in order to search a value related from a Key. However I'm failing in obtaining the tuples from every iteration. From the following code: public void findResponseValue(String param, String inputValue){ ArrayList jsonArray = response.getBody().jsonPath().get(""); Iterator iterator = jsonArray.iterator(); while(iterator.hasNext()){ System.out.println(iterator.next()); iterator.next(); } I receive the following error java.lang.ClassCastException: class java.util.HashMap cannot be cast to class java.lang.String (java.util.HashMap and java.lang.String are in module java.base of loader 'bootstrap') It correctly iterates over the arrayList, but it fails trying to take a value from it. Response Body example [ { "creationDate": "2017-05-29T07:45:43Z", "createdBy": "YYY.YYY", "lastUpdate": "2018-09-26T10:10:12Z", "updatedBy": "XXX.XXX", "id": 22, "partner": "ALS", "countries": [ ], "countriesCodes": "NL", "salesChannels": [ { "code": "Integrated" } ], "touchPoints": [ ], "serviceEntity": "AWPNL", "flag": false, "activated": true, "type": "dcx" }, { "creationDate": "2017-06-01T13:22:29Z", "createdBy": "YYY.YYY", "lastUpdate": "2017-11-30T12:24:36Z", "updatedBy": "MMM.T.NNN", "id": 39, "partner": "ALS", "countries": [ ], "countriesCodes": "ES", "salesChannels": [ { "code": "Integrated" } ], "touchPoints": [ ], "serviceEntity": "AWPES", "flag": false, "activated": true, "type": "dcx" } ] JavaScript questions and answers, JavaScript questions pdf, JavaScript question bank, JavaScript questions and answers pdf, mcq on JavaScript pdf, JavaScript questions and solutions, JavaScript mcq Test , Interview JavaScript questions, JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)

1 Answer

0 votes
by
Your response body should be as below: {"data":[ { "creationDate": "2017-05-29T07:45:43Z", "createdBy": "YYY.YYY", "lastUpdate": "2018-09-26T10:10:12Z", "updatedBy": "XXX.XXX", "id": 22, "partner": "ALS", "countries": [ ], "countriesCodes": "NL", "salesChannels": [ { "code": "Integrated" } ], "touchPoints": [ ], "serviceEntity": "AWPNL", "flag": false, "activated": true, "type": "dcx" }, { "creationDate": "2017-06-01T13:22:29Z", "createdBy": "YYY.YYY", "lastUpdate": "2017-11-30T12:24:36Z", "updatedBy": "MMM.T.NNN", "id": 39, "partner": "ALS", "countries": [ ], "countriesCodes": "ES", "salesChannels": [ { "code": "Integrated" } ], "touchPoints": [ ], "serviceEntity": "AWPES", "flag": false, "activated": true, "type": "dcx" } ]} This is a method I used (Gson parser API used). In for loop you can compare your key or value. public static void findResponseValue(String param, String inputValue){ JsonObject jsonObject = new JsonParser().parse(jsonResponseBody).getAsJsonObject(); JsonElement jsonElement = jsonObject.get("data"); JsonArray jsonArray = jsonElement.getAsJsonArray(); for (int i = 0; i < jsonArray.size(); i++) { System.out.println(i+". Array element Content:"+jsonArray.get(i)); JsonObject jsonObj = jsonArray.get(i).getAsJsonObject(); System.out.println("\t creationDate:"+jsonObj.get("creationDate")+" partner:"+jsonObj.get("partner")); } }

Related questions

0 votes
    state one advantage and one disavantage of using recursion over iteration Select the correct answer from above options...
asked Dec 25, 2021 in Education by JackTerrance
0 votes
    Which of the following model sums the importance over each boosting iteration? (a) Boosted trees (b) Bagged ... and answers pdf, Data Science interview questions for beginners...
asked Oct 28, 2021 in Education by JackTerrance
0 votes
    Which of the following model sums the importance over each boosting iteration? (a) Boosted trees (b) Bagged trees (c) Partial least squares (d) None of the mentioned...
asked Oct 9, 2021 in Technology by JackTerrance
0 votes
    I want to send the following JSON text {"Email":"[email protected]","Password":"123456"} to a web ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Feb 13, 2022 in Education by JackTerrance
0 votes
    I have the following stored in an Oracle database as JSON: { value: [1,2,3] } The value can be ... , JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 24, 2022 in Education by JackTerrance
0 votes
    newbie question: how do i make my JSON output ignore null values? I don't want to necessarily set ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 7, 2022 in Education by JackTerrance
0 votes
    newbie question: how do i make my JSON output ignore null values? I don't want to necessarily set ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 7, 2022 in Education by JackTerrance
0 votes
    How to get ASP.NET Web API to return JSON instead of XML using Chrome?...
asked Jan 11, 2021 in Technology by JackTerrance
0 votes
    I am working in an graphql application where I have to send custom error object / message in json ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 26, 2022 in Education by JackTerrance
0 votes
    I have one column in my table which will store data in string format the sample data is {"pre- ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Feb 27, 2022 in Education by JackTerrance
0 votes
    In java, how can I parse json text to get value of variables like post_id ? Here is my json text { "page": { ... ": "1234567890" } ] } Select the correct answer from above options...
asked Jan 24, 2022 in Education by JackTerrance
0 votes
    Let's say there is a table structured like this: ID | article_id | article_count | created_at ---| ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 19, 2022 in Education by JackTerrance
0 votes
    Let's say there is a table structured like this: ID | article_id | article_count | created_at ---| ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 17, 2022 in Education by JackTerrance
0 votes
    Which is the new method introduced in java 8 to iterate over a collection? (a) for (String i : StringList) ... & Miscellaneous of Java Select the correct answer from above options...
asked Feb 23, 2022 in Education by JackTerrance
...