5

List同じタイプの複数のオブジェクトをJavaでに変換しようとしています。たとえば、私のjsonは次のようになります。

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

私はクラスを持っています:

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

取得したjson文字列をオブジェクトのリストに変換できるようにしたいと思いExampleます。私はこのようなことをしたいと思います:

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

これを行うと、エラーが発生します。

Cannot set property Example on class java.util.ArrayList
4

4 に答える 4

4

JSONを次の形式に変更することで解決しました。

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

次に、Javaコードを使用しました:

JSONParser parser = new JSONParser();
    ArrayList list = parser.parse(ArrayList.class, json);
    List<Example> result = new ArrayList<Example>();
    for(int i = 0 ; i < list.size() ; i++){
        HashMap<String, String> map = (HashMap) list.get(i);
        Example example = new Example();
        example.setFoo(map.get("foo"));
        example.setBar(map.get("bar"));
        example.setFubar(map.get("fubar"));
        result.add(example);
    }
于 2012-05-23T18:47:12.183 に答える
4

この json を に変換することはできませんListが、これをMap.
あなたのjsonを見てくださいString

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

ここで、"Example" は key(String) で、value は Example の List オブジェクトです。

これを試して:

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

の完全なコード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;
    }
}

出力:

VALUE :->a1
VALUE :->a2
VALUE :->a3
于 2012-05-23T17:01:35.370 に答える
1

私はこのようなjsonstringを持っています:

[
{"author":"amahta","bookId":1,"bookName":"name"},
{"author":"amahta2","bookId":2,"bookName":"name2"}
]

次のコード スニペットでリストに変換します。

List<BookVO> listdata = new ArrayList<BookVO>();

JSONArray jArray = JSONArray.fromObject(booklist);
if (jArray != null) {
    for (int i = 0; i < jArray.size(); i++) {
        JSONObject obj = JSONObject.fromObject(jArray.get(i));
        listdata.add(new BookVO(Long.valueOf(obj.get("bookId")), obj.get("bookName"), obj.get("author")));
    }
}

私は net.sf.json-lib jar ファイルを使用します。

于 2014-05-05T05:03:50.423 に答える