java.lang.IllegalStateException: Expected BEGIN_OBJECT but was STRING at line 1 column 2 path $

2024-10-09 11:05:57 浏览数 (1)

java.lang.IllegalStateException: Expected BEGIN_OBJECT but was STRING at line 1 column 2 path $

代码语言:javascript复制
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.apache.commons.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":"https://www.url.test"},"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复制
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;
    }
}

0 人点赞