I\'m very new to Java 8 lambdas and stuff... I want to write a lambda function that takes a JsonArray, goes over its JsonObjects and creates a list of values of certain fiel
Yes, I recognized that result of put is being added to json array instead of json object. Should be like this.
private static JSONObject getJson(String key, String value){
JSONObject result = new JSONObject();
result.put(key, value);
return result;
}
public static void main(String[] args) {
JSONArray jsonArray = new JSONArray();
jsonArray.add(getJson("name", "John"));
jsonArray.add(getJson("name", "David"));
List list = (List) jsonArray.stream()
.map(json -> json.toString())
.collect(Collectors.toList());
System.out.println(list);
}
and output is now [{"name":"John"}, {"name":"David"}] like expected