com.google.gson.stream.MalformedJson Expected literal value at line 1 column 28
来源:互联网 发布:域名防红工具 编辑:程序博客网 时间:2024/06/08 02:50
今天使用json字符串转对象的时候,遇到一个错误。所以做一个总结
Exception in thread "main" com.google.gson.JsonSyntaxException: com.google.gson.stream.MalformedJsonException: Expected literal value at line 1 column 28
这个错误是json字符串有存在问题。
我有一个这样子的字符串
String httpResponse="{\"code\":\"0\",\"success\":true,\"data\":{\"IsSuccess\":1,\"FailReason\":\"\",\"RefundOrderId\":\"161017132456273328\",\"ErrorCode\":\"\"},\"message\":\"\",\"requestid\":\"8ef58d60-7d72-46ec-b16a-3692b30d4a5c\"}";Refund response=new Gson().fromJson(httpResponse, Refund.class);BaseResponse baseResponse=new BaseResponse();baseResponse.setCode(response.getCode());baseResponse.setMessage(response.getMessage());baseResponse.setSuccess(response.isSuccess());baseResponse.setData(response.getData().toString());//改成baseResponse.setData(new Gson().toJson(response.getData()));
其实这里的json字符串变成了{IsSuccess=1.0, FailReason=, RefundOrderId=161017132456273328, ErrorCode=}这样子。
RefundApplyResponse res1=new Gson().fromJson(baseResponse.getData(),RefundApplyResponse.class);
因为我们这边的代码都是要先转换成BaseResponse的,然后在转换成对应的对象,我这里只是吧代码简单的复制出来了。还有很多没贴
一执行这段代码。就报错
所以用上面的Data的json字符串,去转换对象,肯定是要报错的。
那要怎么解决呢?怎么在Data那里为空的数据,""双引号不会自动去掉
所以要在生成错误Json字符串,那里修改:
baseResponse.setData(new Gson().toJson(response.getData()));
打印的json字符串为:{"IsSuccess":1.0,"FailReason":"","RefundOrderId":"161017132456273328","ErrorCode":""}
这样子的话,就可以了
0 0
- com.google.gson.stream.MalformedJson Expected literal value at line 1 column 28
- 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
- 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 $
- Expected an int but was 0.01 at line 1 column 782 Gson工具使用出错的解决
- Gson解析Json字符串出现异常:Expected a BEGIN_ARRAY but was STRING at line 1 column 107 $的解决方法
- Expected BEGIN_ARRAY but was BEGIN_OBJECT at line 1 column 498
- com.google.gson.JsonSyntaxException: com.google.gson.d.e: Unterminated object at line
- com.google.gson.stream.MalformedJsonException
- com.google.gson.JsonSyntaxException: com.google.gson.stream.MalformedJsonException:
- com.google.gson.JsonSyntaxException: com.google.gson.stream.MalformedJsonException:解决办法
- reason=>"Expected one of #, input, filter, output at line 1, column 1 (byte 1) after "}
- java.lang.IllegalStateException: Expected BEGIN_OBJECT but was was STRING at line 1 column 1
- 解析json Expected BEGIN_ARRAY but was NUMBER at line 1 column 5
- java.lang.IllegalStateException: Expected BEGIN_ARRAY but was BEGIN_OBJECT at line 1 column 81
- 【JWPlayer】官方JWPlayer去水印步骤
- Spark Broadcast 广播变量
- 列间距column-gap
- Java 堆内存和栈内存
- The differences between define and typedef
- com.google.gson.stream.MalformedJson Expected literal value at line 1 column 28
- 列表边框column-rule
- 一道Java集合框架题!!!!!
- Lucene 学习资料
- shrink_page_list 函数分析
- android 安全机制验证机制总结(就本人目前的水平进行总结,记录以方便以后提高补充)
- 排序算法汇总
- 导布局ID的神Convert Layout XML to Java
- 获取json对象的长度