NutzCN Logo
精华 Json字符串如何转Enum
发布于 474天前 作者 lihongjie0209 665 次浏览 复制 上一个帖子 下一个帖子
标签:

Enum:

@JsonShape(Type.OBJECT)
public enum TestEnum {
	Test("name", "value");

	public String name;
	public String value;

	TestEnum() {
	}

	TestEnum(String name, String value) {

		this.name = name;
		this.value = value;
	}


Json字符串:

{
   "name": "name",
   "value": "value"
}

我应该如何转出Test这个Enum

8 回复

当前报什么错误呢?

[DEBUG] 09:27:34.248 org.nutz.castor.Castors.reload(Castors.java:116) - Using 101 castor for Castors
{
   "name": "name",
   "value": "value"
}

org.nutz.lang.born.BorningException: Fail to born or cast to 'TestEnum'
 by args: []
 because: No suitable Constructor or Factory Method!!

	at org.nutz.lang.Mirror.born(Mirror.java:1020)
	at org.nutz.mapl.impl.convert.ObjConvertImpl.injectObj(ObjConvertImpl.java:218)
	at org.nutz.mapl.impl.convert.ObjConvertImpl.inject(ObjConvertImpl.java:91)
	at org.nutz.mapl.impl.convert.ObjConvertImpl.convert(ObjConvertImpl.java:72)
	at org.nutz.mapl.Mapl.maplistToObj(Mapl.java:34)
	at org.nutz.json.Json.parse(Json.java:94)
	at org.nutz.json.Json.fromJson(Json.java:73)
	at org.nutz.json.Json.fromJson(Json.java:178)
	at JsonTest.TestJson(JsonTest.java:19)
	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)


Process finished with exit code -1

用的是1.r.65吗?

嗯, 最新的nutzboot

在系统启动时候设置一下JsonCallback

        Json.getEntity(Mirror.me(TestEnum.class)).setJsonCallback(new JsonCallback() {
            public boolean toJson(Object obj, JsonFormat jf, Writer writer) throws IOException {
                return false;
            }
            public Object fromJson(Object obj) {
                if (obj == null)
                    return null;
                if (obj instanceof Map)
                    return TestEnum.valueOf((String)((Map)obj).get("name"));
                return TestEnum.valueOf(obj.toString());
            }
        });

添加回复
请先登陆
回到顶部