我正在尝试将下面没有双引号的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.HjsonDecodeError:额外数据:第156行第1列-第620行第27列(字符4551-232056)
demjson.JSONDecodeError:JSON值结束后出现意外文本
预期结果:
Json对象
答案 0 :(得分:1)
请尝试使用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之前对其进行字符串化。
var pyJSON = JSON.stringify(obj);
将输出正确的json数据格式