我有这种整洁的数据格式,最好用对象作为键来表示。所以我尝试使用以下JSON:
{
"blocks": {
"stone": {
{
"variant": ["bricks", "smooth"]
}: {
"sound": "guitar",
"particle": "guitar",
}
},
"dirt": {
{
"variant": "dirt"
}: {
"sound": "square",
"particle": "note",
"volume": 0.5
}
}
}
}
但它给了我一个JsonSyntaxException。我正在使用GSON btw。我怎样才能做到这一点?
数据结构:
import java.util.*;
public class Instrument {
private final String name;
private final String particle;
private final float volume;
public Instrument(String name, Optional<String> particle, Optional<Float> volume) {
this.name = name;
this.particle = particle.orElse("note");
this.volume = volume.orElse(1.0f);
}
/* getters and stuff */
}
public class BlockType {
private final String name;
private final BlockStateMatcher state;
public BlockType(String name, BlockStateMatcher state) {
this.name = name;
this.state = state;
}
/* getters and stuff */
}
import java.util.*;
public class BlockStateMatcher {
private final Map<PropertyMap, Instrument> states;
/* etc */
}
import java.util.*;
public class PropertyMap extends HashMap<Property<T>, List<PropertyValue<T>>> /* simplified */ {
/* etc */
}
答案 0 :(得分:5)
答案 1 :(得分:1)
您提供的文字无效json。
如果您的文字是
{
"blocks": [{
"name": "stone",
"variant": "bricks",
"sound": "guitar",
"particle": "guitar"
}, {
"name": "dirt",
"variant": "dirt",
"sound": "square",
"particle": "note",
"volume": 0.5
}]
}
你可以解析它。