1

can somebody show code which deserialize json to TreeMap? Some simple example which includes example of json I'll show what I've tried First of all I am newbie so forgive me That's my json(probably i have mistakes even here):

{"Car":[{"mark":"AUDI_A3", "colour": "black"},
{"mark":"BMW_m3", "colour": "white"}]}

There is my code which does not work

public static void main(String[] args) {
        open();
    }
    private static void open(){
        try {
            BufferedReader buff = new BufferedReader(new FileReader("C:\Users\\t1.json"));
            String bf=null;
            String json= null;
            while((bf=buff.readLine())!=null){
                json+=bf;
            }
            Gson gson = new Gson();
            Type type = new TypeToken<TreeMap<String, SmallGuys>>(){}.getType();
            TreeMap<String, SmallGuys> Platoon = gson.fromJson(json, type);
            System.out.print(Platoon.keySet());
        }
        catch (Exception e){
            System.out.println("There is a mistake");
        }
    }
Geba
  • 5
  • 1
  • 7
  • have you tried this: http://stackoverflow.com/a/29965924/1754020 – Eddie Martinez Feb 23 '17 at 22:05
  • Have you looked at [Convert Json to Map](http://stackoverflow.com/questions/443499/convert-json-to-map)? – azurefrog Feb 23 '17 at 22:11
  • just tried, have not helped final Type Map = new TypeToken>() { }.getType(); Gson gson = new Gson(); JsonReader reader = new JsonReader(buff); TreeMap data = gson.fromJson(reader, Map); // contains the whole reviews list System.out.print(data.keySet()); – Geba Feb 23 '17 at 22:20
  • what about my json? maybe mistake is there& – Geba Feb 23 '17 at 22:24
  • Possible duplicate of [Convert Json to Map](http://stackoverflow.com/questions/443499/convert-json-to-map) – Tony Dong Feb 23 '17 at 22:31

1 Answers1

1

With Jackson, I put some methods:

public static Object convertJsonRequestToObject(HttpServletRequest pRequest, Class<?> pObject) throws JsonParseException, JsonMappingException, IOException {
    return new ObjectMapper().readValue(IOUtils.toString(pRequest.getInputStream(), StandardCharsets.UTF_8), pObject);
}

public static Map<String,Object> parseJsonToMap(String json) throws JsonParseException, JsonMappingException, IOException{
    return   new ObjectMapper().readValue(json, HashMap.class);
}

public static Map<String,Object> parseObjectToMap(Object object){
    return  (Map<String, Object>) new ObjectMapper().convertValue(object, Map.class);
}

//inverse
public static Object parseMapToObject(Map<?, ?> pMap, Class<?> pClass){
    return new ObjectMapper().convertValue(pMap, pClass);
}
Jason Glez
  • 1,254
  • 1
  • 16
  • 16
  • {"batchSize":10,"batchIndex":1,"inventoryInformation":{"inventoryName":"Test","userName":"jason.gonzalez","beginDate":"2017-02-16","endDate":"2017-02-23","category":"2"}} and the entity for this JSON public class InventoryInput { private int batchIndex; private int batchSize; private HashMap inventoryInformation; public InventoryInput(){ } //Set and Get } – Jason Glez Feb 23 '17 at 22:33
  • Also I can add object on the entity to convert, example: var user = { fullName: getCurrentSpotUser() } var location = { idLocation: spotInformation.locationId, user } var inventory = { idInventory: inventoryGeneralInformation.inventoryData.inventoryId } dataSend.inventoryInformation = { location, inventory }; – Jason Glez Feb 23 '17 at 22:36