[Update Sept 2020] Although my original answer here, from many years ago, seems to be helpful and is still getting upvotes, I now use the GSON library from Google, which I find to be more intuitive.
I've got the following code:
public void testJackson() throws IOException {
ObjectMapper mapper = new ObjectMapper();
File from = new File("albumnList.txt");
TypeReference<HashMap<String,Object>> typeRef
= new TypeReference<HashMap<String,Object>>() {};
HashMap<String,Object> o = mapper.readValue(from, typeRef);
System.out.println("Got " + o);
}
It's reading from a file, but mapper.readValue() will also accept an InputStream and you can obtain an InputStream from a string by using the following:
new ByteArrayInputStream(astring.getBytes("UTF-8"));
There's a bit more explanation about the mapper on my blog.
Answer from djna on Stack Overflow[Update Sept 2020] Although my original answer here, from many years ago, seems to be helpful and is still getting upvotes, I now use the GSON library from Google, which I find to be more intuitive.
I've got the following code:
public void testJackson() throws IOException {
ObjectMapper mapper = new ObjectMapper();
File from = new File("albumnList.txt");
TypeReference<HashMap<String,Object>> typeRef
= new TypeReference<HashMap<String,Object>>() {};
HashMap<String,Object> o = mapper.readValue(from, typeRef);
System.out.println("Got " + o);
}
It's reading from a file, but mapper.readValue() will also accept an InputStream and you can obtain an InputStream from a string by using the following:
new ByteArrayInputStream(astring.getBytes("UTF-8"));
There's a bit more explanation about the mapper on my blog.
Try TypeFactory. Here's the code for Jackson JSON (2.8.4).
Map<String, String> result;
ObjectMapper mapper;
TypeFactory factory;
MapType type;
factory = TypeFactory.defaultInstance();
type = factory.constructMapType(HashMap.class, String.class, String.class);
mapper = new ObjectMapper();
result = mapper.readValue(data, type);
Here's the code for an older version of Jackson JSON.
Map<String, String> result = new ObjectMapper().readValue(
data, TypeFactory.mapType(HashMap.class, String.class, String.class));
Parse properties to Map<String, Object>
java - Deserialize to a Map<String, Object> with correct type for each value - Stack Overflow
Deserializing into Map<String, Object> based on a map-global type property
Jackson mapping of JSON to Java Map<String, Map<String, String> not working correctly with annotations
Hi,
As the title states, I am trying to map the following JSON data
{ "value1" : { "key1":"value1", "key2":"value2"}}The class to map this is has a field
@JsonProperty private Map<String, Map<String, String>> values;
Jackson says unknown property value1, well I want value1 to be the key to the map and the rest of the object to populate the value Map<String, String>. There could be several key -> Map<String, String> lines in the Json object. Also the key value1 is based on what was searched for, for example if I search for 100 I will get
{"100": {"key1":"value1", "key2":"value2"}}it also needs to support multiple response lines e.g
{"100" : {"key1":"value1" , "key2" : "value2" },
"101" : {"key1":"value1", "key2" : "value2"}}thanks