解决Gson解析报 com.google.gson.stream.MalformedJsonException 的错误

今天在做解析和风天气时报了如下的错误:



之前解析是这样的:

                    HeWeatherResponse heWeatherResponse = new Gson().fromJson
                            (o.toString(), HeWeatherResponse.class);

找到报错的地方:


这是我在postman上解析出json格式的,但是在AS上是先转换为object类这时候是一排排列的,然后转换为字符串,这时候遇到空格解析的时候会默认解析到结尾,故而会报错误。所以应该一步到位,直接转成json格式就好了,避免错误。

正确解法:

                    HeWeatherResponse heWeatherResponse = new Gson().fromJson
                            (new Gson().toJson(o), HeWeatherResponse.class);

猜你喜欢

转载自blog.csdn.net/crazyzhangxl/article/details/80076747
今日推荐