1. 程式人生 > >當 map 的 value 只是整數,沒有別的型別時,將 map string 轉成 map

當 map 的 value 只是整數,沒有別的型別時,將 map string 轉成 map

    /**      * 當  map 的 value 只是整數,沒有別的型別時,將 map string 轉成 map      */     public static Map<String, Object> mapStrToMap( String mapStr ) {         mapStr = mapStr.substring( 1, mapStr.length() - 1 );

        String[] arr = mapStr.split( ", " );

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

        for ( String str : arr ) {             String[] arrInner = str.split( "=" );

            result.put( arrInner[ 0 ],  arrInner[ 1 ] );         }

        return result;     }

呼叫

        Map<String, Object> map = new HashMap<>();         map.put( "c1", 123123 + "3" );         map.put( "c2", -1 );

        String str1 = map.toString();

        Map<String, Object> result = mapStrToMap( str1 );         System.out.println( result );

-------------------------------

序列化物件應該用 String jsonStr = JSONObject.toJSONString( map ); 和 JSONObject.parseObject( jsonStr, Map.class );

不要用 map.toString() ,再轉成 map 這種方法