How to convert Object type to HashMap

I’m trying to parse JSON my own way and I’m stuck on converting List to Map:

   public static void main ( final String[] args ) throws Exception
   {
      {
         String test = new String("[{"label": 1, "value": 12345}, {"label": 2, "value": 12}]");
         final StringReader stringReader = new StringReader(test);
         final Object obj = new JSONReader( stringReader ).parseData();
         List list = (List)obj;  // [{label=1, value=12345}, {label=2, value=12}] looks good
         System.out.println(list);
         for (Object o : list) {
             System.out.println(o);  // {label=1, value=12345} {label=2, value=12} also looks ok
                     if (!(o instanceof Map)) {
            throw new IllegalArgumentException("Root object must be a JSON object.");
        }
         }
      }

The following code doesn’t throw exception thus the o is instance of Map. However I can’t use the o.get("label") to retrieve the label value. How can I get every value from label and value field?

Answer

Actually it works with objectMapper

@Test
void t2() throws IOException {
    String test = new String("[{"label": 1, "value": 12345}, {"label": 2, "value": 12}]");
    final StringReader stringReader = new StringReader(test);
    final Object obj = new ObjectMapper().readValue(stringReader, Object.class);
    List list = (List)obj;  // [{label=1, value=12345}, {label=2, value=12}] looks good
    System.out.println(list);
    for (Object o : list) {
        System.out.println(o);  // {label=1, value=12345} {label=2, value=12} also looks ok
        if (!(o instanceof Map)) {
            throw new IllegalArgumentException("Root object must be a JSON object.");
        }
        System.out.println(((Map)o).get("label"));
    }
}

The output is;

[{label=1, value=12345}, {label=2, value=12}]
{label=1, value=12345}
1
{label=2, value=12}
2

This may be due to the JsonReader that you are using. I cannot figure out the parser with parseData method…