user3306669
user3306669

Reputation: 117

How do I map to HashMap for array?

I have a JSON I want a to convert it to a HashMap. I have the following code -

ObjectMapper mapper = new ObjectMapper();
Map<String, String> jsonData = new HashMap<String, String>();

jsonData = mapper.readValue(userPropertyJson, new TypeReference<HashMap<String,String>>(){});

it is working fine if the input JSON is

{"user":1, "entity": "email"}

but fails when the JSON is as below -

{"user":1, "entity": ["email","fname","lname","phone"]}

How do I map to HashMap for array also?

Upvotes: 2

Views: 1381

Answers (4)

Maksim
Maksim

Reputation: 16931

Check out http://www.jsonschema2pojo.org/

It allows you to convert json to java object automatically. I use it when I need to create DTOs from a web service for which I don't have java mapping or SDK.

Upvotes: 0

fps
fps

Reputation: 34460

If you know in advance that your json will always have the same format (a String key mapped to a List<String>, either with a single element or with many elements), then you could use the ACCEPT_SINGLE_VALUE_AS_ARRAY deserialization feature:

ObjectMapper mapper = new ObjectMapper()
    .enable(DeserializationFeature.ACCEPT_SINGLE_VALUE_AS_ARRAY);

String jsonWithArray = 
    "{\"user\": 1, \"entity\": [\"email\", \"fname\", \"lname\", \"phone\"]}";

Map<String, List<String>> map1 = 
    mapper.readValue(
        jsonWithArray,
        new TypeReference<HashMap<String, List<String>>>() {});

System.out.println(map1); // {user=[1], entity=[email, fname, lname, phone]}

String jsonWithoutArray = "{\"user\": 1, \"entity\": \"email\"}";

Map<String, List<String>> map2 = 
    mapper.readValue(
        jsonWithoutArray, 
        new TypeReference<HashMap<String, List<String>>>() {});

System.out.println(map2); // {user=[1], entity=[email]}

This enables you to either have an array for the values in your json, or a single element.

Upvotes: 1

Syam S
Syam S

Reputation: 8499

Use Map<String, Object>. Example

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import com.fasterxml.jackson.core.JsonParseException;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.JsonMappingException;
import com.fasterxml.jackson.databind.ObjectMapper;


public class JacksonParser {

    public static void main(String[] args) {
        String userPropertyJson = "{\"user\":1, \"entity\": [\"email\",\"fname\",\"lname\",\"phone\"]}";
        ObjectMapper mapper = new ObjectMapper();
        try {
        Map<String, Object> jsonData = new HashMap<String, Object>();

        jsonData = mapper.readValue(userPropertyJson, new TypeReference<HashMap<String,Object>>(){});
        System.out.println(jsonData);
        } catch (JsonParseException e) {
        System.out.println(e.getMessage());
        } catch (JsonMappingException e) {
        System.out.println(e.getMessage());
        } catch (IOException e) {
        System.out.println(e.getMessage());
        }
    }

}

Upvotes: 2

Suresh Atta
Suresh Atta

Reputation: 121998

Declare a generic HashMap with String as a key and Object as a value , since you don't know the type of value exactly.

Map<String, Object>

And beware of assigning wrong types while retrieving data

Upvotes: 4

Related Questions