二维对象数组 YAML 到 Java
Posted
技术标签:
【中文标题】二维对象数组 YAML 到 Java【英文标题】:2D array of objects YAML to Java 【发布时间】:2014-06-11 12:59:32 【问题描述】:我正在尝试在 Java 中反序列化我在 YAML 中组合的二维数组。我正在为此使用 Yamlbeans 库。
这是我的 YAML:
states:
- 0:
- name: SPAWNINGSTATE
duration: 4
这是我的课:
public class State
public String name;
public long duration;
public State()
public State(String name, long duration)
this.name = name;
this.duration = duration;
数组在另一个类中声明,只有一行:
public State[][] states;
这是我得到的错误:
com.esotericsoftware.yamlbeans.YamlReader$YamlReaderException: Line 57, column 5: Error creating object.
at com.esotericsoftware.yamlbeans.YamlReader.readValueInternal(YamlReader.java:266)
at com.esotericsoftware.yamlbeans.YamlReader.readValue(YamlReader.java:152)
at com.esotericsoftware.yamlbeans.YamlReader.readValueInternal(YamlReader.java:335)
at com.esotericsoftware.yamlbeans.YamlReader.readValue(YamlReader.java:152)
at com.esotericsoftware.yamlbeans.YamlReader.readValueInternal(YamlReader.java:295)
at com.esotericsoftware.yamlbeans.YamlReader.readValue(YamlReader.java:152)
at com.esotericsoftware.yamlbeans.YamlReader.read(YamlReader.java:103)
at com.esotericsoftware.yamlbeans.YamlReader.read(YamlReader.java:90)
at com.sbg.wickedtemplate.layers.ForegroundLightning.<init>(ForegroundLightning.java:22)
at com.sbg.wickedtemplate.World.<init>(World.java:25)
at com.sbg.wickedtemplate.LWP_Engine.create(LWP_Engine.java:39)
at com.badlogic.gdx.backends.lwjgl.LwjglApplication.mainLoop(LwjglApplication.java:136)
at com.badlogic.gdx.backends.lwjgl.LwjglApplication$1.run(LwjglApplication.java:114)
Caused by: java.lang.reflect.InvocationTargetException: Unable to find a no-arg constructor for class: [Lcom.sbg.wickedtemplate.State;
at com.esotericsoftware.yamlbeans.Beans.createObject(Beans.java:111)
at com.esotericsoftware.yamlbeans.YamlReader.createObject(YamlReader.java:363)
at com.esotericsoftware.yamlbeans.YamlReader.readValueInternal(YamlReader.java:264)
制作常规数组没有问题。为什么它告诉我它找不到无参数构造函数,而它显然存在?我做错了什么?
【问题讨论】:
【参考方案1】:你能尝试修复你的一个节点吗?
states:
0:
name: SPAWNINGSTATE
duration: 4
“-”使它成为一个数组,它可能会丢弃它。
【讨论】:
以上是关于二维对象数组 YAML 到 Java的主要内容,如果未能解决你的问题,请参考以下文章