json数据格式错误

时间:2016-02-25 07:38:53

标签: json formatting

美好的一天,我收到以下错误:

    Error: Parse error on line 1:
...ekDay":"Thu","Date":new Date(Date.UTC(20
-----------------------^
Expecting 'STRING', 'NUMBER', 'NULL', 'TRUE', 'FALSE', '{', '[', got 'undefined'
格式化以下json时

{"__type":"TwentyFour.Services.Weather.Objects.CurrentOneReport, TwentyFour.Services.Weather, Version=1.2.0.0, Culture=neutral, PublicKeyToken=null","Observations":[{"__type":"TwentyFour.Services.Weather.Objects.Observation, TwentyFour.Services.Weather, Version=1.2.0.0, Culture=neutral, PublicKeyToken=null","CityName":"Cape Town","Location":"Cape Town International Airport","Sky":"Partly sunny","Temperature":"18.00","Humidity":"83","WindSpeed":"2","WindDirectionAbreviated":"N","Comfort":"18","DewPoint":"15","Description":"Partly sunny. Mild.","Icon":"6","IconName":"mostly_cloudy","Cached":false},{"__type":"TwentyFour.Services.Weather.Objects.Observation, TwentyFour.Services.Weather, Version=1.2.0.0, Culture=neutral, PublicKeyToken=null","CityName":"George Airport","Location":"George Airport","Sky":"Overcast","Temperature":"19.00","Humidity":"88","WindSpeed":"4","WindDirectionAbreviated":"N","Comfort":"19","DewPoint":"17","Description":"Overcast. Mild.","Icon":"7","IconName":"cloudy","Cached":false},{"__type":"TwentyFour.Services.Weather.Objects.Observation, TwentyFour.Services.Weather, Version=1.2.0.0, Culture=neutral, PublicKeyToken=null","CityName":"Port Elizabeth Airport","Location":"Port Elizabeth Airport","Sky":"Broken clouds","Temperature":"23.00","Humidity":"89","WindSpeed":"7","WindDirectionAbreviated":"S","Comfort":"22","DewPoint":"21","Description":"Broken clouds. Mild.","Icon":"6","IconName":"mostly_cloudy","Cached":false},{"__type":"TwentyFour.Services.Weather.Objects.Observation, TwentyFour.Services.Weather, Version=1.2.0.0, Culture=neutral, PublicKeyToken=null","CityName":"Upington Airport","Location":"Upington Airport","Sky":"Sunny","Temperature":"26.00","Humidity":"61","WindSpeed":"20","WindDirectionAbreviated":"NE","Comfort":"27","DewPoint":"18","Description":"Sunny. Warm.","Icon":"1","IconName":"sunny","Cached":false},{"__type":"TwentyFour.Services.Weather.Objects.Observation, TwentyFour.Services.Weather, Version=1.2.0.0, Culture=neutral, PublicKeyToken=null","CityName":"Kimberley Airport","Location":"Kimberley Airport","Sky":"Overcast","Temperature":"21.00","Humidity":"83","WindSpeed":"","WindDirectionAbreviated":"","Comfort":"21","DewPoint":"18","Description":"Overcast. Mild.","Icon":"7","IconName":"cloudy","Cached":false},{"__type":"TwentyFour.Services.Weather.Objects.Observation, TwentyFour.Services.Weather, Version=1.2.0.0, Culture=neutral, PublicKeyToken=null","CityName":"East London","Location":"East London","Sky":"Passing clouds","Temperature":"24.00","Humidity":"94","WindSpeed":"7","WindDirectionAbreviated":"NE","Comfort":"23","DewPoint":"23","Description":"Passing clouds. Mild.","Icon":"2","IconName":"mostly_sunny","Cached":false},{"__type":"TwentyFour.Services.Weather.Objects.Observation, TwentyFour.Services.Weather, Version=1.2.0.0, Culture=neutral, PublicKeyToken=null","CityName":"Bloemfontein Airport","Location":"Bloemfontein Airport","Sky":"Scattered clouds","Temperature":"22.00","Humidity":"69","WindSpeed":"4","WindDirectionAbreviated":"NE","Comfort":"24","DewPoint":"16","Description":"Scattered clouds. Mild.","Icon":"2","IconName":"mostly_sunny","Cached":false}],"FirstObservation":{"__type":"TwentyFour.Services.Weather.Objects.Observation, TwentyFour.Services.Weather, Version=1.2.0.0, Culture=neutral, PublicKeyToken=null","CityName":"Cape Town","Location":"Cape Town International Airport","Sky":"Partly sunny","Temperature":"18.00","Humidity":"83","WindSpeed":"2","WindDirectionAbreviated":"N","Comfort":"18","DewPoint":"15","Description":"Partly sunny. Mild.","Icon":"6","IconName":"mostly_cloudy","Cached":false},"Location":{"__type":"TwentyFour.Services.Weather.Objects.Location, TwentyFour.Services.Weather, Version=1.2.0.0, Culture=neutral, PublicKeyToken=null","City":"77107","CityName":"Cape Town","Forecasts":[{"__type":"TwentyFour.Services.Weather.Objects.Forecast, TwentyFour.Services.Weather, Version=1.2.0.0, Culture=neutral, PublicKeyToken=null","DaySequence":1,"Segment":null,"DaySegment":null,"DayOfWeek":5,"WeekDay":"Thursday","ShortWeekDay":"Thu","Date":new Date(Date.UTC(2016,1,24,22,0,0,0)),"FormattedDate":"Thu, Feb 25","DayLight":"D","SkyDescriptor":"1","Sky":"Sunny","PrecipitationDescriptor":"","Precipitation":"","TemperatureDescriptor":"6","TemperatureDescription":"Cool","HighTemp":"20","LowTemp":"17","UV":"High","AirDescription":"20","Air":"Beautiful","Description":"Sunny. Cool.","Temperature":null,"WindSpeed":"20","Beaufort":"4","BeaufortDescriptor":"Moderate breeze","WindDirection":"266","WindDirectionAbreviated":"W","WindDirectionDescription":"West","DewPoint":"16","Humidity":"77","Comfort":"20","Visibility":null,"Rainfall":"*","Snowfall":"*","PrecipitationProbability":"0","Icon":"1","IconName":"sunny","Cached":false}],"Cached":false},"Forecast":{"__type":"TwentyFour.Services.Weather.Objects.Forecast, TwentyFour.Services.Weather, Version=1.2.0.0, Culture=neutral, PublicKeyToken=null","DaySequence":1,"Segment":null,"DaySegment":null,"DayOfWeek":5,"WeekDay":"Thursday","ShortWeekDay":"Thu","Date":new Date(Date.UTC(2016,1,24,22,0,0,0)),"FormattedDate":"Thu, Feb 25","DayLight":"D","SkyDescriptor":"1","Sky":"Sunny","PrecipitationDescriptor":"","Precipitation":"","TemperatureDescriptor":"6","TemperatureDescription":"Cool","HighTemp":"20","LowTemp":"17","UV":"High","AirDescription":"20","Air":"Beautiful","Description":"Sunny. Cool.","Temperature":null,"WindSpeed":"20","Beaufort":"4","BeaufortDescriptor":"Moderate breeze","WindDirection":"266","WindDirectionAbreviated":"W","WindDirectionDescription":"West","DewPoint":"16","Humidity":"77","Comfort":"20","Visibility":null,"Rainfall":"*","Snowfall":"*","PrecipitationProbability":"0","Icon":"1","IconName":"sunny","Cached":false},"AstronomyReport":null,"MarineReport":null,"LocalTime":"Thu, 25 Feb 2016 08:41:02 SAST","LocalUpdateTime":"Thu, 25 Feb 2016 08:27:06 SAST","CountryName":"South Africa","TimeZone":"2","Cached":false}

任何帮助都将非常感激

1 个答案:

答案 0 :(得分:0)

这是我如何处理格式化破碎数据的逻辑:

def convert_date(match):
""" Format the broken date
:param match:
:return:
"""
return '"' + datetime(*map(int, match.groups())).strftime("%Y-%m-%d T%H%M:%S") + '"'

data = re.sub(r"new Date\(Date\.UTC\((\d+),(\d+),(\d+),(\d+),(\d+),(\d+),(\d+)\)\)", convert_date, request_post.text)