简体   繁体   中英

How to convert an Observable<String> to JSON

I want to convert an Observable object into a json object

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);

This is what I'm trying to do but getting OnErrorNotImplementedException. Please someone help me on this.

The subscribe() method doesn't return the actual value of the subscription, it just triggers a subscription to that observable. Instead, you have to "listen" to the subscription and do your operation once it's done. Something like this:

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
        });

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM