json数据格式错误 [英] json data format error

查看:286
本文介绍了json数据格式错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

 错误:第1行解析错误:
... ekDay:Thu,Date:new Date(Date.UTC(20
----------------------- ^
期望'STRING','NUMBER','NULL','TRUE','FALSE','{','[',got'undefined'
pre

当格​​式化下面的json时:

$ p $ {__ type :TwentyFour.Services.Weather.Objects.CurrentOneReport,TwentyFour.Services.Weather,Version = 1.2.0.0,Culture = neutral,PublicKeyToken = null,Observations:[{__ type:TwentyFour.Services.Weather。对象。观察,二十四。服务。天气,版本= 1.2.0.0,文化=中立,PublicKeyToken =空,城市名称:开普敦,位置:开普敦国际机场,温度:18.00,湿度:83,WindSpeed:2,WindDirectionAbreviated:N,Comfort:18,露点:15 ,说明:部分晴朗,温和,图标:6,图标名称:居多,缓存:false,__ type:TwentyFour.Services.Weather.Objects.Observation,TwentyFour.Services.Weather,Version = 1.2.0.0,Culture = neutral,PublicKeyToken = null,CityName:George Airport ,位置:乔治机场,天空:阴,温度:19.00,湿度:88,风速:4,风向缩短:N, 舒适: 19, 露点: 17, 描述:阴。 Mild。,Icon:7,IconName:cloudy,Cached:false},{__type:TwentyFour.Services.Weather.Objects.Observation,TwentyFour.Services.Weather,Version伊丽莎白港机场,天空:破碎的云彩,温度:23.00 ,湿度:89,WindSpeed:7,WindDirectionAbreviated:S,舒适度:22,露点:21,描述 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, : 61\" , 风速: 20, WindDirectionAbreviated: NE, 舒适: 27, 露点: 18, 描述:晴天。 Icon:1,IconName:sunny,Cached:false},{__ type:TwentyFour.Services.Weather.Objects.Observation,TwentyFour.Services.Weather,Version =1.2.0.0,文化=中性,PublicKeyToken = null,城市名称:金伯利机场,位置:金伯利机场,天空:阴,温度:21.00, : 83\" , 风速: , WindDirectionAbreviated: , 舒适: 21, 露点: 18, 描述:阴。 Mild。,Icon:7,IconName:cloudy,Cached:false},{__type:TwentyFour.Services.Weather.Objects.Observation,TwentyFour.Services.Weather,Version 东伦敦,地点,东伦敦,天空,传递云,温度:24.00,湿度:94,WindSpeed:7,WindDirectionAbreviated:NE,Comfort:23,DewPoint:23,Description Mild。,Icon:2,IconName:mostly_sunny,Cached:false},{__type:TwentyFour.Services.Weather.Objects.Observation,TwentyFour.Services.Weather,Version布隆方丹机场,天空,散射云,温度:22.00,城市名称,文化=中性,PublicKeyToken = null湿度:69,WindSpeed:4,WindDirectionAbreviated:NE,Comfort:24,露点:16,描述 Mild。,Icon:2,IconName:mostly_sunny,Cached:false}],FirstObservation:{__ type:TwentyFour.Services.Weather.Objects.Observation,TwentyFour。服务天气,版本= 1.2.0.0,文化=中性,PublicKeyToken =空,城市名称:开普敦,位置:开普敦国际机场,天空:部分阳光明媚 : 18.00\" , 湿度: 83, 风速: 2, WindDirectionAbreviated: N, 舒适: 18, 露点: 15, 描述: 局部晴朗。 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:二十四个服务。 Weather.Objects.Forecast,TwentyFour.Services.Weather,Version = 1.2.0.0,Culture = neutral,PublicKeyToken = null,DaySequence:1,Segment:null,DaySegment:null,DayOfWeek:5 星期几:星期四,短周日:星期四,日期:新日期(Date.UTC(2016,1,24,22,0,0,0)),FormattedDate: ,DayLight,D,SkyDescriptor,1,Sky,Sunny,PrecipitationDescriptor,,Precipitation,,TemperatureDescriptor TemperatureDescription: 酷, HighTemp: 20, LowTemp: 17, UV: 高, AirDescription: 20, 空气: 美丽,说明:阳光。 WindSpeed20Beaufort4BeaufortDescriptor微风WindDirection266WindDirectionAbreviatedW WindDirectionDescription: 西部大开发, 露点: 16, 湿度: 77, 舒适: 20, 可见性:空, 降雨: *, 降雪 : *, PrecipitationProbability: 0, 图标: 1, IconName: 晴天, 缓存:假}], 缓存:假}, 预测:{ __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:星期二,2月25日,DayLight,D,SkyDescriptor:1,Sky:晴天,PrecipitationDescriptor:,降水 TemperatureDescriptor: 6, TemperatureDescription: 酷, HighTemp: 20, LowTemp: 17, UV: 高, AirDescription: 20,空气:美丽‘说明’:阳光。 WindSpeed20Beaufort4BeaufortDescriptor微风WindDirection266WindDirectionAbreviatedW WindDirectionDescription: 西部大开发, 露点: 16, 湿度: 77, 舒适: 20, 可见性:空, 降雨: *, 降雪 : *, PrecipitationProbability: 0, 图标: 1, IconName: 阳光灿烂, 网页快照:假}, AstronomyReport:空, MarineReport:空,本地时间:星期四,25二月2016 08:41:02 SAST,LocalUpdateTime:Thu,25 Feb 2016 08:27:06 SAST,CountryName:南非,TimeZone:2 ,Cached:false}

 <$> 

c $ c> def convert_date(match):
格式化破碎日期
:param match:
:return:

return' '+ datetime(* map(int,match.groups()))。strftime(%Y-%m-%d T%H%M:%S)+''

一个d

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


Good day, I'm receiving the following error:

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

when formatting the following 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}

Any help will be really appreciated

解决方案

This how I have approached the logic to format the broken data:

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") + '"'

and

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

这篇关于json数据格式错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆