Gson 转 json字符串为实体对象

gson 成员变量

private Gson gson = new GsonBuilder()
            .registerTypeAdapter(Date.class, (JsonDeserializer<Date>) (json, typeOfT, context) -> new Date(json.getAsJsonPrimitive().getAsLong()))
            .registerTypeAdapter(Date.class, (JsonSerializer<Date>) (date, type, jsonSerializationContext) -> new JsonPrimitive(date.getTime()))
            .create();

json串转实体类对象

//jsonStr 为json字符串
Pessoais pessoais = gson.fromJson(jsonStr, Pessoais.class);

Gson maven 依赖

<dependency>
    <groupId>com.google.code.gson</groupId>
    <artifactId>gson</artifactId>
</dependency>

你可能感兴趣的:(json,java)