JSON

特殊情况记录

使用json.loads,key不带引号,且value中可能含有“:”,如何最好地处理?

例如:

'''{
    colorSize: [{
        "Color": "超窄边IPS",
        "SkuId": 1365289,
        "Size": "27英寸"
    }, {
        "Color": "21:9超宽IPS曲面",
        "SkuId": 1742140,
        "Size": "29英寸"
    }, {
        "Color": "17英寸(TN,5:4方屏)",
        "SkuId": 1032147,
        "Size": "17英寸"
    }],
    warestatus: 1,
    shangjiazizhi: false
}'''

解决方案:

(\w+):,误处理"21:9超宽IPS曲面"
[{,]\s*(\w+):,误处理"17英寸(TN,5:4方屏)"
(?!={|, )(\w*): 误处理"17英寸(TN,5:4方屏)"

网址: https://github.com/dmeranda/demjson 安装:

pip install demjson

使用:

    >>> import demjson

    >>> demjson.encode( ['one',42,True,None] )    # From Python to JSON
    '["one",42,true,null]'

    >>> demjson.decode( '["one",42,true,null]' )  # From JSON to Python
    ['one', 42, True, None]
import execjs
import demjson

str = '''
{
    colorSize: [{
        "Color": "超窄边IPS",
        "SkuId": 1365289,
        "Size": "27英寸"
    }, {
        "Color": "21:9超宽IPS曲面",
        "SkuId": 1742140,
        "Size": "29英寸"
    }, {
        "Color": "17英寸(TN,5:4方屏)",
        "SkuId": 1032147,
        "Size": "17英寸"
    }],
    warestatus: 1,
    shangjiazizhi: false
}
'''
print demjson.decode(str)
print execjs.eval(str)
支付宝打赏 微信打赏

您的打赏是对我最大的鼓励!

Table of Contents