2

I have some api parameters which I have received from a json file in test method. Now I need to convert this object to a list, because it contains arrays that I would like to handle individually.

 @SuppressWarnings("unchecked")
    static Optional getQueryParametersTEST(JsonObject inputJsonObject) {
        JsonObject operators = inputJsonObject.getJsonObject("operators");
        JsonObject tez = operators.getJsonObject("tez");
        JsonArray parameters = tez.getJsonArray("parameters");

        Optional<JsonObject> queryParameters = parameters.stream().
            filter(JsonObject.class::isInstance).
            map(JsonObject.class::cast).
            filter(jsonObject -> jsonObject.getJsonObject("queryParameters") != null).
            map(item -> item.getJsonObject("queryParameters")).
            findFirst();

        return  queryParameters; 
    }

after i received queryParameters it comes in this format

Optional[{"priceMin":["0"],"priceMax":["150000"],"currency":["5561"],"hotelClassId":[""],"accommodationId":["158525","9002884","1"],"rAndBId":["15350","2424","2474","2749","5737","5738"]}

What can i do to get a list of Strings

I tried to perform a

return  queryParameters.ifPresent(newList::add)
            .flatMap(Optional::stream)
            .collect(Collectors.toList());

Tried using this but Jsonobject requires a source of string but i have queryParameters of type Optional queryParameters JSONObject obj = new JSONObject(my source should be the query param )

    List<String> list = new ArrayList<String>();
    JSONArray array = obj.getJSONArray("interests");
    for(int i = 0 ; i < array.length() ; i++){
        list.add(array.getJSONObject(i).getString("interestKey"));
    }


    JSONObject obj = new JSONObject("{interests : [{interestKey:Dogs}, {interestKey:Cats}]}");

    List<String> list = new ArrayList<String>();
    JSONArray array = obj.getJSONArray("interests");
    for(int i = 0 ; i < array.length() ; i++){
        list.add(array.getJSONObject(i).getString("interestKey"));
    }
valik
  • 2,014
  • 4
  • 24
  • 55

0 Answers0