Nirmal Gamage
Nirmal Gamage

Reputation: 177

Creating POJO objects from JSON array String

I have a JSON array string like below.

"[
  {
    "id" : "123",
    "name":"John Doe",
    "Address":"53/A"
    
   },
   {
    "id" : "1234",
    "name":"John Doe1",
    "Address":"53/AB"
    
   }
  
]"

I have a POJO class which maps with the inner JSON objects like below.

class User {
  String id;
  String name;
  String Address;
 //Getters & Setters
}

I want to create POJO objects from the above String and create a User ArrayList. How can I achieve this ? (using a library like Gson is fine). Thank you.

Upvotes: 2

Views: 3427

Answers (4)

bohme
bohme

Reputation: 81

You can use Jackson. Simple example you can find here: https://www.baeldung.com/jackson-collection-array#to-array

Example:

ObjectMapper mapper = new ObjectMapper();
String jsonArray = "[{\"stringValue\":\"a\",\"intValue\":1,\"booleanValue\":true}, {\"stringValue\":\"bc\",\"intValue\":3,\"booleanValue\":false}]";
MyDto[] asArray = mapper.readValue(jsonArray, MyDto[].class);

Upvotes: 1

Dale K James
Dale K James

Reputation: 204

ArrayList<LinkedTreeMap> list = gson.fromJson(json, ArrayList.class);
List<User> users= list.stream()
                  .map(s -> gson.fromJson(gson.toJson(s), User.class))
                  .collect(Collectors.toList());

Upvotes: 1

sparker
sparker

Reputation: 1578

You can use Jackson ObjectMapper and its very simple. below is the example code.

import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;

import java.io.IOException;
import java.util.List;

public class JsanParserExample {

    public static void main(String[] args) throws IOException {
        String json = "[\n" +
                "  {\n" +
                "    \"id\" : \"123\",\n" +
                "    \"name\":\"John Doe\",\n" +
                "    \"address\":\"53/A\"\n" +
                "    \n" +
                "   },\n" +
                "   {\n" +
                "    \"id\" : \"1234\",\n" +
                "    \"name\":\"John Doe1\",\n" +
                "    \"address\":\"53/AB\"\n" +
                "    \n" +
                "   }\n" +
                "  \n" +
                "]";

        ObjectMapper objectMapper = new ObjectMapper();
        List<User> userList = objectMapper.readValue(json,new TypeReference<List<User>>() {});
        userList.forEach(user -> System.out.println(user.getId()));
    }

    private static class User {
        private String id;
        private String name;
        private String address;

        public String getId() {
            return id;
        }

        public void setId(String id) {
            this.id = id;
        }

        public String getName() {
            return name;
        }

        public void setName(String name) {
            this.name = name;
        }

        public String getAddress() {
            return address;
        }

        public void setAddress(String address) {
            this.address = address;
        }
    }
}

Upvotes: 1

Silviu Burcea
Silviu Burcea

Reputation: 5348

Gson, since you mentioned it, offers a method (link) which is exactly what you're looking for.

Gson.fromJson​(java.lang.String json, java.lang.Class<T> classOfT)

If you want to deserialize a generic type (e.g. a list), docs advise you to use this method:

Gson.fromJson​(java.lang.String json, java.lang.reflect.Type typeOfT)

There is also a tutorial (link) which tackles a few use-cases close to what you want to do.

Upvotes: 2

Related Questions