Expected BEGIN_OBJECT but was STRING at line 1 column 2 path $
syntax error, expect {, actual string, pos 0, fastjson-version 1.2.62
syntax error, expect {, actual string, pos 0, fastjson-version 1.2.62
以上的报错都是Json字符串格式错误,比如缺少{},比如两头多了”“,或者转义字符\",比如在映射的对象错误,本来是List,但是写成了String,或者是单个对象,而不是List,都会报错。
1.在本地运行main方法排查解决
2.如果是多了”“,或者是内部多了转义字符\",则使用简单的替换方法。
比如完整的Json字符串格式如下:
{"err_no":"0","err_tips":"success","data":{"refund_list":[{"refund_id":"ots7240038790928135","out_refund_no":"29160417157000010","refund_total_amount":1090,"refund_status":"PROCESSING","refund_at":0,"message":"","order_id":"ots72396406088135","item_order_detail":[{"item_order_id":"ots723969678768135","refund_amount":1090}],"merchant_audit_detail":"{\"deny_message\":\"\",\"need_refund_audit\":1,\"refund_audit_deadline\":16859682,\"audit_status\":\"TOAUDIT\"}","create_at":1685701000,"refund_source":2}],"refund_id":"ots72400387928135","out_refund_no":"2916041715168561000010","refund_total_amount":1090,"refund_status":"PROCESSING","refund_at":0,"message":"","order_id":"ots7239640656088135","item_order_detail":[{"item_order_id":"ots72396406578768135","refund_amount":1090}],"merchant_audit_detail":{"need_refund_audit":1,"refund_audit_deadline":168596282,"audit_status":"TOAUDIT","deny_message":""}}}
package com.example.core.mydemo.json2; import com.alibaba.fastjson.JSON; import com.alibaba.fastjson.JSONObject; import com.google.gson.reflect.TypeToken; public class JSONTest { public static void main(String[] args) { String msg = "\"orderNo\":\"50133310603299\",\"riskAccidentStatus\":\"1\",\"operator\":\"admin\""; //Expected BEGIN_OBJECT but was STRING at line 1 column 2 path $ //缺少的是{} try { MQReceiveCLevelRiskControlDTO cLevelRiskControlDTO = GsonUtils.convert(msg, new TypeToken<MQReceiveCLevelRiskControlDTO>() {}); System.out.println("gson1=" + GsonUtils.toJson(cLevelRiskControlDTO)); }catch (Exception e) { MQReceiveCLevelRiskControlDTO cLevelRiskControlDTO = GsonUtils.convert("{"+msg+"}", new TypeToken<MQReceiveCLevelRiskControlDTO>() {}); System.out.println("gson2=" + GsonUtils.toJson(cLevelRiskControlDTO)); } //syntax error, expect {, actual string, pos 0, fastjson-version 1.2.62 // MQReceiveCLevelRiskControlDTO cLevelRiskControlDTO2 = JSON.parseObject(msg, MQReceiveCLevelRiskControlDTO.class); // System.out.println("gson2=" + GsonUtils.toJson(cLevelRiskControlDTO2)); //syntax error, expect {, actual string, pos 0, fastjson-version 1.2.62 // MQReceiveCLevelRiskControlDTO cLevelRiskControlDTO3 = JSONObject.parseObject(msg, MQReceiveCLevelRiskControlDTO.class); // System.out.println("gson3=" + GsonUtils.toJson(cLevelRiskControlDTO3)); } }