以下输出:
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"}
- 如何获得预期的输出?
答案 0 :(得分:3)
创建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));
}