JSON(其中包含很少的对象)在Java上treemap



某人可以显示对treemap的json的代码吗?一些简单的示例,包括JSON的示例我会展示我尝试的东西首先,我是新手,所以请原谅我那是我的JSON(也许我在这里有错误):

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

有我的代码不起作用

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");
        }
    }

与杰克逊一起,我放了一些方法:

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);
}

最新更新