GSON 到 JSON 的转换 - 长格式和简单日期格式 "yyyy-MM-dd'T'HH:mm'Z'"
GSON to JSON conversion - long format and simple date format "yyyy-MM-dd'T'HH:mm'Z'"
我正在使用 GSON 将 JSONArray 转换为 Java 对象。我需要将长日期和简单日期格式 "yyyy-MM-dd'T'HH:mm'Z'" 转换为 Java 对象。我可以单独转换长日期或简单日期,但无法同时转换两者。
使用下面的代码片段进行长转换:
GsonBuilder builder = new GsonBuilder();
// Register an adapter to manage the date types as long values
builder.registerTypeAdapter(Date.class, new JsonDeserializer<Date>() {
public Date deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) {
return new Date(json.getAsJsonPrimitive()
.getAsLong());
}
});
Gson gson = builder.create();
Type userListType = new TypeToken<ArrayList<CurrentLocation>>() {
}.getType();
gson.fromJson(data, userListType);
我需要转换的示例数据:
[
{
"instance": {
"location": {
"lat": 31.522291,
"lon": -96.532816,
"timestamp": 1587693720000
},
"timeAtLocation": "2020-04-23T04:59Z"
}
},
{
"instance": {
"location": {
"lat": 31.522291,
"lon": -96.532816,
"timestamp": 1737693720000
},
"timeAtLocation": "2020-0-23T04:59Z"
}
}
]
请尝试以下代码:
public class Main {
static final String DATE_FORMAT = "yyyy-MM-dd'T'HH:mm'Z'";
static final Calendar calendar = Calendar.getInstance();
static class LongUtil {
static boolean isLong(String longValue) {
try {
Long.parseLong(longValue);
return true;
} catch (RuntimeException e) {
return false;
}
}
}
static class DateDeserializer implements JsonDeserializer<Date> {
final SimpleDateFormat simpleDateFormat = new SimpleDateFormat(DATE_FORMAT);
@Override
public Date deserialize(JsonElement jsonElement, Type type, JsonDeserializationContext jsonDeserializationContext) throws JsonParseException {
System.out.println(type.getTypeName());
System.out.println(jsonElement.getAsJsonPrimitive());
if (jsonElement.getAsJsonPrimitive() != null) {
final String expectedDateValue = jsonElement.getAsJsonPrimitive().getAsString();
if (LongUtil.isLong(expectedDateValue)) {
System.out.println("It is long value hence parsing it to long");
calendar.setTimeInMillis(Long.parseLong(expectedDateValue));
return calendar.getTime();
} else {
System.out.println("It is dateformat value hence parsing it to dateformat");
try {
return simpleDateFormat.parse(expectedDateValue);
} catch (ParseException e) {
throw new JsonParseException("Invalud dateFormat while parsing");
}
}
} else {
throw new JsonParseException("JSOn Premitive Exception null");
}
}
}
public static void main(String[] args) throws IOException {
GsonBuilder builder = new GsonBuilder();
builder.registerTypeAdapter(Date.class, new DateDeserializer());
Gson gson = builder.create();
Type userListType = new TypeToken<ArrayList<Wrapper>>() {
}.getType();
File file = new File("response.json");
System.out.println("file exists : " + file.exists());
JsonReader jsonReader = new JsonReader(new FileReader(file));
List<Wrapper> wrapper = gson.fromJson(jsonReader, userListType);
System.out.println(wrapper);
}
class Wrapper {
User instance;
@Override
public String toString() {
return "Wrapper{" +
"instance=" + instance +
'}';
}
}
class User {
Location location;
Date timeAtLocation;
@Override
public String toString() {
return "User{" +
"location=" + location +
", timeAtLocation='" + timeAtLocation + '\'' +
'}';
}
}
class Location {
String lat;
String lon;
Date timestamp;
@Override
public String toString() {
return "Location{" +
"lat='" + lat + '\'' +
", lon='" + lon + '\'' +
", timestamp='" + timestamp + '\'' +
'}';
}
}
}
解释:
虽然 deserializing
我正在检查输入字符串是在 dateformat
还是在 long
并基于它创建日历实例,然后我设置 long 并最终得到日期对象日期,否则只需使用字符串并根据日期格式化程序对其进行格式化。
输出:
file exists : true
It is long value hence parsing it to long
It is dateformat value hence parsing it to dateformat
It is long value hence parsing it to long
It is dateformat value hence parsing it to dateformat
[Wrapper{instance=User{location=Location{lat='31.522291', lon='-96.532816', timestamp='Fri May 01 13:49:08 IST 2020'}, timeAtLocation='Thu Apr 23 04:59:00 IST 2020'}}, Wrapper{instance=User{location=Location{lat='31.522291', lon='-96.532816', timestamp='Fri Jan 24 10:12:00 IST 2025'}, timeAtLocation='Mon Dec 23 04:59:00 IST 2019'}}]
我正在使用 GSON 将 JSONArray 转换为 Java 对象。我需要将长日期和简单日期格式 "yyyy-MM-dd'T'HH:mm'Z'" 转换为 Java 对象。我可以单独转换长日期或简单日期,但无法同时转换两者。
使用下面的代码片段进行长转换:
GsonBuilder builder = new GsonBuilder();
// Register an adapter to manage the date types as long values
builder.registerTypeAdapter(Date.class, new JsonDeserializer<Date>() {
public Date deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) {
return new Date(json.getAsJsonPrimitive()
.getAsLong());
}
});
Gson gson = builder.create();
Type userListType = new TypeToken<ArrayList<CurrentLocation>>() {
}.getType();
gson.fromJson(data, userListType);
我需要转换的示例数据:
[
{
"instance": {
"location": {
"lat": 31.522291,
"lon": -96.532816,
"timestamp": 1587693720000
},
"timeAtLocation": "2020-04-23T04:59Z"
}
},
{
"instance": {
"location": {
"lat": 31.522291,
"lon": -96.532816,
"timestamp": 1737693720000
},
"timeAtLocation": "2020-0-23T04:59Z"
}
}
]
请尝试以下代码:
public class Main {
static final String DATE_FORMAT = "yyyy-MM-dd'T'HH:mm'Z'";
static final Calendar calendar = Calendar.getInstance();
static class LongUtil {
static boolean isLong(String longValue) {
try {
Long.parseLong(longValue);
return true;
} catch (RuntimeException e) {
return false;
}
}
}
static class DateDeserializer implements JsonDeserializer<Date> {
final SimpleDateFormat simpleDateFormat = new SimpleDateFormat(DATE_FORMAT);
@Override
public Date deserialize(JsonElement jsonElement, Type type, JsonDeserializationContext jsonDeserializationContext) throws JsonParseException {
System.out.println(type.getTypeName());
System.out.println(jsonElement.getAsJsonPrimitive());
if (jsonElement.getAsJsonPrimitive() != null) {
final String expectedDateValue = jsonElement.getAsJsonPrimitive().getAsString();
if (LongUtil.isLong(expectedDateValue)) {
System.out.println("It is long value hence parsing it to long");
calendar.setTimeInMillis(Long.parseLong(expectedDateValue));
return calendar.getTime();
} else {
System.out.println("It is dateformat value hence parsing it to dateformat");
try {
return simpleDateFormat.parse(expectedDateValue);
} catch (ParseException e) {
throw new JsonParseException("Invalud dateFormat while parsing");
}
}
} else {
throw new JsonParseException("JSOn Premitive Exception null");
}
}
}
public static void main(String[] args) throws IOException {
GsonBuilder builder = new GsonBuilder();
builder.registerTypeAdapter(Date.class, new DateDeserializer());
Gson gson = builder.create();
Type userListType = new TypeToken<ArrayList<Wrapper>>() {
}.getType();
File file = new File("response.json");
System.out.println("file exists : " + file.exists());
JsonReader jsonReader = new JsonReader(new FileReader(file));
List<Wrapper> wrapper = gson.fromJson(jsonReader, userListType);
System.out.println(wrapper);
}
class Wrapper {
User instance;
@Override
public String toString() {
return "Wrapper{" +
"instance=" + instance +
'}';
}
}
class User {
Location location;
Date timeAtLocation;
@Override
public String toString() {
return "User{" +
"location=" + location +
", timeAtLocation='" + timeAtLocation + '\'' +
'}';
}
}
class Location {
String lat;
String lon;
Date timestamp;
@Override
public String toString() {
return "Location{" +
"lat='" + lat + '\'' +
", lon='" + lon + '\'' +
", timestamp='" + timestamp + '\'' +
'}';
}
}
}
解释:
虽然 deserializing
我正在检查输入字符串是在 dateformat
还是在 long
并基于它创建日历实例,然后我设置 long 并最终得到日期对象日期,否则只需使用字符串并根据日期格式化程序对其进行格式化。
输出:
file exists : true
It is long value hence parsing it to long
It is dateformat value hence parsing it to dateformat
It is long value hence parsing it to long
It is dateformat value hence parsing it to dateformat
[Wrapper{instance=User{location=Location{lat='31.522291', lon='-96.532816', timestamp='Fri May 01 13:49:08 IST 2020'}, timeAtLocation='Thu Apr 23 04:59:00 IST 2020'}}, Wrapper{instance=User{location=Location{lat='31.522291', lon='-96.532816', timestamp='Fri Jan 24 10:12:00 IST 2025'}, timeAtLocation='Mon Dec 23 04:59:00 IST 2019'}}]