如何在JSON中使用对象作为键?

Son*_*Ex2 2 java json user-input gson javascript-objects

我有这种整洁的数据格式,最好用对象作为键表示.所以我尝试使用以下JSON:

{
  "blocks": {
    "stone": {
      {
        "variant": ["bricks", "smooth"]
      }: {
        "sound": "guitar",
        "particle": "guitar",
      }
    },
    "dirt": {
      {
        "variant": "dirt"
      }: {
        "sound": "square",
        "particle": "note",
        "volume": 0.5
      }
    }
  }
}
Run Code Online (Sandbox Code Playgroud)

但它给了我一个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 */
}
Run Code Online (Sandbox Code Playgroud)
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 */
}
Run Code Online (Sandbox Code Playgroud)
import java.util.*;
public class BlockStateMatcher {
  private final Map<PropertyMap, Instrument> states;
  /* etc */
}
Run Code Online (Sandbox Code Playgroud)
import java.util.*;
public class PropertyMap extends HashMap<Property<T>, List<PropertyValue<T>>> /* simplified */ {
  /* etc */
}
Run Code Online (Sandbox Code Playgroud)

Rob*_*sen 5

你不能.它不是有效的JSON语法.

根据文档,对象键需要是字符串,并根据规范:

对象结构表示为围绕零个或多个名称/值对(或成员)的一对花括号.名称是一个字符串.