Exception: "org.codehaus.jackson.map.JsonMappingException: unable to deserialize java.lang.String instance from START_OBJECT token"

I am trying to test a simple JSON structure using Jackson, where I store the JSON request as a string in the dataJson variable.

public void unmarshal(InputStream is) throws Exception {
// this will contain my actual json string
  String dataJson= StUtil.toString(is);
  System.out.println(dataJson);

  //parse json string    
  String response = objectMapper.readValue(dataJson, String.class);
  System.out.println(response);
}

SUtil.toString (InputStream) Method:

 public static String toString(final InputStream is) {
    final BufferedReader br = new BufferedReader(new InputStreamReader(is));
    final StringBuffer buffer = new StringBuffer();
    try {
       for (String line; (line = br.readLine()) != null; ) {
          buffer.append(line);
           }
        } catch (IOException ioe) {
        }
      return buffer.toString();
   }

I am studying the check part with Jackson, but it throws an error / exception in the line

String response = objectMapper.readValue(dataJson, String.class);

And below is the exception that I get -

Exception: "org.codehaus.jackson.map.JsonMappingException: Can not deserialize instance of java.lang.String out of START_OBJECT token"

I wanted to know what I was doing wrong. Any help on this would be appreciated.

JSON request:

{"username" : "my_username","password" : "my_password","validation-factors":{"validationFactors":[{"name":"remote_address","value":"127.0.0.1"}]}}
+4
source share
2 answers

JSON Java String ( ), JSON Java String, readValue.

JSON, -

objectMapper.readTree(dataJson);

. , .

+9

ObjectMapper ,

 objectMapper.enable(DeserializationFeature.ACCEPT_SINGLE_VALUE_AS_ARRAY);
+2

All Articles