com.google.gson.JsonSyntaxException:com.google.gson.stream.MalformedJsonException的解决

来源:互联网 发布:手机图文设计软件 编辑:程序博客网 时间:2024/05/17 23:58

问题:

    Android开发时将服务器上下载的Json数据经过FileOutputStream存储到对应文件目录后,获取本地文件并以FileInputStream方式输出为字符串传给Gson解析时报如下错误,应该是在用I/O流以字符串形式传递数据时造成Json数据结构引入了不合法的空格字符,导致Gson无法解析。



解决方法:

   不直接解析Json数据对应的字符串,将拿到的Json数据字符串经过JsonReader处理后再将JsonReader对象传入Gson方法进行解析。示例代码如下:

JsonReader jsonReader = new JsonReader(new StringReader(jsonContext));//其中jsonContext为String类型的Json数据  jsonReader.setLenient(true);  final Bean bean = gson.fromJson(jsonReader, Bean.class);




0 0
原创粉丝点击