如何将Observable <string>转换为JSON

时间:2018-09-27 08:42:15

标签: java json object rx-java

我想将Observable对象转换为json对象

ObjectMapper mapper = new ObjectMapper();
    Observable<String> response = accountSearchService.searchAccount(paramMap, "", 0, 1);
    mapper.disable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES);
    AccountSearchResult resultDto = mapper.convertValue(response.subscribe().toString(), AccountSearchResult.class);

这是我要尝试的操作,但遇到OnErrorNotImplementedException。 请有人帮我。

1 个答案:

答案 0 :(得分:0)

subscribe()方法不会返回订阅的实际值,它只会触发对该可观察对象的订阅。相反,您必须“收听”订阅并在完成后进行操作。像这样:

ObjectMapper mapper = new ObjectMapper();
    Observable<String> response = accountSearchService.searchAccount(paramMap, "", 0, 1);
    mapper.disable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES);

    response.subscribe(stringValue -> {
            AccountSearchResult resultDto = mapper.convertValue(stringValue, AccountSearchResult.class);
        }, throwable -> {
            //onError
        });