admin管理员组文章数量:1794759
java.lang.IllegalStateException: Expected BEGIN
java.lang.IllegalStateException: Expected BEGIN_OBJECT but was STRING at line 1 column 2 path $
代码语言:javascript代码运行次数:0运行复制package com.example.core.mydemo.scooterOrderSms;
import com.alibaba.fastjson.JSON;
import com.example.core.mydemo.json2.GsonUtils;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import org.apachemons.lang3.StringUtils;
public class JSONTest {
private static final Gson gson = new GsonBuilder().setDateFormat("yyyy-MM-dd HH:mm:ss").create();
public static <T> T convertObj(String json, Class<T> cls) {
if (StringUtils.isBlank(json)) {
return null;
}
return gson.fromJson(json, cls);
}
public static void main(String[] args) {
TemplateCodeSmsMq mq1 = new TemplateCodeSmsMq();
mq1.setMobile("137XXXX1198");
mq1.setTemplateCode("myTemplateId001");
System.out.println("mq=" + JSON.toJSONString(mq1));
//java.lang.IllegalStateException: Expected BEGIN_OBJECT but was STRING at line 1 column 2 path $
//Expected BEGIN_OBJECT but was STRING at line 1 column 2 path $
//" Expected BEGIN_OBJECT but was STRING at line 1 column 2 path $
String messageBody = "\"{\"mobile\":\"137XXXX1198\",\"params\":{\"expireDate\":\"2023-12-28\",\"plateNum\":\"测RGU269\",\"reportDateTime\":\"2023年11月28日\",\"url\":\"\"},\"templateCode\":\"myTemplateId001\"}\"";
System.out.println("messageBody=" + messageBody);
TemplateCodeSmsMq mq = null;
try{
mq = GsonUtils.convertObj(messageBody, TemplateCodeSmsMq.class);
System.out.println("mq=" + mq.toString());
}catch (Exception e) {
e.printStackTrace();
}
//转换之后
if(messageBody.startsWith("\"") && messageBody.endsWith("\"")){
messageBody = messageBody.substring(1,messageBody.length() - 1).replace("\\","");
}
System.out.println("messageBody22=" + messageBody);
mq = GsonUtils.convertObj(messageBody, TemplateCodeSmsMq.class);
System.out.println("mq=" + mq.toString());
}
}
代码语言:javascript代码运行次数:0运行复制import java.util.Map;
public class TemplateCodeSmsMq {
private String mobile;
private String templateCode;
private Map<String,Object> params;
@Override
public String toString() {
return "TemplateCodeSmsMq{" +
"mobile='" + mobile + '\'' +
", templateCode='" + templateCode + '\'' +
", params=" + params +
'}';
}
public String getMobile() {
return mobile;
}
public void setMobile(String mobile) {
this.mobile = mobile;
}
public String getTemplateCode() {
return templateCode;
}
public void setTemplateCode(String templateCode) {
this.templateCode = templateCode;
}
public Map<String, Object> getParams() {
return params;
}
public void setParams(Map<String, Object> params) {
this.params = params;
}
}
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。 原始发表:2023-12-04,如有侵权请联系 cloudcommunity@tencent 删除illegalstateexceptionlineobjectpathstring本文标签: javalangIllegalStateException Expected BEGIN
版权声明:本文标题:java.lang.IllegalStateException: Expected BEGIN 内容由林淑君副主任自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.xiehuijuan.com/baike/1754975307a1708906.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论