我可能会@JsonAnySetter
按照以下方式使用 。
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import com.fasterxml.jackson.annotation.JsonAnySetter;
import com.fasterxml.jackson.databind.ObjectMapper;
public class App
{
public static void main(String[] args) throws Exception
{
/* {"menu":{"a":"1", "b":"2"},"menu":{"c":"1", "d":"2"},"menu":{"e":"1", "f":"2"}} */
String json = "{\"menu\":{\"a\":\"1\", \"b\":\"2\"},\"menu\":{\"c\":\"1\", \"d\":\"2\"},\"menu\":{\"e\":\"1\", \"f\":\"2\"}}";
ObjectMapper mapper = new ObjectMapper();
Foo foo = mapper.readValue(json, Foo.class);
System.out.println(foo.menus);
}
}
class Foo
{
List<Menu> menus = new ArrayList<>();
@JsonAnySetter
public void addMenu(String key, Menu menu)
{
menus.add(menu);
}
}
class Menu
{
Map<String, Object> items = new HashMap<> ();
@JsonAnySetter
public void addItems(String itemName, String itemValue)
{
items.put(itemName, itemValue);
}
@Override
public String toString()
{
return String.format("%s", items);
}
}