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 7987455
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T12:09:14+00:00 2026-06-04T12:09:14+00:00

I am trying to convert multiple objects of the same type into a List

  • 0

I am trying to convert multiple objects of the same type into a List in Java. For example, my json would be:

{
    "Example": [
        {
            "foo": "a1",
            "bar": "b1",
            "fubar": "c1"
        },
        {
            "foo": "a2",
            "bar": "b2",
            "fubar": "c2"
        },
        {
            "foo": "a3",
            "bar": "b3",
            "fubar": "c3"
        }
    ]
}

I have a class:

public class Example {
    private String foo;
    private String bar;
    private String fubar;
    public Example(){};
    public void setFoo(String f){
        foo = f;
    }
    public void setBar(String b){
        bar = b;
    }
    public void setFubar(String f){
        fubar = f;
    }
...
}

I want to be able to turn the json string I get into a list of Example objects. I would like to do something like this:

JSONParser parser = new JSONParser();
parser.addTypeHint(".Example[]", Example.class);
List<Example> result = parser.parse(List.class, json);

Doing this I get an error:

Cannot set property Example on class java.util.ArrayList
  • 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-04T12:09:15+00:00Added an answer on June 4, 2026 at 12:09 pm

    You cannot convert this json to List but you can convert this to Map.
    See your json String:

    ...
    "Example": [
            {
                "foo": "a1",
                "bar": "b1",
                "fubar": "c1"
            },
            {
                "foo": "a2",
                "bar": "b2",
                "fubar": "c2"
            },
            ...
    ]
    }
    

    Here “Example” is key(String) and value is List object of Example.

    Try this:

     parser.addTypeHint("Example[]", Example.class);
     Map<String,List<Example>> result1 = parser.parse(Map.class, json);
     for (Entry<String, List<Example>> entry : result1.entrySet()) {
         for (Example example : entry.getValue()) {
              System.out.println("VALUE :->"+ example.getFoo());
         }
     }
    

    Full code of Example:

    import java.util.List;
    import java.util.Map;
    import java.util.Map.Entry;
    
    import org.svenson.JSONParser;
    
    public class Test {
        public static void main(String[] args) {
            JSONParser parser = new JSONParser();
            parser.addTypeHint(".Example[]", Example.class);
            String json = "{" + "\"Example\": [" + "{" + "\"foo\": \"a1\","
                    + "\"bar\": \"b1\"," + "\"fubar\": \"c1\"" + "}," + "{"
                    + "\"foo\": \"a2\"," + "\"bar\": \"b2\"," + "\"fubar\": \"c2\""
                    + "}," + "{" + "\"foo\": \"a3\"," + "\"bar\": \"b3\","
                    + "\"fubar\": \"c3\"" + "}" + "]" + "}\"";
            parser.addTypeHint("Example[]", Example.class);
            Map<String, List<Example>> result1 = parser.parse(Map.class, json);
            for (Entry<String, List<Example>> entry : result1.entrySet()) {
                for (Example example : entry.getValue()) {
                    System.out.println("VALUE :->" + example.getFoo());
                }
            }
        }
    }
    
    public class Example {
        private String foo;
        private String bar;
        private String fubar;
        public Example(){}
        public void setFoo(String foo) {
            this.foo = foo;
        }
        public String getFoo() {
            return foo;
        }
        public void setBar(String bar) {
            this.bar = bar;
        }
        public String getBar() {
            return bar;
        }
        public void setFubar(String fubar) {
            this.fubar = fubar;
        }
        public String getFubar() {
            return fubar;
        }
    }
    

    OutPut:

    VALUE :->a1
    VALUE :->a2
    VALUE :->a3
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to convert multiple .tif files into .png files using Imagemagick on the
I'm trying to convert some SQL queries into Linq to avoid multiple trips to
I'm trying to convert varchar dates with multiple formats into a standard DateTime format
Trying to convert this c code into MIPS and run it in SPIM. int
I am trying to convert a string value into a name of an attribute
I am trying to convert as following: bool foo(int a, unsigned short b) {
I'm trying to convert an old 'C' program containing some static methods into Obj-c
I'm trying to convert the RealViewSwitcher based on the work from Marc Reichelt into
I am trying to convert multiple images to pdf using pdfsharp library . I
I am trying to convert my shell scripts into python code, but I am

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.