[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));
java - How to map an object inside a string to set using ObjectMapper - Stack Overflow
String with JSON inside to HashMap (instead to objects)
Object Mapper (Swift 4) - Array of objects in parent object doesn’t load/partially fills data
Two things,
I’ve never used
mapping(map: map)within the init. That shouldn’t be necessary. Keep the body of the init method empty. Only return nil there if your JSON is invalid.You should go back to using
routineTasks <- map["_routineTasks"]instead ofroutineTasks = [RoutineTask(map: map)]. Make sure that key "_routineTasks" is correct.
Also, try setting a breakpoint in the mapping method and doing a po map.JSON to confirm what you’re expecting is actually there.
How to convert json string to pojo if json response starts with [] instead of {} ??
Consider using DeserializationFeature.ACCEPT_SINGLE_VALUE_AS_ARRAYS in your ObjectMapper instance. Then you must change the json field countryCode to countries or mark Set countries as json property countryCode using @JsonProperty and provide a constructor/factory method within String parameter. This will make your object mapper behave as per your requirements.
You have to define countryCode inside SkuMasterDTO class.
You can add a getter and build a set
Set<CountryDTO> getCountries(){
Set<CountryDTO> countries = new HashSet<>();
countries.add(new CountryDTO(countryCode));
return countries;
}
Or you can prepare set inside set method.