0

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]
Mark Rotteveel
  • 100,966
  • 191
  • 140
  • 197
LOrD_ARaGOrN
  • 3,884
  • 3
  • 27
  • 49

1 Answers1

0

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
pratap
  • 538
  • 1
  • 5
  • 11
  • I am getting below exception Exceptioncom.fasterxml.jackson.databind.exc.MismatchedInputException: Cannot deserialize value of type `java.util.ArrayList` from String value (token `JsonToken.VALUE_STRING`) – LOrD_ARaGOrN Jul 11 '22 at 05:30
  • does your json string start with '['? if not, it will treat as single object and wont be converted in to arraylist. – pratap Jul 11 '22 at 05:40
  • yes it is starting from '['. – LOrD_ARaGOrN Jul 11 '22 at 05:45
  • can you paste your code here? Thats strange if you are getting this error – pratap Jul 11 '22 at 05:47
  • modified my code in question. please notice Prediction class is in the same java file Myclass – LOrD_ARaGOrN Jul 11 '22 at 05:52
  • have added all the working code here; hope this helps – pratap Jul 11 '22 at 06:19
  • looks like some issue with my jsonString. I observed extra double quote in starting and ending. but when I am removing them than getting below error. Unexpected character ('\' (code 92)): was expecting double-quote to start field name – LOrD_ARaGOrN Jul 11 '22 at 07:08