MCPcopy Create free account
hub / github.com/cxapython/catvm / parse_json

Function parse_json

selenium/shannon_spider/util/__init__.py:19–33  ·  view source on GitHub ↗

把一个类json字符串转换成json字符串

(string)

Source from the content-addressed store, hash-verified

17 return json.dumps(req)
18
19def 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
35def parse_date(d):
36 if d is None or '-' == d: return None

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected