android - LoganSquare 在生成的 Json 中不包含字段名称

标签 android logan-square

我有一个类 DbRecord

public class DbRecord {


    @JsonField(name="on_duties")
    List<OnDutyElement> onDuties;

    @JsonField(name = "date_time")
    DateTime dateTime;

当我尝试序列化此类的对象时,LoganSquare 不包含 dateTime 字段。

生成的 JSON:{:"2015-12-21T11:32:17.503-05:00","on_duties":[{...从这里一切正常

最佳答案

如果所有 JSON 字段名称都是小写且带有下划线,您可以这样定义 fieldNamingPolicy。

@JsonObject(fieldNamingPolicy = JsonObject.FieldNamingPolicy.LOWER_CASE_WITH_UNDERSCORES)
public class DbRecord {


@JsonField
List<OnDutyElement> onDuties;

@JsonField
DateTime dateTime;

如果您还没有为 DateTime 设置 TypeConverter,LoganSquare 现在允许您像那样在 JsonField 注释中定义 TypeConverter

 @JsonField(typeConverter = DateTimeConverter.class)
DateTime time;

和日期时间转换器

public class DateTimeConverter implements TypeConverter<DateTime> {
@Override
public DateTime parse(JsonParser jsonParser) throws IOException {
    String dateString = jsonParser.getValueAsString(null);
    try {
        DateTime dateTime = new DateTime(dateString);
        return dateTime.changeTimeZone(TimeZone.getTimeZone("UTC"), TimeZone.getDefault());
    } catch (RuntimeException runtimeException) {
        runtimeException.printStackTrace();
        return null;
    }
}

@Override
public void serialize(DateTime object, String fieldName, boolean writeFieldNameForObject, JsonGenerator jsonGenerator) throws IOException {
    jsonGenerator.writeStringField(fieldName, object.format("YYYY-MM-DDThh:mm:ss"));
}

关于android - LoganSquare 在生成的 Json 中不包含字段名称,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34400383/

相关文章:

android - 键盘未显示在焦点文本输入上 - Android

java - 如何删除Android状态栏自动颜色变化

java - 如何不混淆带有列表参数的方法

android - 我们如何在 Android N Developer Preview 上尝试自由格式多窗口模式?

android - 通知 channel 错误

android - Retrofit 2 调用失败 - 抽象方法 "void okhttp3.Callback.onResponse"

android - 我应该如何使用 WiFi Direct 将文件共享到另一台 Android 设备? - 致敬和洛根广场