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

  • Home
  • SEARCH
  • 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 7920667
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T16:18:16+00:00 2026-06-03T16:18:16+00:00

I know i can convert JSON to an object using HJackson. Below is the

  • 0

I know i can convert JSON to an object using HJackson. Below is the code snippet to do it.

    JsonFactory jsonFactory = new JsonFactory();
    ObjectMapper mapper = new ObjectMapper(jsonFactory);
    DTO1 user = mapper.readValue(new File("c:\\oneFile.json"),
                DTO1.class);

But my requirment is to convert JSON to multiple objects. In my case to multiple objects like DTO1, DTO2…. and also to String variables if possible.

Is there an API in Jackson to perform this operation.
Or is there a way where i can manually pick and store them in corresponding DTO’s(Objects).

Regards
Karan

  • 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-06-03T16:18:19+00:00Added an answer on June 3, 2026 at 4:18 pm

    From http://wiki.fasterxml.com/JacksonInFiveMinutes (but also look at TypeFactory in Jackson 1.3+) :

    Data Binding with Generics

    In addition to binding to POJOs and “simple” types, there is one additional variant: that of binding to generic (typed) containers. This case requires special handling due to so-called Type Erasure (used by Java to implement generics in somewhat backwards compatible way), which prevents you from using something like Collection.class (which does not compile).

    So if you want to bind data into a Map you will need to use:

      Map<String,User> result = mapper.readValue(src, new TypeReference<Map<String,User>>() { });
    

    where TypeReference is only needed to pass generic type definition (via anynomous inner class in this case): the important part is > which defines type to bind to.

    If you don’t do this (and just pass Map.class), call is equivalent to binding to Map (i.e. “untyped” Map), as explained above.


    If you mean polymorphic deserialization, check this link http://programmerbruce.blogspot.com/2011/05/deserialize-json-with-jackson-into.html which discusses using the jackson annotations to assist the mapper in picking the right object for the job.


    I wrote this today (I’m sure it can be improved) for simply taking a JSON string and always converting it into a List of DTOs (or whatever it can be converted into). This could be called with any type, and could be modified to return a single instance, or List, or whatever you wished. OBJECT_MAPPER is a static ObjectMapper variable.

      public static <T> List<T> getFromJSONCollection(String jsonString, final Class<T> type) throws IOException {
        try {
          return OBJECT_MAPPER.readValue(jsonString, TypeFactory.defaultInstance().constructCollectionType(ArrayList.class, type));
        } catch (JsonMappingException e) {
          return getFromJSON(jsonString, type);
        }
      }
    
      public static <T> List<T> getFromJSON(final String jsonString, final Class<T> type) throws IOException {
        return new ArrayList<T>() {{ add(OBJECT_MAPPER.readValue(jsonString, type));}};
      }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to know how can I convert a Java object to JSON by
I know the method to convert a JS object into a JSON string by
I'm using jackson to convert an object of mine to json. The object has
So I know I can convert a string to a hashcode simply by doing
Does any on know how can I convert this $events = array( array(type =>
I just want to know if there is a program that can convert an
How can I convert this document: <Root> <!-- yes, I know I don't need
i know there is a convert -swirl effect in imagemagick but can i create
I have a query that I know can be done using a subselect, but
I have a object that im serializing to a JSON object using JSON.Net. This

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.