parse json object

[code]
def _parseJSON(self, obj):
newobj = {}

for key, value in obj.iteritems():
key = str(key)

if isinstance(value, dict):
newobj[key] = self._parseJSON(value)
elif isinstance(value, list):
if key not in newobj:
newobj[key] = []
for i in value:
newobj[key].append(self._parseJSON(i))
elif isinstance(value, unicode):
val = str(value)
if val.isdigit():
val = int(val)
else:
try:
val = float(val)
except ValueError:
val = str(val)
newobj[key] = val

return newobj
[/code]
use like this:
obj = json.loads(content, parse_float=float, parse_int=int)
obj = _parseJSON(obj)

你可能感兴趣的:(python)