gpt4 book ai didi

java - 如何将对象用作 JSON 中的键?

转载 作者:行者123 更新时间:2023-12-03 19:40:20 25 4
gpt4 key购买 nike

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

{
"blocks": {
"stone": {
{
"variant": ["bricks", "smooth"]
}: {
"sound": "guitar",
"particle": "guitar",
}
},
"dirt": {
{
"variant": "dirt"
}: {
"sound": "square",
"particle": "note",
"volume": 0.5
}
}
}
}

但它给了我一个 JsonSyntaxException。我正在使用 GSON 顺便说一句。我怎样才能使它工作?

数据结构:

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 语法。

根据 documentation ,对象键需要是字符串,并且根据 spec :

An object structure is represented as a pair of curly bracketssurrounding zero or more name/value pairs (or members). A name is astring.

关于java - 如何将对象用作 JSON 中的键?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40296396/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com