我有這個整齊的數據格式,最好用對象作爲鍵來表示。所以我試着用下面的JSON:如何使用對象作爲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 */
}
這不是有效的JSON語法。請參閱http://jsonlint.com/ –
@ScaryWombat我不能以某種方式使用此語法嗎? – SoniEx2
當然,但它不是JSON。 –