Results 1 to 10 of 10
  1. #1
    anjibman is offline Member
    Join Date
    Dec 2010
    Posts
    52
    Rep Power
    0

    Default Convert JSON string to HashMap in Java

    How can I convert JSON string to HashMap. My JSON string is like

    {
    "AvailableDeliveries": {
    "500": {
    "code": "INOFFICE",
    "desc": "In Office",
    "id": 500,
    "name": "IN OFFICE"
    },
    "502": {
    "code": "TELEPHONE",
    "desc": "Telephone",
    "id": 502,
    "name": "TELEPHONE"
    },
    "503": {
    "code": "DIY DOC ACCESS",
    "desc": "Online Documents",
    "id": 503,
    "name": "DIY DOC ACCESS"
    }
    }
    }

    I looked on other examples which have collection of object but I have this extra top level object "AvailableDeliveries" not sure how to handle that level.

  2. #2
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    13

    Default Re: Convert JSON string to HashMap in Java

    You can do nested maps where the value of one map is another map.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  3. #3
    anjibman is offline Member
    Join Date
    Dec 2010
    Posts
    52
    Rep Power
    0

    Default Re: Convert JSON string to HashMap in Java

    I haven't done this before. Could you provide skeleton code?

  4. #4
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    13

    Default Re: Convert JSON string to HashMap in Java

    Here's is one way.

    Java Code:
    Map<String, Map<String, YourClass>> map = new HashMap<>();
    YourClass yc = map.get("AvailableDeliveries").get("503");
    String desc = yc.desc;  // assumes a field in YourClass
    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  5. #5
    anjibman is offline Member
    Join Date
    Dec 2010
    Posts
    52
    Rep Power
    0

    Default Re: Convert JSON string to HashMap in Java

    Here is this correct?

    Java Code:
    class YourClass {
       private Integer id;
       private String code;
       private String desc;
       private String name;
    }
    Don't I have to create class for AvailableDeliveries?

  6. #6
    anjibman is offline Member
    Join Date
    Dec 2010
    Posts
    52
    Rep Power
    0

    Default Re: Convert JSON string to HashMap in Java

    I am trying to read the JSON string like
    Java Code:
    Map<String, Map<String, DeliveriesType>> map = new HashMap<>();
    ObjectMapper mapper = new ObjectMapper();
    map = mapper.readValue(inputLine, new TypeReference<HashMap<String,String>>(){});
    but getting this exception
    Java Code:
    Can not deserialize instance of java.lang.String out of START_OBJECT token

  7. #7
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    13,541
    Rep Power
    26

    Default Re: Convert JSON string to HashMap in Java

    Java Code:
    new TypeReference<HashMap<String,String>>
    That type (the HashMap) needs to match your declared type you're trying to map to.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  8. #8
    anjibman is offline Member
    Join Date
    Dec 2010
    Posts
    52
    Rep Power
    0

    Default Re: Convert JSON string to HashMap in Java

    Thanks ... Yes that was error. Changed to
    Java Code:
    map = mapper.readValue(inputLine, 
    				  new TypeReference<Map<String, Map<String, MyClass>>>(){});
    and works fine.

  9. #9
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    13

    Default Re: Convert JSON string to HashMap in Java

    It appears I misunderstood your question. I thought you wanted to store the equivalent JSON string in some data structure.
    So I suggested a hashmap. I didn't know you wanted to convert it using some 3rd party conversion files. I never heard of
    TypeReference and had to look up online.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  10. #10
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    13,541
    Rep Power
    26

    Default Re: Convert JSON string to HashMap in Java

    I only know it because I have it right in front of me this week...:)

    ObjectMapper (and TypeReference) are part of Jackson FasterXML.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

Similar Threads

  1. convert xml to JSON using XPath dynamically
    By blertadhimitri in forum New To Java
    Replies: 2
    Last Post: 11-17-2013, 01:40 PM
  2. How to convert xml to json using JSONWriter?
    By blertadhimitri in forum New To Java
    Replies: 3
    Last Post: 11-08-2013, 02:13 PM
  3. could not convert the xml to json using java
    By cgk_js in forum Advanced Java
    Replies: 0
    Last Post: 11-30-2012, 09:31 AM
  4. convert html to json
    By nigina in forum New To Java
    Replies: 3
    Last Post: 09-22-2011, 09:38 AM
  5. hashmap to json
    By prakashkadakol in forum New To Java
    Replies: 5
    Last Post: 07-30-2011, 01:32 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •