Java Json麻烦读取hashmap

时间:2015-09-12 18:22:58

标签: java json

我试图读取这个json文件:

{
  "username": "someusername",
  "password": "12345678",
  "ipAddresses": {
    "2015-09-12": "127.0.0.1"
  }
}

使用此类存储信息:

private final class SavedPlayer {
    private final String username;
    private final String password;
    private final HashMap<LocalDate, String> ipAddresses;

    private SavedPlayer(
            String username, String password,
            HashMap<LocalDate, String> ipAddresses
    ) {
        this.username = username;
        this.password = password;
        this.ipAddresses = ipAddresses;
    }
}

这部分代码抛出异常:

private static final Gson GSON = new GsonBuilder().setPrettyPrinting().create();
SavedPlayer savedPlayer = GSON.fromJson(reader, SavedPlayer.class);

这是抛出的异常:

com.google.gson.JsonSyntaxException: java.lang.IllegalStateException: Expected BEGIN_OBJECT but was STRING at line 5 column 17

如何正确读取存储的HashMap?

编辑:当我使用<String, String>代替<LocalDate, String> 时,它可以正常工作

3 个答案:

答案 0 :(得分:1)

要将JSON绑定到LocalDate,您必须通过实现JsonDeserializer并使用方法registerTypeAdapter()向GSON注册来编写自定义序列化程序/解串器,或者您可以使用现有库:{{3 }}

答案 1 :(得分:1)

Gson允许您注册自己的自定义序列化程序和反序列化程序。这是通过定义两部分来完成的:

Json Serialiers:需要为对象定义自定义序列化 Json反序列化器:需要为类型定义自定义反序列化 实例创建者:如果no-args构造函数可用或注册了反序列化器,则不需要

GsonBuilder gson = new GsonBuilder();
gson.registerTypeAdapter(LocalDate.class, new MyDeserializer());

registerTypeAdapter调用检查类型适配器是否实现了多个接口并为所有接口注册。

了解更多信息,请查看gson-user-guide

以下是如何为LocalDate

编写自定义反序列化的示例
public class LocalDateJsonDeserializer implements JsonDeserializer<LocalDate> {    
      public LocalDate deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context)
          throws JsonParseException {
        return new LocalDate(json.getAsJsonPrimitive().getAsString());
      }
}

并使用此行创建GSON。

final static Gson GSON = new GsonBuilder().registerTypeAdapter(LocalDate.class, new LocalDateJsonDeserializer()).setPrettyPrinting().create();

答案 2 :(得分:0)

修改:您可以找到GSON deserializing key-value to custom object

我尝试了使用Gson 2.4库粘贴的代码。我还将LocalDate转换为String。它工作得非常好。

public class SavedPlayer {
  private final String username;
  private final String password;
  private final HashMap<String, String> ipAddresses;

  private SavedPlayer(
      String username, String password,
      HashMap<String, String> ipAddresses
  ) {
    this.username = username;
    this.password = password;
    this.ipAddresses = ipAddresses;
  }

  public static void main(String[] args) throws FileNotFoundException {
    final Gson GSON = new GsonBuilder().setPrettyPrinting().create();
    SavedPlayer savedPlayer = GSON.fromJson("{\n" +
        "  \"username\": \"someusername\",\n" +
        "  \"password\": \"12345678\",\n" +
        "  \"ipAddresses\": {\n" +
        "    \"2015-09-12\": \"127.0.0.1\"\n" +
        "  }\n" +
        "}", SavedPlayer.class);
    System.out.println(savedPlayer.username);
    System.out.println(savedPlayer.password);
    for (Map.Entry entry : savedPlayer.ipAddresses.entrySet()) {
      System.out.println("Key: " + entry.getKey() + "\tValue: " + entry.getValue());
    }
  }
}

<强>输出:
someusername
12345678个
关键:2015-09-12价值:127.0.0.1