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发布者:admin,转转请注明出处:http://www.yc00.com/web/1754975292a5223516.html
评论列表(0条)