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 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!

上一篇:CSS从入门到精通——基础选择器
下一篇:Spark streaming不同数据来源(socket套接字、hdfs目录)和存储位置(hdfs、本地)的java代码

发表评论

最新留言

关注你微信了!
[***.104.42.241]2024年04月10日 11时50分09秒

关于作者

    喝酒易醉,品茶养心,人生如梦,品茶悟道,何以解忧?唯有杜康!
-- 愿君每日到此一游!

推荐文章