如何为 Json 对象使用动态属性名称

我们如何使 JSON 属性名称动态化。例如


public class Value {

    @JsonProperty(value = "value")

    private String val;


    public void setVal(String val) {

        this.val = val;

    }


    public String getVal() {

        return val;

    }

}

序列化此对象时,它被保存为,{"value": "actual_value_saved"}但我想让密钥也像{"new_key": "actual_value_saved"}. 任何帮助深表感谢。


慕容708150
浏览 323回答 3
3回答

凤凰求蛊

您可以使用JsonAnySetter&nbsp;JsonAnyGetter注释。后面可以使用Map实例。如果您总是one-key-object可以Collections.singletonMap在其他情况下使用HashMap或其他实现中使用。下面的示例显示了您可以轻松地使用这种方法并根据key需要创建任意数量的随机 -s:import com.fasterxml.jackson.annotation.JsonAnyGetter;import com.fasterxml.jackson.annotation.JsonAnySetter;import com.fasterxml.jackson.databind.ObjectMapper;import java.util.Collections;import java.util.Map;import java.util.Objects;public class JsonApp {&nbsp; &nbsp; public static void main(String[] args) throws Exception {&nbsp; &nbsp; &nbsp; &nbsp; DynamicJsonsFactory factory = new DynamicJsonsFactory();&nbsp; &nbsp; &nbsp; &nbsp; ObjectMapper mapper = new ObjectMapper();&nbsp; &nbsp; &nbsp; &nbsp; System.out.println(mapper.writeValueAsString(factory.createUser("Vika")));&nbsp; &nbsp; &nbsp; &nbsp; System.out.println(mapper.writeValueAsString(factory.createPhone("123-456-78-9")));&nbsp; &nbsp; &nbsp; &nbsp; System.out.println(mapper.writeValueAsString(factory.any("val", "VAL!")));&nbsp; &nbsp; }}class Value {&nbsp; &nbsp; private Map<String, String> values;&nbsp; &nbsp; @JsonAnySetter&nbsp; &nbsp; public void put(String key, String value) {&nbsp; &nbsp; &nbsp; &nbsp; values = Collections.singletonMap(key, value);&nbsp; &nbsp; }&nbsp; &nbsp; @JsonAnyGetter&nbsp; &nbsp; public Map<String, String> getValues() {&nbsp; &nbsp; &nbsp; &nbsp; return values;&nbsp; &nbsp; }&nbsp; &nbsp; @Override&nbsp; &nbsp; public String toString() {&nbsp; &nbsp; &nbsp; &nbsp; return values.toString();&nbsp; &nbsp; }}class DynamicJsonsFactory {&nbsp; &nbsp; public Value createUser(String name) {&nbsp; &nbsp; &nbsp; &nbsp; return any("name", name);&nbsp; &nbsp; }&nbsp; &nbsp; public Value createPhone(String number) {&nbsp; &nbsp; &nbsp; &nbsp; return any("phone", number);&nbsp; &nbsp; }&nbsp; &nbsp; public Value any(String key, String value) {&nbsp; &nbsp; &nbsp; &nbsp; Value v = new Value();&nbsp; &nbsp; &nbsp; &nbsp; v.put(Objects.requireNonNull(key), Objects.requireNonNull(value));&nbsp; &nbsp; &nbsp; &nbsp; return v;&nbsp; &nbsp; }}上面的代码打印:{"name":"Vika"}{"phone":"123-456-78-9"}{"val":"VAL!"}

千万里不及你

您可以将所有可能的名称作为变量,并对它们进行注释,以便在为 null 时忽略它们。这样,您只能在 JSON 中获取具有值的然后更改您的设置器以输入映射到您想要的键的变量。class Value {&nbsp; &nbsp; @JsonProperty("val")&nbsp; &nbsp; @JsonInclude(JsonInclude.Include.NON_NULL)&nbsp; &nbsp; private String val;&nbsp; &nbsp; @JsonProperty("new_key")&nbsp; &nbsp; @JsonInclude(JsonInclude.Include.NON_NULL)&nbsp; &nbsp; private String newKey;&nbsp; &nbsp; @JsonProperty("any_random_string")&nbsp; &nbsp; @JsonInclude(JsonInclude.Include.NON_NULL)&nbsp; &nbsp; private String anyRandomString;&nbsp; &nbsp; public void setVal(String s) {&nbsp; &nbsp; &nbsp; &nbsp; if(/* condition1 */)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; this.val = s;&nbsp; &nbsp; &nbsp; &nbsp; else if (/* condition2 */) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; this.newKey = s;&nbsp; &nbsp; &nbsp; &nbsp; } else&nbsp; if (/* condition3 */) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; this.anyRandomString = s;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }}

qq_花开花谢_0

好问题@Prasad,这个答案与 JAVA 或 SPRING BOOT 无关,我只是提出这个答案,因为我搜索过使用 node 来做到这一点,并希望这能以某种方式帮助某人。在 JAVASCRIPT 中,我们可以为 JSON 对象添加动态属性名称,如下所示var dogs = {};var dogName = 'rocky';dogs[dogName] = {&nbsp; &nbsp; age: 2,&nbsp; &nbsp; otherSomething: 'something'};dogName = 'lexy';dogs[dogName] = {&nbsp; &nbsp; age: 3,&nbsp; &nbsp; otherSomething: 'something'};console.log(dogs);但是当我们需要动态更改名称时,我们必须得到那个属性并创建另一个具有相同内容和新名称的属性并从 JSON 中删除旧属性将新属性分配给 JSON除此方法外,还有另一种动态更改 JSON 名称的方法,在此先感谢
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java