com.alibaba.fastjson.JSONException********com.mysql.jdbc.JDBC4DatabaseMetaData, fieldName : metaData

com.alibaba.fastjson.JSONException: write javaBean error, fastjson version 1.2.56, class org.springframework.boot.web.servlet.context.AnnotationConfigServletWebServerApplicationContext, fieldName : applicationContext, write javaBean error, fastjson version 1.2.56, class org.springframework.beans.factory.support.DefaultListableBeanFactory, fieldName : autowireCapableBeanFactory, write javaBean error, fastjson version 1.2.56, class org.apache.ibatis.session.Configuration, fieldName : configuration, write javaBean error, fastjson version 1.2.56, class com.mysql.jdbc.JDBC4DatabaseMetaData, fieldName : metaData, Positioned Update not supported.
	at com.alibaba.fastjson.serializer.JavaBeanSerializer.write(JavaBeanSerializer.java:523) ~[fastjson-1.2.56.jar:na]
	at com.alibaba.fastjson.serializer.JavaBeanSerializer.write(JavaBeanSerializer.java:160) ~[fastjson-1.2.56.jar:na]
	at com.alibaba.fastjson.serializer.JSONSerializer.writeWithFieldName(JSONSerializer.java:314) ~[fastjson-1.2.56.jar:na]
	at com.alibaba.fastjson.serializer.ASMSerializer_60_ContextClosedEvent.write(Unknown Source) ~[na:na]
	at com.alibaba.fastjson.serializer.JSONSerializer.write(JSONSerializer.java:285) ~[fastjson-1.2.56.jar:na]
	at com.alibaba.fastjson.JSON.toJSONString(JSON.java:731) ~[fastjson-1.2.56.jar:na]
	at com.alibaba.fastjson.JSON.toJSONString(JSON.java:669) ~[fastjson-1.2.56.jar:na]
	at com.alibaba.fastjson.JSON.toJSONString(JSON.java:634) ~[fastjson-1.2.56.jar:na]
	at com.honeypeng.event.CustomizeEventListener.onApplicationEvent(CustomizeEventListener.java:16) ~[classes/:na]
	at org.springframework.context.event.SimpleApplicationEventMulticaster.doInvokeListener(SimpleApplicationEventMulticaster.java:172) ~[spring-context-5.0.9.RELEASE.jar:5.0.9.RELEASE]
	at org.springframework.context.event.SimpleApplicationEventMulticaster.invokeListener(SimpleApplicationEventMulticaster.java:165) ~[spring-context-5.0.9.RELEASE.jar:5.0.9.RELEASE]
	at org.springframework.context.event.SimpleApplicationEventMulticaster.multicastEvent(SimpleApplicationEventMulticaster.java:139) ~[spring-context-5.0.9.RELEASE.jar:5.0.9.RELEASE]
	at org.springframework.context.support.AbstractApplicationContext.publishEvent(AbstractApplicationContext.java:400) [spring-context-5.0.9.RELEASE.jar:5.0.9.RELEASE]
	at org.springframework.context.support.AbstractApplicationContext.publishEvent(AbstractApplicationContext.java:354) [spring-context-5.0.9.RELEASE.jar:5.0.9.RELEASE]
	at org.springframework.context.support.AbstractApplicationContext.doClose(AbstractApplicationContext.java:1000) [spring-context-5.0.9.RELEASE.jar:5.0.9.RELEASE]
	at org.springframework.context.support.AbstractApplicationContext.close(AbstractApplicationContext.java:967) [spring-context-5.0.9.RELEASE.jar:5.0.9.RELEASE]
	at org.springframework.boot.SpringApplication.handleRunFailure(SpringApplication.java:839) [spring-boot-2.0.5.RELEASE.jar:2.0.5.RELEASE]
	at org.springframework.boot.SpringApplication.run(SpringApplication.java:344) [spring-boot-2.0.5.RELEASE.jar:2.0.5.RELEASE]
	at org.springframework.boot.SpringApplication.run(SpringApplication.java:1277) [spring-boot-2.0.5.RELEASE.jar:2.0.5.RELEASE]
	at org.springframework.boot.SpringApplication.run(SpringApplication.java:1265) [spring-boot-2.0.5.RELEASE.jar:2.0.5.RELEASE]
	at com.honeypeng.SpringbootfirstApplication.main(SpringbootfirstApplication.java:15) [classes/:na]
	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:1.8.0_131]
	at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[na:1.8.0_131]
	at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:1.8.0_131]
	at java.lang.reflect.Method.invoke(Method.java:498) ~[na:1.8.0_131]
	at org.springframework.boot.devtools.restart.RestartLauncher.run(RestartLauncher.java:49) [spring-boot-devtools-2.0.5.RELEASE.jar:2.0.5.RELEASE]

自学ApplicationEventPublisherAware广播消息的时候出现一个错误,具体原因到时候有空深究一下,知道的也可以留言一下,但是问题就是下面代码使用了JSON.toString方法,去掉就行了,或者使用toString方法.

### 问题原因分析 `com.alibaba.fastjson.JSONException: field null expect '[', but {` 或 `expect '[', but string` 这类异常通常出现在使用 `fastjson` 将 JSON 字符串转换为 Java 集合(如 `List`)时,JSON 字符串的格式与预期不符。具体原因包括: 1. **输入的 JSON 字符串不是数组格式** `JSONArray.parseArray()` 方法期望输入是一个 JSON 数组(以 `[` 开头,以 `]` 结尾),但实际传入的是一个 JSON 对象(以 `{` 开头,以 `}` 结尾)或字符串内容。例如: ```json {"id": 1, "name": "test"} // 错误:这是一个对象,不是数组 ``` 如果尝试将其解析为 `List<T>`,就会抛出 `expect '[', but {` 的异常[^3]。 2. **输入的字符串格式不规范** 某些情况下,输入的字符串可能包含非法字符或格式错误,例如双引号嵌套、空字符串、未转义的特殊字符等。例如: ```json "\"\"" ``` 这会导致解析失败并抛出 `expect '[', but string` 的异常[^4]。 3. **输入字符串为空或 `null`** 如果传入的字符串是 `null` 或空字符串,解析器无法识别其结构,也会导致异常。 --- ### 解决方案 1. **确保输入的 JSON 字符串是数组格式** 检查输入字符串是否符合 JSON 数组格式。例如: ```json [{"id": 1, "name": "test1"}, {"id": 2, "name": "test2"}] ``` 如果输入是对象格式,应使用 `JSON.parseObject()` 而不是 `parseArray()` 来解析。 2. **验证输入字符串的有效性** 在调用 `parseArray()` 之前,检查字符串是否为空或格式错误。例如: ```java if (!StringUtils.isEmpty(tasksInfos)) { List<String> tasks = JSONArray.parseArray(tasksInfos, String.class); } ``` 这可以避免因空字符串或无效格式导致的异常[^4]。 3. **处理不规范的数据** 如果输入数据中存在双引号或其他非法字符,可以进行预处理。例如: ```java if (!StringUtils.isEmpty(url) && !"\"\"".equals(url)) { List<FileDto> fileDtos = JSON.parseArray(url, FileDto.class); if (fileDtos != null) { helloWorld.setFileDtos(fileDtos); } } ``` 这种方式可以临时规避数据不规范的问题。 4. **使用 try-catch 捕获异常** 在解析过程中,使用异常捕获机制处理不可预知的错误。例如: ```java try { List<String> tasks = JSONArray.parseArray(tasksInfos, String.class); } catch (JSONException e) { // 处理解析失败的情况 e.printStackTrace(); } ``` 5. **调试和日志记录** 在开发和测试阶段,建议记录输入的 JSON 字符串,以便排查格式问题。例如: ```java System.out.println("Input JSON String: " + tasksInfos); ``` --- ### 示例代码 以下是一个完整的示例,展示如何安全地解析 JSON 字符串为 `List`: ```java import com.alibaba.fastjson.JSON; import com.alibaba.fastjson.JSONArray; import com.alibaba.fastjson.JSONException; import org.apache.commons.lang3.StringUtils; import java.util.List; public class JsonParseExample { public static void main(String[] args) { String tasksInfos = "[{\"id\": 1, \"name\": \"test1\"}, {\"id\": 2, \"name\": \"test2\"}]"; if (!StringUtils.isEmpty(tasksInfos)) { try { List<Task> tasks = JSONArray.parseArray(tasksInfos, Task.class); for (Task task : tasks) { System.out.println(task); } } catch (JSONException e) { System.err.println("JSON 解析失败: " + e.getMessage()); } } else { System.out.println("输入的 JSON 字符串为空"); } } static class Task { private int id; private String name; @Override public String toString() { return "Task{" + "id=" + id + ", name='" + name + '\'' + '}'; } } } ``` --- ### 总结 `com.alibaba.fastjson.JSONException: field null expect '[', but {` 或 `expect '[', but string` 的根本原因是输入的 JSON 字符串格式与解析方法不匹配。解决方法包括验证输入格式、预处理非法数据、使用异常捕获机制以及记录调试信息。 ---
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值