python - 尝试将凌乱的字符串转换为JSON

时间:2013-01-22 19:52:30

标签: python json

我尝试使用python将以下数据转换为JSON,例如import json。我原本以为我收到JSON数据,直到我回复它并找到了下面的内容。

不幸的是,我从SOAP服务收到以下数据,因此无法控制我如何收到它。

无论如何,看起来下面是否有任何可以解析的结构?或者我是否必须逐行完成并手动转换它?它看起来不像我以前见过的任何东西,即使一些变量名称看起来像是XML,或者最初是XML。

我只对stopData[]数组的值感兴趣,只是过了一半,如果这简化了事情的话!

为了记录,我尝试使用JSON解析器解析它,如下所示,但它无法识别它:

    data  = json.loads(rawData)
    data['StopData']

I also tried:    
    data = json.loads(rawData)

但都没有奏效。 以下是我要处理的数据:

('schema', [(schema){
   _id = "NewDataSet"
   element[] = 
      (element){
         _UseCurrentLocale = "true"
         _IsDataSet = "true"
         _MainDataTable = "StopData"
         _name = "NewDataSet"
         complexType[] = 
            (complexType){
               choice[] = 
                  (choice){
                     _maxOccurs = "unbounded"
                     _minOccurs = "0"
                     element[] = 
                        (element){
                           _name = "StopData"
                           complexType[] = 
                              (complexType){
                                 sequence[] = 
                                    (sequence){
                                       element[] = 
                                          (element){
                                             _type = "xs:dateTime"
                                             _name = "ServiceDelivery_ResponseTimestamp"
                                             _minOccurs = "0"
                                          },
                                          (element){
                                             _type = "xs:string"
                                             _name = "ServiceDelivery_ProducerRef"
                                             _minOccurs = "0"
                                          },
                                          (element){
                                             _type = "xs:boolean"
                                             _name = "ServiceDelivery_Status"
                                             _minOccurs = "0"
                                          },
                                          (element){
                                             _type = "xs:boolean"
                                             _name = "ServiceDelivery_MoreData"
                                             _minOccurs = "0"
                                          },
                                          (element){
                                             _type = "xs:string"
                                             _name = "StopMonitoringDelivery_Version"
                                             _minOccurs = "0"
                                          },
                                          (element){
                                             _type = "xs:dateTime"
                                             _name = "StopMonitoringDelivery_ResponseTimestamp"
                                             _minOccurs = "0"
                                          },
                                          (element){
                                             _type = "xs:string"
                                             _name = "StopMonitoringDelivery_RequestMessageRef"
                                             _minOccurs = "0"
                                          },
                                          (element){
                                             _type = "xs:dateTime"
                                             _name = "MonitoredStopVisit_RecordedAtTime"
                                             _minOccurs = "0"
                                          },
                                          (element){
                                             _type = "xs:string"
                                             _name = "MonitoredStopVisit_MonitoringRef"
                                             _minOccurs = "0"
                                          },
                                          (element){
                                             _type = "xs:string"
                                             _name = "MonitoredVehicleJourney_LineRef"
                                             _minOccurs = "0"
                                          },
                                          (element){
                                             _type = "xs:string"
                                             _name = "MonitoredVehicleJourney_DirectionRef"
                                             _minOccurs = "0"
                                          },
                                          (element){
                                             _type = "xs:string"
                                             _name = "FramedVehicleJourneyRef_DataFrameRef"
                                             _minOccurs = "0"
                                          },
                                          (element){
                                             _type = "xs:string"
                                             _name = "FramedVehicleJourneyRef_DatedVehicleJourneyRef"
                                             _minOccurs = "0"
                                          },
                                          (element){
                                             _type = "xs:string"
                                             _name = "MonitoredVehicleJourney_PublishedLineName"
                                             _minOccurs = "0"
                                          },
                                          (element){
                                             _type = "xs:string"
                                             _name = "MonitoredVehicleJourney_OperatorRef"
                                             _minOccurs = "0"
                                          },
                                          (element){
                                             _type = "xs:string"
                                             _name = "MonitoredVehicleJourney_DestinationRef"
                                             _minOccurs = "0"
                                          },
                                          (element){
                                             _type = "xs:string"
                                             _name = "MonitoredVehicleJourney_DestinationName"
                                             _minOccurs = "0"
                                          },
                                          (element){
                                             _type = "xs:boolean"
                                             _name = "MonitoredVehicleJourney_Monitored"
                                             _minOccurs = "0"
                                          },
                                          (element){
                                             _type = "xs:boolean"
                                             _name = "MonitoredVehicleJourney_InCongestion"
                                             _minOccurs = "0"
                                          },
                                          (element){
                                             _type = "xs:string"
                                             _name = "MonitoredVehicleJourney_BlockRef"
                                             _minOccurs = "0"
                                          },
                                          (element){
                                             _type = "xs:string"
                                             _name = "MonitoredVehicleJourney_VehicleRef"
                                             _minOccurs = "0"
                                          },
                                          (element){
                                             _type = "xs:string"
                                             _name = "MonitoredCall_VisitNumber"
                                             _minOccurs = "0"
                                          },
                                          (element){
                                             _type = "xs:boolean"
                                             _name = "MonitoredCall_VehicleAtStop"
                                             _minOccurs = "0"
                                          },
                                          (element){
                                             _type = "xs:dateTime"
                                             _name = "MonitoredCall_AimedArrivalTime"
                                             _minOccurs = "0"
                                          },
                                          (element){
                                             _type = "xs:dateTime"
                                             _name = "MonitoredCall_ExpectedArrivalTime"
                                             _minOccurs = "0"
                                          },
                                          (element){
                                             _type = "xs:dateTime"
                                             _name = "MonitoredCall_AimedDepartureTime"
                                             _minOccurs = "0"
                                          },
                                          (element){
                                             _type = "xs:dateTime"
                                             _name = "MonitoredCall_ExpectedDepartureTime"
                                             _minOccurs = "0"
                                          },
                                          (element){
                                             _type = "xs:dateTime"
                                             _name = "Timestamp"
                                             _minOccurs = "0"
                                          },
                                    },
                              },
                        },
                  },
            },
      },
 }])('diffgram', [(diffgram){
   DocumentElement[] = 
      (DocumentElement){
         StopData[] = 
            (StopData){
               _id = "StopData1"
               _rowOrder = "0"
               ServiceDelivery_ResponseTimestamp[] = 
                  "2013-01-21T20:57:40.33+00:00",
               ServiceDelivery_ProducerRef[] = 
                  "bac",
               ServiceDelivery_Status[] = 
                  "true",
               ServiceDelivery_MoreData[] = 
                  "false",
               StopMonitoringDelivery_Version[] = 
                  "1.0",
               StopMonitoringDelivery_ResponseTimestamp[] = 
                  "2013-01-21T20:57:40.333+00:00",
               StopMonitoringDelivery_RequestMessageRef[] = 
                  "0",
               MonitoredStopVisit_RecordedAtTime[] = 
                  "2013-01-21T20:57:40.333+00:00",
               MonitoredStopVisit_MonitoringRef[] = 
                  "02371",
               MonitoredVehicleJourney_LineRef[] = 
                  "27",
               MonitoredVehicleJourney_DirectionRef[] = 
                  "Inbound",
               FramedVehicleJourneyRef_DataFrameRef[] = 
                  "2013-01-21",
               FramedVehicleJourneyRef_DatedVehicleJourneyRef[] = 
                  "4215",
               MonitoredVehicleJourney_PublishedLineName[] = 
                  "77A",
               MonitoredVehicleJourney_OperatorRef[] = 
                  "bac",
               MonitoredVehicleJourney_DestinationRef[] = 
                  "00354",
               MonitoredVehicleJourney_DestinationName[] = 
                  "Ringsend Rd via Tymon Park",
               MonitoredVehicleJourney_Monitored[] = 
                  "true",
               MonitoredVehicleJourney_InCongestion[] = 
                  "false",
               MonitoredVehicleJourney_BlockRef[] = 
                  "027023A:34",
               MonitoredVehicleJourney_VehicleRef[] = 
                  "33521",
               MonitoredCall_VisitNumber[] = 
                  "39",
               MonitoredCall_VehicleAtStop[] = 
                  "false",
               MonitoredCall_AimedArrivalTime[] = 
                  "2013-01-21T21:21:00+00:00",
               MonitoredCall_ExpectedArrivalTime[] = 
                  "2013-01-21T21:20:12+00:00",
               MonitoredCall_AimedDepartureTime[] = 
                  "2013-01-21T21:21:00+00:00",
               MonitoredCall_ExpectedDepartureTime[] = 
                  "2013-01-21T21:20:12+00:00",
               Timestamp[] = 
                  "2013-01-21T20:57:40.627+00:00",
            },
            (StopData){
               _id = "StopData2"
               _rowOrder = "1"
               ServiceDelivery_ResponseTimestamp[] = 
                  "2013-01-21T20:57:40.33+00:00",
               ServiceDelivery_ProducerRef[] = 
                  "bac",
               ServiceDelivery_Status[] = 
                  "true",
               ServiceDelivery_MoreData[] = 
                  "false",
               StopMonitoringDelivery_Version[] = 
                  "1.0",
               StopMonitoringDelivery_ResponseTimestamp[] = 
                  "2013-01-21T20:57:40.333+00:00",
               StopMonitoringDelivery_RequestMessageRef[] = 
                  "0",
               MonitoredStopVisit_RecordedAtTime[] = 
                  "2013-01-21T20:57:40.333+00:00",
               MonitoredStopVisit_MonitoringRef[] = 
                  "02371",
               MonitoredVehicleJourney_LineRef[] = 
                  "27",
               MonitoredVehicleJourney_DirectionRef[] = 
                  "Inbound",
               FramedVehicleJourneyRef_DataFrameRef[] = 
                  "2013-01-21",
               FramedVehicleJourneyRef_DatedVehicleJourneyRef[] = 
                  "4061",
               MonitoredVehicleJourney_PublishedLineName[] = 
                  "77A",
               MonitoredVehicleJourney_OperatorRef[] = 
                  "bac",
               MonitoredVehicleJourney_DestinationRef[] = 
                  "00354",
               MonitoredVehicleJourney_DestinationName[] = 
                  "Ringsend Rd via Tymon Park",
               MonitoredVehicleJourney_Monitored[] = 
                  "true",
               MonitoredVehicleJourney_InCongestion[] = 
                  "false",
               MonitoredVehicleJourney_BlockRef[] = 
                  "027008:34",
               MonitoredVehicleJourney_VehicleRef[] = 
                  "33204",
               MonitoredCall_VisitNumber[] = 
                  "39",
               MonitoredCall_VehicleAtStop[] = 
                  "false",
               MonitoredCall_AimedArrivalTime[] = 
                  "2013-01-21T21:44:00+00:00",
               MonitoredCall_ExpectedArrivalTime[] = 
                  "2013-01-21T21:44:00+00:00",
               MonitoredCall_AimedDepartureTime[] = 
                  "2013-01-21T21:44:00+00:00",
               MonitoredCall_ExpectedDepartureTime[] = 
                  "2013-01-21T21:44:00+00:00",
               Timestamp[] = 
                  "2013-01-21T20:57:40.627+00:00",
            },
      },
 }])

1 个答案:

答案 0 :(得分:0)

为了记录,我使用这样的代码解决了这个问题:

tmpData = requestoutput[1][0][0][0]

这表明数据实际上存储在一个多维数组中。当我把它放到python中时,它会创建一个数组(虽然当我输出它[data = list]时,它告诉我它不能连接一个字符串和一个列表)。

但无论如何,我现在可以使用数组访问数据的各个元素。

干杯。