2

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.

1

4 Answers 4

2

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.

Sign up to request clarification or add additional context in comments.

Comments

1
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());

Comments

1

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;
        }
    }
}

Comments

1

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);

2 Comments

While this link may answer the question, it is better to include the essential parts of the answer here and provide the link for reference. Link-only answers can become invalid if the linked page changes. - From Review
@Clijsters agree, I've added example to my answer

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.