NutzCN Logo
问答 JSON实体类序列化化后再次序列化回来变成NutMap了
发布于 2037天前 作者 threefish 3146 次浏览 复制 上一个帖子 下一个帖子
标签:

错误日志

1.r.68.v20190220
[18:13:23:248] [DEBUG] - org.nutz.castor.Castors.reload(Castors.java:116) - Using 107 castor for Castors
true
false

java.lang.ClassCastException: org.nutz.lang.util.NutMap cannot be cast to json.TestVO

	at json.JsonTest.test(JsonTest.java:26)
	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
	at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
	at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
	at java.lang.reflect.Method.invoke(Method.java:498)
	at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
	at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
	at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
	at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
	at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
	at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78)
	at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57)
	at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
	at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
	at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
	at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
	at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
	at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
	at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
	at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:68)
	at com.intellij.rt.execution.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:47)
	at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:242)
	at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:70)


代码

 @Test
    public void test() {
        System.out.println(Nutz.version());
        String json = "{'roleId':'2ftcjh15h2h0lok25df5loihi8','tableId':2,'fieldId':5,'name':'姓名','auth':'hide','auths':['hide','r','rw']}";
        TestVO roleFields = Json.fromJson(TestVO.class, json);
        System.out.println("姓名".equals(roleFields.getName()));
        String json2 = Json.toJson(roleFields, JsonFormat.compact()).replaceAll("\"", "'");
        System.out.println(json.equals(json2));
        ArrayList<TestVO> arr = new ArrayList();
        arr.add(roleFields);
        ArrayList<TestVO> list = Json.fromJson(ArrayList.class, Json.toJson(arr));
        System.out.println(list.get(0).getName());
    }
public class TestVO {

    private String roleId;

    private int tableId;

    private int fieldId;

    private String name;

    private FieldAuth auth;

    /**
     * 可以拥有的权限
     */
    private List<FieldAuth> auths;

    public TestVO() {
    }

    public TestVO(String roleId, int tableId, int fieldId, String name, FieldAuth auth, List<FieldAuth> auths) {
        this.roleId = roleId;
        this.tableId = tableId;
        this.fieldId = fieldId;
        this.name = name;
        this.auth = auth;
        this.auths = auths;
    }

    public String getRoleId() {
        return roleId;
    }

    public void setRoleId(String roleId) {
        this.roleId = roleId;
    }

    public int getTableId() {
        return tableId;
    }

    public void setTableId(int tableId) {
        this.tableId = tableId;
    }

    public int getFieldId() {
        return fieldId;
    }

    public void setFieldId(int fieldId) {
        this.fieldId = fieldId;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public FieldAuth getAuth() {
        return auth;
    }

    public void setAuth(FieldAuth auth) {
        this.auth = auth;
    }

    public List<FieldAuth> getAuths() {
        return auths;
    }

    public void setAuths(List<FieldAuth> auths) {
        this.auths = auths;
    }
}
package json;

public enum FieldAuth {

    hide("不可见", "hide"),
    r("只读", "r"),
    rw("读写", "rw");
    private final String lable;
    private final String value;

    FieldAuth(String lable, String value) {
        this.lable = lable;
        this.value = value;
    }

    public String getLable() {
        return lable;
    }

    public String getValue() {
        return value;
    }
}
3 回复

自己上个正解

 List<TestVO> testVOS= (List<TestVO>) Json.fromJson(PType.list(TestVO.class),Json.toJson(arr));

Json.fromJsonAsList才会有泛型信息

正解2

List<TestVO> testVOS2 = Json.fromJsonAsList(TestVO.class, Json.toJson(arr));
添加回复
请先登陆
回到顶部