把一个类json字符串转换成json字符串
(string)
| 17 | return json.dumps(req) |
| 18 | |
| 19 | def parse_json(string): |
| 20 | ''' |
| 21 | 把一个类json字符串转换成json字符串 |
| 22 | ''' |
| 23 | if len(string) == 0: |
| 24 | raise Exception('argument value {} error'.format(string)) |
| 25 | |
| 26 | pattern = r"([a-zA-Z_][a-zA-Z_0-9]*)\s*\:" |
| 27 | repl = lambda match: '"{}":'.format(match.group(1)) |
| 28 | json_str = string[string.find('{'):string.rfind('}')+1] # '{' 从前往后找,'}'从后往前找 |
| 29 | json_str = re.sub(pattern, repl, json_str) |
| 30 | json_str = re.sub(r',+', ',', json_str) # 把连续的逗号(两个或两个以上)替换成一个逗号 |
| 31 | json_str = re.sub(r',]', ']', json_str) # 列表中最后一个元素后面不能有逗号,和python语法不同 |
| 32 | |
| 33 | return json.loads(json_str) |
| 34 | |
| 35 | def parse_date(d): |
| 36 | if d is None or '-' == d: return None |
nothing calls this directly
no outgoing calls
no test coverage detected