2
{
 "userType": "I",
 "classList": [
   {
     "id": 1,
     "name": "c1"
   },
   {
     "id": 2,
     "name": "c2"
   },
   {
     "id": 3,
     "name": "c3"
   }
 ]
}

How to declare and put values to the above sample example?

1
  • 2
    Please tell us what you want the map to look like. It's not clear to me what you're expecting. Commented Mar 20, 2017 at 3:16

3 Answers 3

2

Use jackson library,

HashMap<String,Object> map =
    new ObjectMapper().readValue(jsonString, HashMap.class);

Use this dependency for maven,

<dependency>
    <groupId>com.fasterxml.jackson.core</groupId>
    <artifactId>jackson-core</artifactId>
    <version>2.8.7</version>
</dependency>
Sign up to request clarification or add additional context in comments.

Comments

0

More here, do your researchs Create Map in Java.

Map<String,Object> map = new LinkedHashMap<String,Object>();
map.put("id",3);
map.put("name","c2");

Comments

0

You need to create a Map, and put a map and List into the first Map:

    public Map<String, Object> createMap() {
    Map<String, Object> map = new HashMap<String, Object>();
    map.put("userType", "I");
    List<Map<String, Object>> classList = new ArrayList<Map<String, Object>>();
    Map<String, Object> class1 = new HashMap<String, Object>();
    class1.put("id", 1);
    class1.put("name", "c1");
    classList.add(class1);

    Map<String, Object> class2 = new HashMap<String, Object>();
    class1.put("id", 2);
    class1.put("name", "c2");
    classList.add(class2);

    Map<String, Object> class3 = new HashMap<String, Object>();
    class1.put("id", 3);
    class1.put("name", "c3");
    classList.add(class3);

    map.put("classList", classList);
    return map;
}

Comments

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.