Caused by: com.google.gson.stream.MalformedJsonException: Expected name at line 1 column 2 path $.
来源:互联网 发布:mac韩服lol汉化 编辑:程序博客网 时间:2024/06/04 18:47
今天用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("\\\\\\\\", "\\\\"));}}运行结果:
阅读全文
1 0
- Caused by: com.google.gson.stream.MalformedJsonException: Expected name at line 1 column 2 path $.
- 错误处理:com.google.gson.stream.MalformedJsonException: Unexpected value at line 1 column 15
- 错误处理:com.google.gson.stream.MalformedJsonException: Unexpected value at line 1 column 15
- HTTP传输错误解决com.google.gson.stream.MalformedJsonException: Unterminated string at line 1 column 13
- com.google.gson.stream.MalformedJson Expected literal value at line 1 column 28
- com.google.gson.stream.MalformedJsonException
- Gson 解析时候报错: Expected BEGIN_OBJECT but was BEGIN_ARRAY at line 1 column 2 path $
- Gson 解析时候报错: Expected BEGIN_OBJECT but was BEGIN_ARRAY at line 1 column 2 path $
- com.google.gson.stream.MalformedJsonException的解决办法
- com.google.gson.stream.MalformedJsonException的解决办法
- com.google.gson.stream.MalformedJsonException的解决办法
- com.google.gson.JsonSyntaxException: com.google.gson.stream.MalformedJsonException:
- com.google.gson.JsonSyntaxException: com.google.gson.stream.MalformedJsonException:解决办法
- 使用GSON抛出com.google.gson.stream.MalformedJsonException异常
- Expected BEGIN_OBJECT but was BEGIN_ARRARY at line 1 column 2 path$异常解决方法
- Expected BEGIN_OBJECT but was BEGIN_ARRAY at line 1 column 2 path 解决办法
- Expected BEGIN_OBJECT but was BEGIN_ARRAY at line 1 column 2 path 解决办法
- java.lang.IllegalStateException: Expected BEGIN_OBJECT but was BEGIN_ARRAY at line 1 column 2 path $
- Ice与Corba之争(The Rise and Fall of CORBA)
- 成了精的太师椅·树的咆哮·五
- 安卓自定义View基础-颜色
- 一种自定义的Toast的工具类
- Python“传值”或是“引用”?
- Caused by: com.google.gson.stream.MalformedJsonException: Expected name at line 1 column 2 path $.
- sso单点登录
- linux 关机操作
- 写个面经(人生第一面)——手机百度PHP后台开发
- 针对app移动端的测试总结
- 在保存时出现的一个小问题
- 主元素问题探索算法的妙不可言
- 欢迎使用CSDN-markdown编辑器
- Elasticsearch入门