gpt4 book ai didi

java - HashMap toJson 为空 JSON

转载 作者:行者123 更新时间:2023-11-29 04:51:03 25 4
gpt4 key购买 nike

以下输出:

private static class Target extends HashMap<String, String> {
}

public static void main(String[] args) {
final Moshi build = new Moshi.Builder().build();
final JsonAdapter<Target> adapter = build.adapter(Target.class);
final Target value = new Target();
value.put("foo", "bar");
System.out.println(adapter.toJson(value));
}

{} - 我希望 {"foo":"bar"} - 我如何获得预期的输出?

最佳答案

创建 JsonAdapter 时使用 Map 类而不是 Target

private static class Target extends HashMap<String, String> {
}

public static void main(String[] args) {
final Moshi build = new Moshi.Builder().build();
final JsonAdapter<Map> adapter = build.adapter(Map.class);
final Target value = new Target();
value.put("foo", "bar");
System.out.println(adapter.toJson(value));
}

关于java - HashMap toJson 为空 JSON,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35402659/

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