Weather Underground API正在返回带有格式化字符(大量\ n \ t等)的JSON。 有没有办法从他们得到未格式化的JSON?

这是我从Weather Underground得到的回应:

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

正如你所看到的,有一堆不应该在那里的角色。 是否有一个不同的查询来获得无格式的JSON或我必须解析所有这些垃圾之前,把它交给一个JSON解析器? 我在某种调试模式或什么?

我认为你使用的Restful的Web服务,你在JSON编码数组,如果你使用其余的不做编码默认情况下工作在JSON。

换行符(\ n)和制表符(\ t)字符可能被调试器列出,而实际的响应包含格式化的数据(所以换行符显示为实际的换行符)。 这不会对JSON解析器造成任何问题,只是将数据提供给它。

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

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

代替:

 val jsonObj = gson.toJsonTree(it) 

这是JSON字符串。 代码在Kotlin。

Interesting Posts