我使用ObjectMapper将JSON对象解析为Realm.
我的班级旅行看起来像这样:
class Trip: Object, Mappable {
dynamic var Id : String? = nil
dynamic var CreatedOn : String? = nil
dynamic var LastModified : String? = nil
required convenience init?(_ map: Map) {
self.init()
}
func mapping(map: Map) {
Id <- map["Id"];
CreatedOn <- map["CreatedOn"];
LastModified <- map["LastModified"];
}
}
Run Code Online (Sandbox Code Playgroud)
我正在使用Alamofire调用Web服务请求:
Alamofire.request(.GET, path, headers: ["Token" : auth_token]).responseJSON { response in
let dict : NSDictionary? = response.result.value as? NSDictionary
let test = Mapper<Trip>().map(dict!)
let realm = try! Realm()
realm.beginWrite()
realm.add(test!) …Run Code Online (Sandbox Code Playgroud) 我正在使用AlamofireObjectMapper,每当响应包含任何空值时,它都会出错,
"FAILURE:Error Domain = com.alamofireobjectmapper.error Code = 2"ObjectMapper无法序列化响应."UserInfo = {NSLocalizedFailureReason = ObjectMapper无法序列化响应.}"
这就是我的要求
let URL = "https://demo6336282.mockable.io/myapi"
Alamofire.request(URL).validate().responseObject { (response: DataResponse<WeatherResponse>) in
let weatherResponse = response.result.value
print(weatherResponse?.location)
if let threeDayForecast = weatherResponse?.threeDayForecast {
for forecast in threeDayForecast {
print(forecast.day)
print(forecast.temperature)
}
}
}
Run Code Online (Sandbox Code Playgroud)
这是我的DataModel类
import Foundation
import ObjectMapper
import AlamofireObjectMapper
class WeatherResponse: Mappable {
var location: String? = ""
var threeDayForecast: [Forecast]? = []
required init?(map: Map){
}
func mapping(map: Map) {
location <- map["location"]
threeDayForecast <- map["three_day_forecast"] …Run Code Online (Sandbox Code Playgroud) 我正在使用ObjectMapper(https://github.com/Hearst-DD/ObjectMapper)将我的JSON映射到Swift对象.
假设我有这个JSON结构:
{
animals: [
{
"type": "Cat",
"weight": 23,
"catchMice": true
},
{
"type": "Fish",
"weight": 1,
"swim": true
}
]
}
Run Code Online (Sandbox Code Playgroud)
我有以下Swift对象:
class Foo: Mappable {
var animals: [Animal] = []
func mapping(map: Map) {
animals <- map["animals"] //But I want to be able to distinguish between Cat and Fish objects here
}
}
class Animal: Mappable {
var type: String?
var weight: Double?
required init?(map: Map) {}
func mapping(map: Map) {
type <- map["type"]
weight …Run Code Online (Sandbox Code Playgroud) 我在 iOS 应用程序中使用 Moya 和 RxSwift 进行网络连接,我希望能够在我的Observers 调用onError.
API 始终以以下 JSON 格式返回错误响应:
{
"error": {
"message": "Error message goes here",
"code": "String error code"
}
}
Run Code Online (Sandbox Code Playgroud)
目标是实现类似于以下代码片段的内容,其中传入的错误onError是我的自定义错误类型而不是Moya.Error类型:
observable
.subscribe(
onNext: { response in
// Do typical onNext stuff
},
onError: { error in
// Get and consume my custom error type here:
let customError = error as! MyCustomErrorModel
print("My API's error message: \(customError.error?.message)")
print("My API's error code: \(customError.error?.code)")
})
Run Code Online (Sandbox Code Playgroud)
我能够使用自定义PluginType(粘贴在下面,来自 …
我正在 Java 中使用 Jackson 2.4 来做一些 JSON 跑腿工作。我使用 Apache HttpGet 调用远程服务器,使用 Jackson 将结果反序列化为 POJO,操作这些结果,然后使用 Jackson 对其进行序列化,以使用 HttpPost 推送回远程服务器。
\n\n我发现的问题是 Jackson 正在将 unicode 文字转换为 unicode 字符,由于两端的编码问题,我不需要它这样做。例如,我可能在 JSON 中包含以下内容:
\n\n"field1": "\\u00a2"\nRun Code Online (Sandbox Code Playgroud)\n\n但是 Jackson 在反序列化时将“\\u00a2”转换为“\xc2\xa2”,这会导致远程服务器出现问题。它必须被维护为转义的 unicode。如果我使用 Apache EntityUtils(指定 UTF-8)之类的东西,或者甚至从 Web 浏览器进行调用来获取数据,则转义的 unicode 会被保留,因此我知道它是从服务器正确传入的。如果我让 Jackson 在响应上使用来自实体的输入流,它会自动进行转换。
\n\n我尝试使用显式设置为 UTF-8 的 JsonGenerator 来写入 HttpPost。它不起作用,远程服务器仍然拒绝它。我已经研究了 ObjectMapper 和 JsonParser 的配置选项,但我没有看到任何可以覆盖此行为的内容。当然,转义非 ASCII 码,但这不是我需要做的。也许我遗漏了一些明显的东西,但我无法让 Jackson 反序列化这个字符串而不替换转义的 unicode。
\n\n编辑:好吧,我的错,唯一有问题的文字有 3 或 5 个前导斜杠,而不仅仅是一个。这有点奇怪,但 Java 似乎是在反序列化过程中默认解压它的东西,即使从服务器返回的原始文本保留了它。仍然不确定如何让 Java 在不检查大量文本的情况下保留它。
\n我定义一个JavaTimeModule:
@Bean public Module java8TimeModule() {
JavaTimeModule javaTimeModule = new JavaTimeModule();
return javaTimeModule;
}
Run Code Online (Sandbox Code Playgroud)
但是,它不会向默认对象映射器注册自身。我正在使用 jackson-databind-2.8.9.jar。
Eclipse IDE的在线帮助JavaTimeModule()说:
请注意,从 2.6 开始,该模块不支持自动注册。
不确定 jackson-databind-2.8.9 是否仍然是同样的情况,导致JavaTimeModule未注册默认对象映射器。
我不想创建新的对象映射器。与过去一样,当我尝试此操作时,我创建的对象映射器遇到了另一个问题。
Springboot 文档说:
定义 Jackson2ObjectMapperBuilder 类型的 @Bean 将允许您自定义默认的 ObjectMapper 和 XmlMapper。
但我找不到代码示例,也不知道该怎么做。
如果我定义了一个@Beanof 类型Jackson2ObjectMapperBuilder,它会取代默认的类型吗?其后果是什么?以及如何从中获取默认的对象映射器Jackson2ObjectMapperBuilder?
下面提到的是 JSON 字符串,resultString:
{
"imageMaps": [{
"crc": "c2c4",
"flags": "0",
"length": "117384",
"index": 1,
"version": "1.1.90ea",
"status": ""
}, {
"crc": "7601",
"flags": "8",
"length": "117592",
"index": 2,
"version": "1.1.90ed",
"status": ""
}],
"complete": true,
"nextBootImageVersion": "",
"lastKnownGoodImageVersion": "1.1.90ed",
"runningImageVersion": "1.1.90ed"
}
Run Code Online (Sandbox Code Playgroud)
我想将相同的转换为 A 类的对象:
public class A {
private boolean complete;
private String message;
private String lastKnownGoodImageVersion;
private String nextBootImageVersion;
private String runningImageVersion;
private Map<String, B> imageMaps;
private List<B> images;
private MacID macId;
}
Run Code Online (Sandbox Code Playgroud)
我正在尝试使用以下代码将 json 转换为 A 类的对象: …
如何阅读下面JSON使用Jackson ObjectMapper?我已经开发了代码,但出现以下错误。
com.fasterxml.jackson.databind.exc.MismatchedInputException: Cannot deserialize instance of `java.util.ArrayList` out of START_OBJECT token
at [Source: (File); line: 7, column: 19] (through reference chain: com.example.demo.resources.Orgnization["secondaryIds"])
Run Code Online (Sandbox Code Playgroud)
JSON
{
"id": "100000",
"name": "ABC",
"keyAccount": false,
"phone": "1111111",
"phoneExtn": "11",
"secondaryIds": {
"ROP": [
"700010015",
"454546767",
"747485968",
"343434343"
],
"AHID": [
"01122006",
"03112001"
]
}
}
Run Code Online (Sandbox Code Playgroud) 我的代码中有这个:
ObjectMapper oMapper = new ObjectMapper();
Map<String, Object> objectMap = oMapper.convertValue(test, Map.class);
Run Code Online (Sandbox Code Playgroud)
我收到以下警告:
Type safety: The expression of type Map needs unchecked conversion to conform to Map<String,Object>
Run Code Online (Sandbox Code Playgroud)
我该如何解决这个问题?
解决方案:
import com.fasterxml.jackson.core.type.TypeReference;
ObjectMapper oMapper = new ObjectMapper();
Map<String, Object> objectMap = oMapper.convertValue(prueba, new TypeReference<Map<String, Object>>(){});
Run Code Online (Sandbox Code Playgroud) 我有一个 JSON:
{
"stringField" : 1234,
"booleanField": true,
"numberField": 1200.00
}
Run Code Online (Sandbox Code Playgroud)
我使用对象映射器将 json 反序列化为:-
@Data
class SomeClass {
String stringField;
boolean booleanField;
float numberField;
}
Run Code Online (Sandbox Code Playgroud)
我希望 objectMapper 抛出错误,因为根据 json 规范,字符串字段的值必须双引号。我怎样才能让 objectMapper 抛出错误?