zoukankan      html  css  js  c++  java
  • 很实用的数据类型转换以及读取文件中的数据

    /*
    	 * 001.json转换成对象
    	 * 
    	 * @param:传入对象,json字符串
    	 * 
    	 * @return:Object
    	 */
    	public static Object jsonToObj(Object obj, String jsonStr)
    			throws JsonParseException, JsonMappingException, IOException {
    //		System.out.println(
    public class Reader {
        public static String readToString(String fileName) {
            String encoding = "UTF-8";
            File file = new File(fileName);
            Long filelength = file.length();
            byte[] filecontent = new byte[filelength.intValue()];
            try {
                FileInputStream in = new FileInputStream(file);
                in.read(filecontent);
                in.close();
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
            try {
                return new String(filecontent, encoding);
            } catch (UnsupportedEncodingException e) {
                System.err.println("The OS does not support " + encoding);
                e.printStackTrace();
                return null;
            }
        }
    }
    

      

    obj.toString()); // System.out.println(jsonStr); ObjectMapper mapper = new ObjectMapper(); //mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false); return obj = mapper.readValue(jsonStr, obj.getClass()); } /* * 002.对象转换成json * * @param:传入对象 * * @return:json字符串 */ public static String objToJson(Object obj) throws JsonProcessingException { ObjectMapper mapper = new ObjectMapper(); return mapper.writeValueAsString(obj); } public static Map<String, Object> jsonToMap(String jsonString) throws JsonParseException, JsonMappingException, IOException { ObjectMapper MAPPER = new ObjectMapper(); // JavaType jvt = MAPPER.getTypeFactory().constructParametricType(HashMap.class,String.class,String.class); HashMap<String,Object> mmap = MAPPER.readValue(jsonString, HashMap.class); // Map<String,String> urMap = MAPPER.readValue(jsonString, jvt); return mmap; }

      

  • 相关阅读:
    获取元素位置信息和所占空间大小(via:js&jquery)
    原生js获取元素的样式信息
    真的了解js生成随机数吗
    js中有关滑动问题的一些理解
    禁止遮罩层以下屏幕滑动----正解(更新版)
    js中的null和undefined
    通过ajax获得json数据后格式的转换
    悬浮导航栏的实现以及导航跳转
    css selector
    视频播放器
  • 原文地址:https://www.cnblogs.com/otways/p/11699588.html
Copyright © 2011-2022 走看看