1

My JSON string is like this:

{
    "100": {
        "mode": 100,
        "enabled": true,
        "value": "someString"
    },
    "101": {
        "mode": 101,
        "enabled": false,
        "value": "someString"
    }
}

I have a class actually

class Mode {

    @JsonProperty("mode")
    long mode;

    @JsonProperty("enabled")
    boolean enabled;

    @JsonProperty("value")
    String value;

}

I tried

objectMapper.readValue(jsonString, Map.class); 

But its generic map and also the numbers are converted to Integer types not Long. Using Mode in place of Map above throws exception.

  1. How to get Long in generic Map?
  2. And, how can I get a Map<String, Mode> out of the json string?

I have got jackson library in my projects maven.

1 Answer 1

2

your Key is String . It will work for you .

TypeReference<HashMap<String, Mode>> typeRef = new TypeReference<HashMap<String, Mode>>() {};
Map<String,Mode> map = objectMapper.readValue(jsonString,typeRef);

Updated :

import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

public class Mode {

  @JsonProperty("mode")
  long mode;

  @JsonProperty("enabled")
  boolean enabled;

  @JsonProperty("value")
  String value;

  @Override
  public String toString() {
    return "Mode{" +
        "mode=" + mode +
        ", enabled=" + enabled +
        ", value='" + value + '\'' +
        '}';
  }

  public static void main(String[] args) throws IOException {
    String json = "{\n"
        + "    \"100\": {\n"
        + "        \"mode\": 100,\n"
        + "        \"enabled\": true,\n"
        + "        \"value\": \"someString\"\n"
        + "    },\n"
        + "    \"101\": {\n"
        + "        \"mode\": 101,\n"
        + "        \"enabled\": false,\n"
        + "        \"value\": \"someString\"\n"
        + "    }\n"
        + "}";

    ObjectMapper objectMapper = new ObjectMapper();
    TypeReference<HashMap<String, Mode>> typeRef = new TypeReference<HashMap<String, Mode>>() {
    };
    Map<String, Mode> map = objectMapper.readValue(json, typeRef);
    map.entrySet().forEach(entry-> System.out.println(entry.getKey() + " : " +entry.getValue() ));
  }
}

Output :

100 : Mode{mode=100, enabled=true, value='someString'}
101 : Mode{mode=101, enabled=false, value='someString'}
Sign up to request clarification or add additional context in comments.

4 Comments

com.fasterxml.jackson.databind.exc.MismatchedInputException: Cannot construct instance of "com.xyz.Mode" (although at least one Creator exists): cannot deserialize from Object value (no delegate- or property-based Creator) at [Source: (String)"{"1001":{"mode":9223372036854775807,"enabled":true,"value":8}}"; line: 1, column: 10] (through reference chain: java.util.LinkedHashMap["1001"])
Thanks Khalid for looking. As I said, I am getting above exception.
@xploreraj see Updated code. It work perfectly fine. ans i also show the output after reading Json.
Thanks Khalid. I was creating the map and then using typeref, thats why I think I got the error. Your solutions works. As regards to Integer to Long, I figured out if number is out of range for int, its automatically converted into Long by jackson.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.