Python 构建嵌套json对象
当调用ever函数Python 构建嵌套json对象,python,json,Python,Json,当调用ever函数processOutput(output)时,我得到的结果很差 output = {'LEND': '0', 'LINK': 'Xx', 'REND': '4', 'RULE': ''} 假设这个函数调用了三次,那么结果json应该如下 { 'object[0]' : {'LEND': '0', 'LINK': 'Xx', 'REND': '4', 'RULE': ''}, 'object[1]' : {'LEND': '0', 'LINK': 'Xa', 'REND':
processOutput(output)
时,我得到的结果很差
output = {'LEND': '0', 'LINK': 'Xx', 'REND': '4', 'RULE': ''}
假设这个函数调用了三次,那么结果json应该如下
{ 'object[0]' : {'LEND': '0', 'LINK': 'Xx', 'REND': '4', 'RULE': ''},
'object[1]' : {'LEND': '0', 'LINK': 'Xa', 'REND': '3', 'RULE': 'two'},
'object[2]' : {'LEND': '0', 'LINK': 'Xz', 'REND': '12', 'RULE': ''}
}
这样我就可以通过这种方式访问json值对象[0]['LINK']
等等
def printOutput(self, processed_output):
#processed_output is output.
您应该将计数器和输出定义为全局变量,并将每个结果追加到输出
output = {}
c = 0
def printOutput(self, processed_output):
global c, output
output['object[%i]' % c] = processed_output
c += 1
请给出您尝试过的内容好吗?谢谢,但它给出了
name错误:全局名称“result”未定义好,我在问题的任何部分都没有看到result
,也许您需要查看您的代码。output
在我的例子中是resultoutput
不是result