Home > Net >  How to convert a Response String into List and extract values?
How to convert a Response String into List and extract values?

Time:07-11

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

  1. First block which contains highest num, extract host
  2. 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]

CodePudding user response:

You can covert directly in list as shown below,

ObjectMapper mapper = new ObjectMapper();
String str = "[{\"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\"}]";
List<Prediction> predictions = mapper.readValue(str, new TypeReference<List<Prediction>>() {});
System.out.println(predictions.size());

Prediction class:

public class Prediction {
     @JsonProperty("num")
     BigDecimal num;
     @JsonProperty("host")
     String host;
}

All the code which I tried:

public class Test {
    public static void main(String[] args) throws IOException {
        ObjectMapper mapper = new ObjectMapper();
        String str = "[{\"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\"}]";
        List<Prediction> predictions = mapper.readValue(str, new TypeReference<List<Prediction>>() {});
        System.out.println(predictions.get(0).getNum() " : " predictions.get(0).getHost());
    }

    public static class Prediction {
        @JsonProperty("num")
        BigDecimal num;
        @JsonProperty("host")
        String host;

        public BigDecimal getNum() {
            return num;
        }
        public String getHost() {
            return host;
        }
    }
}

Output:

0.705251649 : a
  • Related