0

json-libを使用してjson オブジェクトを Java に変換しています。コードは次のとおりです。

public class JsonConvertorDemo {

    public static void main(String[] args) {
        B b1 = new B("b1");
        Map<String, B> bMap = new HashMap<String, B>();
        bMap.put("key1", b1);
        A a1 = new A(bMap);

        JSONObject jsonObject = JSONObject.fromObject(a1);
        String json = jsonObject.toString();
        jsonObject = JSONObject.fromObject(json);

        Map<String, Class> classMap = new HashMap<String, Class>();
        classMap.put("bMap", Map.class);
        a1 = (A) JSONObject.toBean(jsonObject, A.class, classMap);

        bMap = a1.getbMap();
        System.out.println(bMap.get("key1").getB1());
    }
}

public class A {
    private Map<String, B> bMap = new HashMap<String, B>();
    public A() {}
    public A(Map<String, B> bMap) {
        this.bMap = bMap;
    }
    public Map<String, B> getbMap() {
        return bMap;
    }
    public void setbMap(Map<String, B> bMap) {
        this.bMap = bMap;
    }
}

public class B {
    private String b1;
    public B() {}
    public B(String b1) {
        this.b1 = b1;
    }
    public String getB1() {
        return b1;
    }
    public void setB1(String b1) {
        this.b1 = b1;
    }
}

次の例外がスローされます。

スレッド「メイン」の例外 java.lang.ClassCastException: net.sf.ezmorph.bean.MorphDynaBean を code.orgexample.json.JsonConvertorDemo.main(JsonConvertorDemo.java:30) で code.orgexample.json.B
にキャストできません

json-lib でマップの値のクラス タイプを指定する方法はありますか?

助けてくれてありがとう。

4

5 に答える 5

1

http://hw1287789687.iteye.com/admin/blogs/1993048

JsonConfig jsonConfig = new JsonConfig();

    jsonConfig.setRootClass(Class2.class);
    Map<String, Class> classMap = new HashMap<String, Class>();
    classMap.put("students", Student.class); // 指定JsonRpcRequest的request字段的内部类型
    jsonConfig.setClassMap(classMap);
于 2013-12-20T08:29:42.390 に答える
1

クロが言ったように:

ここでは、fromObject が JSON 形式の文字列、Maps、DynaBeans、および JavaBeans を受け入れると述べています。

私のコードでは、 ClassCastException は次の原因で発生しました。

for (TheClass childNode : command.getChildren()) {

コードを次のように変更すると、すべてが期待どおりに機能しました。

for (Object childNode : command.getChildren()) {
        JSONObject fromObject = JSONObject.fromObject(childNode);
        TheClass childCommand = (TheClass) JSONObject.toBean(fromObject,
            TheClass.class);
    }
于 2013-04-24T08:56:56.100 に答える
1

ここでは、fromObjectがJSON 形式の文字列、Maps、DynaBeans、および JavaBeans を受け入れると述べています。

于 2011-06-20T13:58:07.733 に答える
0

問題を早急に解決するには、クラス JsonConverterDemo、A、および B のすべてのコードを送信してください。特に、パッケージ宣言の欠如、インポート ステートメント、および行番号が問題の特定を妨げています。

于 2011-05-14T11:21:32.353 に答える
0

Is there a way to specify class type of a map's value in json-lib?

Nope. Same when deserializing to a List<CustomType>, even if you told it what type you want with the toBean call.

After the call to toBean, the values in the collection will be DynaBeans. You have to iterate through the collection values and morph them into the preferred types. The morphing can be done manually, a field at a time, or in a more automatic fashion with a net.sf.ezmorph.Morpher registered in the MorpherRegistry.

WARNING: Even with this approach, you have to be careful about how you reference the value before you morph it to an instance of the target type. The compiler (and thus the runtime) thinks the value is of the parameterized type (if using generics), and so it will gladly try to use it as that type. This of course causes a ClassCastException (even if your code doesn't do any explicit type casting). So, when accessing the values, just get to them by declaring a reference of type Object and using it. Don't try to use the values in any other way without the explicit Object type reference. (You'll know what I'm talking about when you write the code and see the errors. I'm too busy to code an example, right now.)

于 2011-06-24T04:20:29.033 に答える