getWeather Underground API 正在返回带有格式化字符(大量 等)的 JSON.有没有办



这是我从地下天气得到的回应:

"n{n  "response": {n  "version":"0.1",n  "termsofService":"http://www.wunderground.com/weather/api/d/terms.html",n  "features": {n  "geolookup": 1n  }nt}ntt,t"location": {ntt"type":"INTLCITY",ntt"country":"EG",ntt"country_iso3166":"EG",ntt"country_name":"Egypt",ntt"state":"",ntt"city":"Wadi El Natroon",ntt"tz_short":"EET",ntt"tz_long":"Africa/Cairo",ntt"lat":"30.000000",ntt"lon":"30.000000",ntt"zip":"00000",ntt"magic":"1",ntt"wmo":"62357",ntt"l":"/q/zmw:00000.1.62357",ntt"requesturl":"global/stations/62357.html",ntt"wuiurl":"http://www.wunderground.com/global/stations/62357.html",ntt"nearby_weather_stations": {ntt"airport": {ntt"station": [ntt{ "city":"Wadi El Natroon", "state":"", "country":"Egypt", "icao":"", "lat":"30.40250015", "lon":"30.36333275" }ntt,{ "city":"Alexandria Borg El Arab", "state":"", "country":"EG", "icao":"HEBA", "lat":"30.91769981", "lon":"29.69639969" }ntt,{ "city":"Alexandria", "state":"", "country":"EG", "icao":"HEAX", "lat":"31.18166733", "lon":"29.94638824" }ntt]ntt}ntt,ntt"pws": {ntt"station": [ntt]ntt}ntt}nt}n}n"

正如你所看到的,有一堆不应该出现的角色。是否有不同的查询来获取未格式化的 JSON,或者我是否必须在将其交给 JSON 解析器之前解析所有这些垃圾?我是否处于某种调试模式或其他什么?

我认为您正在将 Restful 用于 Web 服务,如果您使用其余的不进行编码,则以 Json 对数组进行编码,默认情况下它在 Json 中工作。

换行符 () 和制表符 (\t) 可能由调试器列出,而实际响应包含格式化数据(因此换行符显示为实际换行符)。这不会给 JSON 解析器带来任何问题,只需将数据提供给它即可。

哎呀。这是一个Gson问题(或我使用Gson的问题),而不是地下天气问题。需要使用:

val jsonObj = JsonParser().parse(it).asJsonObject

而不是:

val jsonObj = gson.toJsonTree(it)

它是 JSON 字符串。代码在 Kotlin 中。

相关内容

最新更新