使用jackson库将领域模型转换为json对象时获取未知密钥.
这是我的领域模型实例.
public class RecordData extends RealmObject {
public RecordData() {
}
private FormData formData;
@Nullable
@JsonProperty("values")
private RealmList<Values> values;
@Nullable
@JsonProperty("value")
private String value;
}
将领域模型转换为json对象的代码.
JSONObject recordDataJsonObject = new JSONObject(new ObjectMapper().writeValueAsString(formData.getRecordData()));
解析json的输出:
{"loaded":true,"managed":false,"valid":true,"value":"fdfdf","values":[]}
领域版本:io.realm:realm-gradle-plugin:4.1.1
Rxjava版本:’io.reactivex.rxjava2:rxjava:2.1.6′
杰克逊版:com.fasterxml.jackson.core:jackson-databind:2.8.6
为什么我得到加载,托管和有效的布尔值?
解决方法:
从ObjectMapper写回复,你必须这样做:
public static String getJsonFromObject() {
ObjectMapper objectMapper = ObjectMapperHelper.getObjectMapperInstance();
String jsonString = null;
try {
jsonString = objectMapper.writeValueAsString(formData.getRecordData());
} catch (JsonProcessingException e) {
e.printStackTrace();
}
return jsonString != null ? jsonString : "";
}
并在JsonObject中获取您的响应:
try {
JSONObject jsonObject = new JSONObject().getJSONObject(getJsonFromObject());
} catch (JSONException e) {
e.printStackTrace();
}
快乐的编码!!