gpt4 book ai didi

android - 反序列化具有键变量 jackson map ObjectMapper 的 JSON

转载 作者:行者123 更新时间:2023-11-30 00:38:00 24 4
gpt4 key购买 nike

JSON:

  {
"id": "1704",
"title": "Choice of Drink",
"multiselect": 0,
"maximum_selection": 1,
"ac_items": 1,
"Choice of Drink": [{
"id": "8151",
"name": "Lemon Ice Tea",
"price": 0,
"orig_price": 0
}, {
"id": "8152",
"name": "Fresh Lime",
"price": 0,
"orig_price": 0
}]
}

问题是键“Choice of Drink”是一个变量。当我没有名称时,如何将其放入@JsonProperty 中?

最佳答案

您可以使用 Jackson 的 @JsonAnySetter 注释将所有变量键指向一个方法,然后您可以根据需要分配/处理它们:

public class Bar
{
// known/fixed properties
public String id;
public String title;
public int multiselect;
public int maximum_selection;
public int ac_items;

// unknown/variable properties will go here
@JsonAnySetter
public void setDrinks(String key, Object value)
{
System.out.println("variable key = '" + key + "'");
System.out.println("value is of type = " + value.getClass());
System.out.println("value toString = '" + value.toString() + "'");
}
}

在示例输入的情况下,输出为:

variable key = 'Choice of Drink'
value is of type = class java.util.ArrayList
value toString = '[{id=8151, name=Lemon Ice Tea, price=0, orig_price=0}, {id=8152, name=Fresh Lime, price=0, orig_price=0}]'

关于android - 反序列化具有键变量 jackson map ObjectMapper 的 JSON,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43092693/

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