Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6255175
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T14:18:55+00:00 2026-05-24T14:18:55+00:00

I have a question which is similar to some questions at stackoverflow but none

  • 0

I have a question which is similar to some questions at stackoverflow but none really answer my problem. I use the ObjectMapper of Jackson and want to parse this JSON string into an List of User objects:

[{ "user" : "Tom", "role" : "READER" }, 
 { "user" : "Agnes", "role" : "MEMBER" }]

I define an inner class like this:

public class UserRole {

    private String user
    private String role;

    public void setUser(String user) {
        this.user = user;
    }

    public void setRole(String role) {
        this.role = role;
    }

    public String getUser() {
        return user;
    }

    public String getRole() {
        return role;
    }
}

To parse the JSON String to an List of UserRoles I use generics:

protected <T> List<T> mapJsonToObjectList(String json) throws Exception {
    List<T> list;
    try {
        list = mapper.readValue(json, new TypeReference<List<T>>() {});
    } catch (Exception e) {
        throw new Exception("was not able to parse json");
    }
    return list;
}

But what I get back is a List of LinkedHashMaps.

What is wrong with my code?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-24T14:18:56+00:00Added an answer on May 24, 2026 at 2:18 pm

    The following works and as per StaxMan’s advice no longer uses the deprecated static collectionType() method.

    public class SoApp
    {
    
       /**
        * @param args
        * @throws Exception
        */
       public static void main(String[] args) throws Exception
       {
          System.out.println("Hello World!");
    
          String s = "[{\"user\":\"TestCity\",\"role\":\"TestCountry\"},{\"user\":\"TestCity\",\"role\":\"TestCountry\"}]";
          StringReader sr = new StringReader("{\"user\":\"TestCity\",\"role\":\"TestCountry\"}");
          //UserRole user = mapper.readValue(sr, UserRole.class);
    
          mapJsonToObjectList(s,UserRole.class);
    
       }
    
       protected static <T> List<T> mapJsonToObjectList(String json, Class<T> clazz) throws Exception
       {
          List<T> list;
          ObjectMapper mapper = new ObjectMapper();
          System.out.println(json);
          TypeFactory t = TypeFactory.defaultInstance();
          list = mapper.readValue(json, t.constructCollectionType(ArrayList.class,clazz));
    
          System.out.println(list);
          System.out.println(list.get(0).getClass());
          return list;
       }
    }
    

    …

    public class UserRole{
    
       private String user;
       private String role;
    
       public void setUser(String user) {
           this.user = user;
       }
    
       public void setRole(String role) {
           this.role = role;
       }
    
       public String getUser() {
           return user;
       }
    
       public String getRole() {
           return role;
       }
    
       @Override
       public String toString()
       {
          return "UserRole [user=" + user + ", role=" + role + "]";
       }
       
    }
    

    output…

     Hello World!
    [{"user":"TestCity","role":"TestCountry"},{"user":"TestCity","role":"TestCountry"}]
    [UserRole [user=TestCity, role=TestCountry], UserRole [user=TestCity, role=TestCountry]]
    class com.test.so.fix.UserRole
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an SQL question which may be basic to some but is confusing
This question is very similar to https://stackoverflow.com/questions/6839516/outlook-2010-reopen-messages which was closed as not being a
I have seen some similar questions on stackoverflow and on searched on google too
i have a table which looks similar to this <asp:TableRow><asp:TableCell>Question 1</asp:TableCell><asp:TableCell ID =Question1Text></asp:TableCell></asp:TableRow> <asp:TableRow><asp:TableCell
I am seeing a similar issue as mentioned in this question - https://stackoverflow.com/questions/1495390/how-do-i-invalidate-a-session-in-jsf .
I've asked a similar question before, but I've done some more research and this
I realise there are some similar questions on here already but I couldn't see
I have searched this question but didn't get / or understood the answer I
I have searched for some similar questions before posting - however I have a
I have a question which is described below: What problems would arise for testing

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.