User
I'm testing a API where it return response as a Array,I am finding it difficult in storing in list or Array?
I am using JsonPath to fetch the records
API response looks like this
[
"String1",
"String2",
"String3",
"String4",
"String5"
]
I am Using below code
Response response;
JsonPath jsonPathEvaluator = response.jsonPath();
jsonPathEvaluator.getString("[0]");
User
It is better if you have tags for you element lists if they are of one type:-
{
"Strings":[
"String1",
"String2",
"String3",
"String4",
"String5"
]
}
JsonArray arrObj = empObj.getJsonArray("Strings");
This will return you a JsonArray that you can convert to any type based on your requirement
If you cannot change the input json , maybe this could help :-
String [] json = new String[10];
obj = parser.parse(response);
String jsoString = obj.toString();
List<String> items = Arrays.asList(jsoString.split("\\s*,\\s*"));
General Tech Technology & Software
1 Replies