0
json = [{"a":"555","b":"ee"},{"a":"556","b":"rr"}]

I tried:

ObjectMapper mapper = new ObjectMapper();
TypeReference<Map<String,String>> typeRef = new TypeReference<Map<String,String>>() {};
HashMap<String, String> result = mapper.readValue(json, typeRef);

but it's not working. I suppose that's the reason is that json is a list and not a single object.

In fact, if json was {"a":"555","b":"ee"}, it works.

Dave
  • 1,428
  • 4
  • 31
  • 49

1 Answers1

0

In order to solve the problem I'll use Jackson SDK. Please download the latest version from: http://wiki.fasterxml.com/JacksonDownload

JSON TO MAP EXAMPLE:

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

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

public class JsonMapExample {

    public static void main(String[] args) {

        try {

            ObjectMapper mapper = new ObjectMapper();
            String json = "{\"name\":\"mkyong\", \"age\":29}";

            Map<String, Object> map = new HashMap<String, Object>();

            // convert JSON string to Map
            map = mapper.readValue(json, new TypeReference<Map<String, String>>(){});

            System.out.println(map);

        } catch (JsonGenerationException e) {
            e.printStackTrace();
        } catch (JsonMappingException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

}

Map to JSON EXAMPLE:

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

import com.fasterxml.jackson.core.JsonGenerationException;
import com.fasterxml.jackson.databind.JsonMappingException;
import com.fasterxml.jackson.databind.ObjectMapper;

public class MapJsonExample{

    public static void main(String[] args) {

        try {

            ObjectMapper mapper = new ObjectMapper();
            String json = "";

            Map<String, Object> map = new HashMap<String, Object>();
            map.put("name", "mkyong");
            map.put("age", 29);

            // convert map to JSON string
            json = mapper.writeValueAsString(map);

            System.out.println(json);

            json = mapper.writerWithDefaultPrettyPrinter().writeValueAsString(map);

            // pretty print
            System.out.println(json);

        } catch (JsonGenerationException e) {
            e.printStackTrace();
        } catch (JsonMappingException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

}

JSONArray to HashMaps:

HashMap<String, String> pairs = new HashMap<String, String>();
for (int i = 0; i < myArray.length(); i++) {
   JSONObject j = myArray.optJSONObject(i);
   Iterator it = j.keys();
   while (it.hasNext()) {
      String n = it.next();
      pairs.put(n, j.getString(n));
   }
}
Avi Levin
  • 1,868
  • 23
  • 32
  • I already tried the solution proposed by mkyong. How I said, with `{"a":"555","b":"ee"}` works, but with a list like `[{"a":"555","b":"ee"},{"a":"556","b":"rr"}]` – Dave Feb 16 '16 at 14:00
  • @DavideFruci I added my answer, basically your format is a jsonArray so you have to parse it in a different way.. See JSONArray to HashMaps. On run time you can always check if the json is a JSONArray or JSONObject. let me know if it solve your problem. – Avi Levin Feb 16 '16 at 15:51
  • Avi Levinshtein, with myArray you mean `[{"a":"555","b":"ee"},{"a":"556","b":"rr"}]`? For me is a String `"[{"a":"555","b":"ee"},{"a":"556","b":"rr"}]"`. So I need to convert it from String to jsonArray before, right? – Dave Feb 16 '16 at 15:58
  • @DavideFruci, indid, you have to send a valid jsonArray object. check http://jsonlint.com/ to see if your json is a valid one. Also i'm not sure but check if Jackson can drop the "___" of your string and parse it alone to jsonarray. – Avi Levin Feb 16 '16 at 16:58