Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I convert a JSON array into a Java List. I'm using svenson

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
like image 673
Boundless Avatar asked May 23 '12 14:05

Boundless


1 Answers

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
like image 122
Subhrajyoti Majumder Avatar answered Sep 22 '22 12:09

Subhrajyoti Majumder