【问题标题】:Iterate through JSON object returned as a list with nested dictionary?遍历作为带有嵌套字典的列表返回的 JSON 对象?
【发布时间】:2020-03-10 18:20:28
【问题描述】:

我在 JSON 方面的经验非常有限,但我有一个工作函数,它返回一个包含三个值的 JSON 列表,最后一个值是嵌套字典。我想通过挑选出嵌套字典来操作列表,然后重新格式化它。

这是 JSON 数据:

{'progress': 100,
 'time': 421.0,
 'values': {'sctpFlowsConcurrent': '0',
  'tcpClientEstablishRate': '0',
  'ethRxFrameDataRate': '0',
  'tcpServerEstablished': '12425694',
  'appAttempted': '12373847',
  'tcpServerClosedByReset': '0',
  'ethTxFrameRate': '0',
  'appAttemptedRate': '0',
  'appUnsuccessfulRate': '0',
  'concurrentAppFlows': '2',
  'rxFrames': '99042605',
  'ethTxFrameDataRate': '0',
  'appSuccessfulRate': '0',
  'appIncomplete': '12373847',
  'tcpFlowsConcurrent': '2',
  'ethRxFrames': '99248556',
  'tcpClientClosed': '12373845',
  'tcpClosedByReset': '0',
  'ethRxFrameRate': '0',
  'totalFlowsConcurrent': '2',
  'ethTxFrames': '99249661',
  'txFrames': '99146687',
  'appUnsuccessful': '0',
  'txFrameDataRate': '0',
  'appAborted': '0',
  'tcpServerCloseRate': '0',
  'tcpAttempted': '12477920',
  'tcpAttemptRate': '0',
  'rxFrameDataRate': '0',
  'tcpClientEstablished': '12373847',
  'rxFrameRate': '0',
  'tcpServerEstablishRate': '0',
  'tcpClientCloseRate': '0',
  'txFrameRate': '0',
  'superFlowsConcurrent': '0',
  'tcpClientClosedByReset': '0',
  'tcpServerClosed': '12373840',
  'udpFlowsConcurrent': '0',
  'appSuccessful': '0'}}

它是未格式化的,这就是我想对其进行操作的原因。

这是我尝试过的:

def send_data():
    bps = BPS(settings.ip, settings.ixia_user, settings.ixia_pass, settings.logger)
    run_id = bps.runTest(modelname='test_name', group=settings.__bps_group)
    send_content = bps.getRealTimeStatistics('run_id')
    for v in send_content:
        if isinstance(v, dict):
            purpose = 'do stuff'

我得到的错误来自for v in send_content: 声明E TypeError: 'NoneType' object is not iterable

send_content = bps.getRealTimeStatistics('run_id') 调用返回之前显示的 JSON 数据的函数。我不确定如何处理这种数据类型。

def getRealTimeStatistics(self, runid, enableRequestPrints=False):
        service = 'https://' + self.ipstr + '/api/v1/bps/tests/operations/getRealTimeStatistics'
        jheaders = {'content-type': 'application/json'}
        jdata = json.dumps({'runid': runid})
        r = self.session.post(service, data=jdata, headers=jheaders, verify=False)
        if (enableRequestPrints):
            self.pretty_print_requests(r)
        if (r.status_code == 200):
            return r.json().get('rts')

我是数据结构的菜鸟,如果这是一个简单的解决方法,我深表歉意。非常感谢!

【问题讨论】:

  • 您显示的 JSON 数据不是列表。但是您收到的错误提示 getRealTimeStatistics 正在返回 None
  • 如果JSON数据不是列表,那么它是字典吗?我知道该数据中有一个字典,但是数据本身也是一个字典吗?
  • 您在此处显示的此数据是具有三个键的python dictionary,第三个是另一个字典:要获取它,您只需说data['values'],假设您的数据称为data
  • 我试过你的方法@moctarjallo,但遇到了这个错误E TypeError: string indices must be integers。但是,在没有打印语句的情况下调用该变量在控制台中可以正常工作......但我对为什么它在脚本中不起作用感到困惑。
  • 我已经格式化了你的数据,再看一遍,也许你现在可以看到了。但我不明白你是如何得到这个错误的:就像你正在访问 list 而这里只有字典..

标签: json python-3.x list loops dictionary


【解决方案1】:

非常感谢您的帮助!我尝试了@moctarjallo 建议的方法,并增加了一个步骤,一切似乎都运行良好。

def send_ixia_data(bps, run_id, test_name):
    nombre = "Details for " + test_name
    x = bps.getRealTimeStatistics(run_id)
    jsonx = json.loads(x)
    var4 = jsonx['values'] #dict of 'values'

所以现在 var4 只保存列表中的嵌套字典。我不确定jsonx = json.loads(x) 究竟是如何解决我的问题的,但现在一切正常。 :)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-12-06
    • 2018-05-13
    • 1970-01-01
    • 2021-10-21
    • 2022-12-04
    相关资源
    最近更新 更多