python 解析多层级的json数据

#! /usr/bin/env pyhton
# -*- coding:utf-8 -*-
# author:jeff.xie
# datetime:2024/2/2 14:03
# software:PyCharm


import json

def get_case_name_status(path):
    # 读取json文件
    with open(path, 'r', encoding='utf-8') as f:
        data = json.load(f)

    # 解析json数据
    function_name = data["name"]
    print(function_name)

    status = data["testStage"]["status"]   # 解析 testStage对应json内容,取status对应的值
    case_name = data["testStage"]["steps"][0]["name"]
    # 解析 testStage对应json内容,取steps对应的json数据的第一个json数据,取name对应的值
    print(status)
    print(case_name)


if __name__ == '__main__':
    path = 'case_status.json'
    get_case_name_status(path)

Json文件内容

{
  "uid" : "3bd701331fbd1356",
  "name" : "test_05",
  "fullName" : "testcases.test_weather.Test_weather#test_05",
  "historyId" : "04bdebd4cda63fb0e230d98dbfff4f7f",
  "time" : {
    "start" : 1706839388327,
    "stop" : 1706839388479,
    "duration" : 152
  },
  "status" : "passed",
  "flaky" : false,
  "beforeStages" : [ {
    "name" : "_xunit_setup_class_fixture_Test_weather",
    "time" : {
      "start" : 1706839388326,
      "stop" : 1706839388326,
      "duration" : 0
    },
    "status" : "passed",
    "steps" : [ ],
    "attachments" : [ ],
    "parameters" : [ ],
    "hasContent" : false,
    "stepsCount" : 0,
    "attachmentsCount" : 0,
    "shouldDisplayMessage" : false
  } ],
  "testStage" : {
    "status" : "passed",
    "steps" : [ {
      "name" : "获取上海天气",
      "time" : {
        "start" : 1706839388327,
        "stop" : 1706839388479,
        "duration" : 152
      },
      "status" : "passed",
      "steps" : [ ],
      "attachments" : [ ],
      "parameters" : [ ],
      "hasContent" : false,
      "stepsCount" : 0,
      "attachmentsCount" : 0,
      "shouldDisplayMessage" : false
    } ],
    "attachments" : [ {
      "uid" : "58a67a26a5822186",
      "name" : "log",
      "source" : "58a67a26a5822186.txt",
      "type" : "text/plain",
      "size" : 242
    } ],
    "parameters" : [ ],
    "hasContent" : true,
    "stepsCount" : 1,
    "attachmentsCount" : 1,
    "shouldDisplayMessage" : false
  },
  "afterStages" : [ {
    "name" : "_xunit_setup_class_fixture_Test_weather::0",
    "time" : {
      "start" : 1706839388797,
      "stop" : 1706839388798,
      "duration" : 1
    },
    "status" : "passed",
    "steps" : [ ],
    "attachments" : [ ],
    "parameters" : [ ],
    "hasContent" : false,
    "stepsCount" : 0,
    "attachmentsCount" : 0,
    "shouldDisplayMessage" : false
  } ],
  "labels" : [ {
    "name" : "severity",
    "value" : "trivial"
  }, {
    "name" : "parentSuite",
    "value" : "testcases"
  }, {
    "name" : "suite",
    "value" : "test_weather"
  }, {
    "name" : "subSuite",
    "value" : "Test_weather"
  }, {
    "name" : "host",
    "value" : "QH20121401L"
  }, {
    "name" : "thread",
    "value" : "5420-MainThread"
  }, {
    "name" : "framework",
    "value" : "pytest"
  }, {
    "name" : "language",
    "value" : "cpython3"
  }, {
    "name" : "package",
    "value" : "testcases.test_weather"
  }, {
    "name" : "resultFormat",
    "value" : "allure2"
  } ],
  "parameters" : [ ],
  "links" : [ {
    "name" : "https://www.baidu.com",
    "url" : "https://www.baidu.com",
    "type" : "issue"
  } ],
  "hidden" : false,
  "retry" : false,
  "extra" : {
    "severity" : "trivial",
    "retries" : [ ],
    "categories" : [ ],
    "tags" : [ ]
  },
  "source" : "3bd701331fbd1356.json",
  "parameterValues" : [ ]
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值