I got JSON like
{
"items":[
{
"id":15
,"name":"abc"
}
,{
"id":16
,"name":"xyz%"
}
,{
"id":17
,"name":"qwerty"
}
,{
"id":18
,"name":"rudloph"
}
,{
"id":19
,"name":"jane"
}
,{
"id":20
,"name":"doe"
}
]
}
I have class which is like:
public class Foo {
public String id;
public String name;
}
And I want to convert this JSON into List<Foo>
. How can i do this? Right now I am doing like:
List<Foo> fooList = new ArrayList<>();
JSONObject jsonObject = new JSONObject(json);
JSONArray araray = jsonObject.getJSONArray("items");
for(int i =0 ; i < araray.length();i++){
Foo dto = new Foo();
dto.setId(Long.valueOf((String) araray.getJSONObject(i).get("id")));
dto.setName((String) araray.getJSONObject(i).get("name"));
fooList.add(dto);
}
PS: Cannot change JSON. Jackson or Gson. Please let me know with code example.