fix json_encode and refine code