Caused by: com.google.gson.stream.MalformedJsonException: Expected name at line 1 column 2 path $.
发布日期:2021-06-29 17:21:18
浏览次数:2
分类:技术文章
本文共 2695 字,大约阅读时间需要 8 分钟。
今天用gson解析json数据的时候遇到一个奇怪的问题,虽然解决了但还是没明白是什么原因。。。
起因:客户端发送的json字符串中包含二进制数据(相当于图片),我需要把这个json解析后将数据存到hbase中去,由于发来的json字符串中有转义字符,所以我想当然的想到了gson的JsonPrimitive,可是按以前的套路却发现报这个错Caused by: com.google.gson.stream.MalformedJsonException: Expected name at line 1 column 2 path $.
最后折腾了半天才发现JsonPrimitive并没有把转义字符去掉,可是当我直接用String的时候就可以,但是当用System.in时(就相当于从客户端获取数据)却不行,我很是困惑,以下是我在Myeclipse中运行的代码:public class Parse1 { private String type; public String getType() { return type; } public void setProbeid(String type) { this.type = type; } public Data data; public static class Data{ public String hctx; }}
import java.io.BufferedReader;import java.io.InputStreamReader;import com.google.gson.Gson;import com.google.gson.JsonPrimitive;public class GsonTest1 { public static void main(String[] args) throws Exception { Gson gson=new Gson(); String hehe = "{\"type\": 1, \"data\": {\"hlen\": 42, \"hctx\": \"(Q2\\u00053?\\u0000#$???\\b\\u0000E\\u0000\\u0001\\u0001?@\\u0000@\\u0011'???f\\u001d??f\\u001f?<\\u001ea\\u0000??*\" }}"; JsonPrimitive jsonPrimitive=new JsonPrimitive(hehe); System.out.println(jsonPrimitive.getAsString()); Parse1 p=gson.fromJson(jsonPrimitive.getAsString(), Parse1.class); System.out.println("type:"+p.getType()); System.out.println("hctx:"+p.data.hctx); BufferedReader wt = new BufferedReader(new InputStreamReader(System.in)); String str = wt.readLine(); JsonPrimitive jsonPrimitive1=new JsonPrimitive(str); System.out.println(jsonPrimitive.getAsString()); Parse1 p1=gson.fromJson(jsonPrimitive1.getAsString(), Parse1.class); System.out.println("type:"+p1.getType()); System.out.println("hctx:"+p1.data.hctx); }}
运行结果:
解决:不用JsonPrimitive而是直接用Java的replaceAll替代方法
import java.io.BufferedReader;import java.io.IOException;import java.io.InputStreamReader;import com.google.gson.JsonPrimitive;public class hehe { public static void main(String args[]) throws IOException{ String a = "{\"type\": 1, \"data\": {\"hlen\": 42, \"hctx\": \"(Q2\\u00053?\\u0000#$???\\b\\u0000E\\u0000\\u0001\\u0001?@\\u0000@\\u0011'???f\\u001d??f\\u001f?<\\u001ea\\u0000??*\" }}"; System.out.println(a.replaceAll("\\\\\"", "\"")); JsonPrimitive jsonPrimitive=new JsonPrimitive(a); System.out.println(jsonPrimitive.getAsString()); BufferedReader wt = new BufferedReader(new InputStreamReader(System.in)); String hui = wt.readLine(); System.out.println(hui.replaceAll("\\\\\"", "\"")); JsonPrimitive jsonPrimitive1=new JsonPrimitive(hui); System.out.println(jsonPrimitive1.getAsString()); System.out.println(hui.replaceAll("\\\\\"", "\"").replaceAll("\\\\\\\\", "\\\\")); }}运行结果:
转载地址:https://blog.csdn.net/m0_37739193/article/details/78657155 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!
发表评论
最新留言
关注你微信了!
[***.104.42.241]2024年04月10日 11时50分09秒
关于作者
喝酒易醉,品茶养心,人生如梦,品茶悟道,何以解忧?唯有杜康!
-- 愿君每日到此一游!
推荐文章
android 混淆代码后 app 运行报错时, 如何精准定位报错位置
2021-07-03
android 定位并通过百度在线查询详细地址教程
2021-07-03
android TextView 首行缩进与部分文字改变颜色大小效果
2021-07-03
android app 优化启动体验, 不闪白屏并且快速展示 splash
2021-07-03
INSTALL_FAILED_NO_MATCHING_ABIS 解决方案
2021-07-03
android 把打好的 apk 包通过 adb 的方式安装到手机上
2021-07-03
区块链学习之路[持续更新]
2021-07-03
RecycleView-Java.lang.IllegalArgumentException: Called attach on a child which is not detached
2021-07-03
AI学习笔记 (一) 手写识别
2021-07-03
七牛云图片外链失效的解决办法
2021-07-03
[Vue warn]: Invalid prop: type check failed for prop “src“. Expected String, Object, got Module
2021-07-03
Laravel 8 整合 Vue 2 解决 history 路由模式 404 问题
2021-07-03
MongoDB 小试牛刀--创建数据库和用户
2021-07-03
C++单链表的文件存取
2021-07-03
Ubuntu 20.04 卸载 xubuntu 安装 gnome 桌面
2021-07-03