将 json 转换为数据框,如 python 中的输出

convert json to a data frame like output in python

我有一个脚本可以从 CA Introscope 中提取数据。该脚本打印出这样的值:

print data

[(TimesliceGroupedMetricData){
   metricData[] = 
      (MetricData){
         agentName = "web01|WEB|instance_01"
         metricName = "WebSpherePMI|orbPerfModule:ConcurrentRequestCount"
         metricType = 258
         metricValue = "0"
      },
      (MetricData){
         agentName = "app01|APP|instnace_03"
         metricName = "WebSpherePMI|beanModule|mobile.jar:ReturnsToPoolCount"
         metricType = 258
         metricValue = "3"
      },
      (MetricData){
         agentName = "app02|APP|instance_02"
         metricName = "JSP|add_client:Stall Count"
         metricType = 385
         metricValue = "0"
      },
      (MetricData){
         agentName = "web05|WEB|instance_02"
         metricName = "WebSpherePMI|beanModule|bizlogic#bizlogic.jar|ejb.entity|WorkStepInstanceEBBBean:ReadyCount"
         metricType = 258
         metricValue = "0"
      },
   timesliceEndTime = 2015-01-05 16:33:15
   timesliceStartTime = 2015-01-05 16:28:15


}]

我需要这样输出:

metricName、AppName、ServerName、InstanceName、Value、timesliceEndTime

实际数据:

WebSpherePMI|orbPerfModule:ConcurrentRequestCount, WEB, web01,instance_01, 0, 2015-01-05 16:33:15
WebSpherePMI|beanModule|mobile.jar:ReturnsToPoolCount,APP,app01,instance_02,3, 2015-01-05 16:33:15
JSP|add_client:Stall Count, APP, app02,instance_02,0, 2015-01-05 16:33:15
WebSpherePMI|beanModule|bizlogic#bizlogic.jar|ejb.entity|WorkStepInstanceEBBBean:ReadyCount, WEB, web05, insntance02, 0, 2015-01-05 16:33:15

我用逗号分隔字段输出,但这不是必需的。有人可以给我一些如何在 python 中执行此操作的指南吗?

好的,就这样吧。我做了一个虚拟 class 并用你的数据填充它,所以它被测试了,YMMV。

def dumpDataBlob(blob):
    """ Print data object in required format """

    timesliceEndTime = blob.timesliceEndTime

    for entry in blob.metricData:
        # gather values
        metricName = entry.metricName
        metricValue = entry.metricValue
        (serverName, appName, instanceName) = entry.agentName.split('|')

        print('{met},{app},{ser},{ins},{val},{tim}'.format(
            met=metricName,
            app=appName,
            ser=serverName,
            ins=instanceName,
            val=metricValue,
            tim=timesliceEndTime))

这是我的测试class是的,仅供参考

class MetricData:
    def __init__(self, agentName, metricName, metricType, metricValue):
        self.agentName = agentName
        self.metricName = metricName
        self.metricType = metricType
        self.metricValue = metricValue

class DummyObject:
    metricData = []

    def __init__(self):
        self.metricData = []

        self.metricData.append(MetricData(
            agentName = "web01|WEB|instance_01",
            metricName = "WebSpherePMI|orbPerfModule:ConcurrentRequestCount",
            metricType = 258,
            metricValue = "0"
        ))

        self.metricData.append(MetricData(
            agentName = "app01|APP|instnace_03",
            metricName = "WebSpherePMI|beanModule|mobile.jar:ReturnsToPoolCount",
            metricType = 258,
            metricValue = "3"
        ))

        self.metricData.append(MetricData(
            agentName = "app02|APP|instance_02",
            metricName = "JSP|add_client:Stall Count",
            metricType = 385,
            metricValue = "0"
        ))

        self.metricData.append(MetricData(
            agentName = "web05|WEB|instance_02",
            metricName = "WebSpherePMI|beanModule|bizlogic#bizlogic.jar|ejb.entity|WorkStepInstanceEBBBean:ReadyCount",
            metricType = 258,
            metricValue = "0"
        ))

        self.timesliceEndTime = "2015-01-05 16:33:15"
        self.timesliceStartTime = "2015-01-05 16:28:15"