Caused by: com.google.gson.stream.MalformedJsonException: Expected name at line 1 column 2 path $.

今天用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("\\\\\\\\", "\\\\"));
	}
}
运行结果:

  • 3
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

小强签名设计

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值