I am getting below Response from an API
"[{\"num\":0.705251649,\"host\":\"a\"},{\"num\":0.6223491728,\"host\":\"b\"},{\"num\":0.6486086175,\"host\":\"c\"},{\"num\":0.6595501527,\"host\":\"d\"},{\"num\":0.5766476765,\"host\":\"e\"},{\"num\":0.6029071212,\"host\":\"f\"}]";
The datatype is
java.lang.String
How to extract values efficiently like
- First block which contains highest num, extract host
- Last block which contains lowest num, extract host
I have written below code:
public class Myclass{
String resp = "[{\"num\":0.705251649,\"host\":\"a\"},{\"num\":0.6223491728,\"host\":\"b\"},{\"num\":0.6486086175,\"host\":\"c\"},{\"num\":0.6595501527,\"host\":\"d\"},{\"num\":0.5766476765,\"host\":\"e\"},{\"num\":0.6029071212,\"host\":\"f\"}]"
ObjectMapper objectMapper = new ObjectMapper();
Prediction[] langs = objectMapper.readValue(resp, Prediction[].class);
List<Prediction> langList = new ArrayList(Arrays.asList(langs));
}
class Prediction {
@JsonProperty("num")
BigDecimal num;
@JsonProperty("host")
String host;
}
I am getting below error:
com.fasterxml.jackson.databind.exc.MismatchedInputException: Cannot construct instance of `com.x.y.z.Prediction[]`: no String-argument constructor/factory method to deserialize from String value ('[{\"num\":0.705251649,\"host\":\"a\"},{\"num\":0.6223491728,\"host\":\"b\"},{\"num\":0.6486086175,\"host\":\"c\"},{\"num\":0.6595501527,\"host\":\"d\"},{\"num\":0.5766476765,\"host\":\"e\"},{\"num\":0.6029071212,\"host\":\"f\"}]')
at [Source: (String)""[{\"num\":0.705251649,\"host\":\"a\"},{\"num\":0.6223491728,\"host\":\"b\"},{\"num\":0.6486086175,\"host\":\"c\"},{\"num\":0.6595501527,\"host\":\"d\"},{\"num\":0.5766476765,\"host\":\"e\"},{\"num\":0.6029071212,\"host\":\"f\"}]; line: 1, column: 1]