在python中用不带双引号的键解析unicode

2024-05-13 01:44:38 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在尝试将下面的python unicode对象(不带双引号)转换为json。你知道吗

x = {
    version: '2.1.2',
    dipa: '1.2.3.4',
    dipaType: '',
    customerInfo: [{
            name: 'xyz',
            id: 1234,
            account_id: 'abc',
            contract_id: 'abc',
            in_use: true,
            region: 'NA',
            location: 'USA'
        },
        {
            name: 'XYZ',
            id: 9644,
            account_id: 'qwerty5',
            contract_id: 'qscdfgr',
            in_use: true,
            region: 'NA',
            location: 'cambridge'
        }
    ],
    maxAlertCount: 2304,
    onEgress: false,
    scrubCenters: [{
        name: 'TO',
        percentage: 95.01,
        onEgress: false
    }],
    state: 'update',
    updated: '1557950465',
    vectors: [{
            name: 'rate',
            alertNames: ['rate'],
            onEgress: false,
            Alerts: [{
                key: '1.2.3.4',
                source: 'eve',
                eNew: '1557943443',
                dc: 'TOP2',
                bond: 'Border',
                percentage: 95.01,
                gress: 'ingress',
                sourceEpochs: ['1557950408',
                    '1557950411',
                    '1557950414',
                    '1557950417',
                    '1557950420',
                    '1557950423',
                    '1557950426',
                    '1557950429',
                    '1557950432',
                    '1557950435',
                    '1557950438',
                    '1557950441',
                    '1557950444',
                    '1557950447',
                    '1557950450',
                    '1557950453',
                    '1557950456',
                    '1557950459',
                    '1557950462',
                    '1557950465'
                ],
                name: 'rate',
                category: 'rate',
                level: 'alarm',
                data_type: 'value',
                data: 19.99,
                timestamp: 1557950466,
                type: 'alert',
                value: 95.01,
                eUpdated: '1557950465'
            }],
            dcs: ['TO'],
            bonds: ['Bo']
        },
        {
            name: 'udp',
            alertNames: ['udp'],
            onEgress: false,
             Alerts: [{
                key: '1.2.3.4',
                source: 'top',
                eNew: '1557943500',
                dc: 'TO',
                bond: 'Bo',
                percentage: 95.01,
                gress: 'ingress',
                sourceEpochs: ['1557950408',
                    '1557950411',
                    '1557950414',
                    '1557950417',
                    '1557950420',
                    '1557950423',
                    '1557950426',
                    '1557950429',
                    '1557950432',
                    '1557950435',
                    '1557950438',
                    '1557950441',
                    '1557950444',
                    '1557950447',
                    '1557950450',
                    '1557950453',
                    '1557950456',
                    '1557950459',
                    '1557950462',
                    '1557950465'
                ],
                name: 'udp',
                category: 'udp',
                level: 'alert',
                data_type: 'named_values_list',
                data: [{
                    name: 'Dst',
                    value: 25
                }],
                timestamp: 1557950466,
                type: 'alert',
                eUpdated: '1557950465'
            }],
            dcs: ['TO'],
            bonds: ['Bo']
        },
        {
            name: 'tcp',
            alertNames: ['tcp_condition'],
            onEgress: false,
            Alerts: [{
                key: '1.2.3.4',
                source: 'to',
                eNew: '1557950354',
                dc: 'TO',
                bond: 'Bo',
                percentage: 95.01,
                gress: 'ingress',
                sourceEpochs: ['1557950360',
                    '1557950363',
                    '1557950366',
                    '1557950372',
                    '1557950384',
                    '1557950387',
                    '1557950396',
                    '1557950399',
                    '1557950411',
                    '1557950417',
                    '1557950423',
                    '1557950426',
                    '1557950432',
                    '1557950441',
                    '1557950444',
                    '1557950447',
                    '1557950450',
                    '1557950456',
                    '1557950459',
                    '1557950465'
                ],
                name: 'tcp',
                category: 'tcp',
                level: 'alert',
                data_type: 'named',
                data: [{
                    name: 'TCP',
                    value: 25
                }],
                timestamp: 1557950466,
                type: 'alert',
                eUpdated: '1557950465'
            }],
            dcs: ['TO'],
            bonds: ['Bo']
        }
    ],
    timestamps: {
        firstAlerted: '1557943443',
        lastAlerted: '1557950465',
        lastLeaked: null
    }
}

我试着用hjson和demjson

import hjson
result = hjson.loads(x)
import demjson
result = demjson.loads(x)

实际结果:

你知道吗hjson.scanner.hjson解码错误:额外数据:第156行第1列-第620行第27列(字符4551-232056)

你知道吗demjson.JSONDecodeError错误:JSON值结尾后出现意外文本

预期结果:

Json对象


Tags: tonameidfalsedataratevaluetype
1条回答
网友
1楼 · 发布于 2024-05-13 01:44:38

请改用demjson.decode()函数。确保将输入转换成一个长字符串,方法是用双引号将其括起来,然后像我在这里做的那样将其转换为一行。你知道吗

import demjson
import json

# input JavaScript object
x = "{ version: '2.1.2', dipa: '1.2.3.4', dipaType: '', customerInfo: [{ name: 'xyz', id: 1234, account_id: 'abc', contract_id: 'abc', in_use: true, region: 'NA', location: 'USA' }, { name: 'XYZ', id: 9644, account_id: 'qwerty5', contract_id: 'qscdfgr', in_use: true, region: 'NA', location: 'cambridge' } ], maxAlertCount: 2304, onEgress: false, scrubCenters: [{ name: 'TO', percentage: 95.01, onEgress: false }], state: 'update', updated: '1557950465', vectors: [{ name: 'rate', alertNames: ['rate'], onEgress: false, Alerts: [{ key: '1.2.3.4', source: 'eve', eNew: '1557943443', dc: 'TOP2', bond: 'Border', percentage: 95.01, gress: 'ingress', sourceEpochs: ['1557950408', '1557950411', '1557950414', '1557950417', '1557950420', '1557950423', '1557950426', '1557950429', '1557950432', '1557950435', '1557950438', '1557950441', '1557950444', '1557950447', '1557950450', '1557950453', '1557950456', '1557950459', '1557950462', '1557950465' ], name: 'rate', category: 'rate', level: 'alarm', data_type: 'value', data: 19.99, timestamp: 1557950466, type: 'alert', value: 95.01, eUpdated: '1557950465' }], dcs: ['TO'], bonds: ['Bo'] }, { name: 'udp', alertNames: ['udp'], onEgress: false, Alerts: [{ key: '1.2.3.4', source: 'top', eNew: '1557943500', dc: 'TO', bond: 'Bo', percentage: 95.01, gress: 'ingress', sourceEpochs: ['1557950408', '1557950411', '1557950414', '1557950417', '1557950420', '1557950423', '1557950426', '1557950429', '1557950432', '1557950435', '1557950438', '1557950441', '1557950444', '1557950447', '1557950450', '1557950453', '1557950456', '1557950459', '1557950462', '1557950465' ], name: 'udp', category: 'udp', level: 'alert', data_type: 'named_values_list', data: [{ name: 'Dst', value: 25 }], timestamp: 1557950466, type: 'alert', eUpdated: '1557950465' }], dcs: ['TO'], bonds: ['Bo'] }, { name: 'tcp', alertNames: ['tcp_condition'], onEgress: false, Alerts: [{ key: '1.2.3.4', source: 'to', eNew: '1557950354', dc: 'TO', bond: 'Bo', percentage: 95.01, gress: 'ingress', sourceEpochs: ['1557950360', '1557950363', '1557950366', '1557950372', '1557950384', '1557950387', '1557950396', '1557950399', '1557950411', '1557950417', '1557950423', '1557950426', '1557950432', '1557950441', '1557950444', '1557950447', '1557950450', '1557950456', '1557950459', '1557950465' ], name: 'tcp', category: 'tcp', level: 'alert', data_type: 'named', data: [{ name: 'TCP', value: 25 }], timestamp: 1557950466, type: 'alert', eUpdated: '1557950465' }], dcs: ['TO'], bonds: ['Bo'] } ], timestamps: { firstAlerted: '1557943443', lastAlerted: '1557950465', lastLeaked: null } }"

# decode it into json data
json_data = demjson.decode(x, "utf-8")
# Return the 4 space indent the original had
json_final = json.dumps(json_data, indent=4)
print(json_final)

如果您有权访问输出数据的Node JS脚本(请参阅上面的注释,我们在这里讨论了更多内容),那么可以在将JavaScript对象发送到python的API之前对其进行stringyify处理。 var pyJSON = JSON.stringify(obj);将输出正确的json数据格式

相关问题 更多 >