- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
美好的一天!
我连续几天都在为一个错误打赌,思绪奔腾。有一个正在序列化 GSON 的对象的 ArrayList 并存储在 MySQL 的 TEXT 中。
这是反序列化的函数:
private void listenBroadcasts() {
try {
while (!shutdownThreads)
{
try
{
ResultSet rs = sql.select("SELECT * FROM messages WHERE time > (now() - INTERVAL 2 SECOND)");
while (rs.next()) {
String subject = rs.getString("subject");
String jsonString = rs.getString("json");
String className = rs.getString("class");
int id = rs.getInt("id");
if (jsonSubjects.contains(subject)
&& !StringUtils.isEmpty(className)
&& !StringUtils.isEmpty(jsonString)
&& id != myLastID) {
LOGGER.info("JSON: "+jsonString);
final Class clazz = Class.forName(className);
Object object = gson.fromJson(jsonString, clazz);
JsonEnvelope envelope = new JsonEnvelope(rs.getString("sender"), null, subject, object);
LOGGER.debug("Received message: "
+ " sender: " + envelope.getSenderInstance()
+ " receiver: " + envelope.getReceiverInstance()
+ " to subject: "
+ envelope.getSubject() + " (" + envelope.getClass().getSimpleName() + ")");
myLastID = id;
jsonReceiveQueue.put(envelope);
}
}
rs.close();
Thread.sleep(1000L);
} catch (final SQLException e) {
LOGGER.debug("Error receiving JSON message ", e);
} catch (final ClassNotFoundException e) {
LOGGER.error("Error deserializing JSON message ", e);
} catch (final InterruptedException e)
{
//LOGGER.error("Interrupt erro in JSOM message ", e);
}
}
} catch (Exception e) {
LOGGER.error("Error JsonMessaging: "+e.toString());
e.printStackTrace();
}
}
发送单个对象时,一切正常进行 - 对象被序列化并且 desalizuetsya 正常。一旦数组被传递 - 它通常会被序列化,而 desearilizatsii 会出现错误。
信息(messaging.JsonMessaging)- JSON:
{"devices":["{
"name": "not set",
"node": 1,
"zone": 0,
"type": "Portable Remote Controller",
"internalType": "controller",
"manufName": "undefined",
"productName": "",
"uuid": "413e3621-f4eb-4469-b986-78699455541c",
"status": "Listening",
"source": "zwave",
"internalName": "zwave/controller/1",
"values": {
"Basic": "0"
}
}","{
"name": "not set",
"node": 4,
"zone": 0,
"type": "Routing Binary Sensor",
"internalType": "binarysensor",
"manufName": "undefined",
"productName": "",
"uuid": "0dd6743c-6250-4cda-ab5c-871844090642",
"status": "Sleeping",
"source": "zwave",
"internalName": "zwave/binarysensor/4",
"values": {
"Sensor": "false",
"Wake-up Interval": "3600"
}
}","{
"name": "not set",
"node": 3,
"zone": 0,
"type": "Multilevel Power Switch",
"internalType": "dimmer",
"manufName": "FIBARO System",
"productName": "FGD211 Universal Dimmer 500W",
"uuid": "c00228e5-45cc-4ac4-8371-9ac179857926",
"status": "Listening",
"source": "zwave",
"internalName": "zwave/dimmer/3",
"values": {
"Timeout": "0",
"1. Enable/Disable ALL ON/OFF": "null",
"20. ADVANCED Impulse length": "110",
"Frame Count": "0",
"17. 3-way switch": "null",
"8. Dimming step at automatic control": "1",
"Report": "null",
"Protocol Version": "3.34",
"Application Version": "1.06",
"Test": "null",
"13. Minimum dimmer level control": "2",
"10. Time of AUTOMATIC moving between the extreme dimming values": "1",
"Test Powerlevel": "null",
"Start Level": "0",
"Bright": "null",
"Library Version": "3",
"12. Maximum dimmer level control": "99",
"16. Saving state before power faillure": "null",
"Test Node": "0",
"39. ALARM FLASHING alarm time": "600",
"14. Inputs Button/Switch configuration": "null",
"Dim": "null",
"18. Synchronizing light level for associated devices": "null",
"7. Control key #2 behaviour": "null",
"11. Dimming step at manual control": "1",
"15. Double click option": "null",
"30. Alarm": "null",
"9. Time of MANUALLY moving between the extreme dimming values": "5",
"Powerlevel": "null",
"19. Change [On-Off] bi-stable keys": "null",
"Test Status": "null",
"Ignore Start Level": "true",
"Level": "99",
"Set Powerlevel": "null",
"Switch All": "null",
"6. Separation of association sending (key 1)": "null",
"Acked Frames": "64"
}
}","{
"name": "not set",
"node": 2,
"zone": 0,
"type": "Routing Alarm Sensor",
"internalType": "alarmsensor",
"manufName": "undefined",
"productName": "",
"uuid": "1588357d-fdf0-4813-b447-10158a72be80",
"status": "Sleeping",
"source": "zwave",
"internalName": "zwave/alarmsensor/2",
"values": {
"Application Version": "Unknown",
"Alarm Type": "0",
"Battery Level": "100",
"Wake-up Interval": "3600",
"Protocol Version": "Unknown",
"Alarm Level": "0",
"Library Version": "Unknown"
}
}"]}
2013年11月26日12:15:04
ERROR (messaging.JsonMessaging) - Error JsonMessaging: com.google.gson.JsonSyntaxException: com.google.gson.stream.MalformedJsonException: Unterminated array at line 2 column 5
com.google.gson.JsonSyntaxException: com.google.gson.stream.MalformedJsonException: Unterminated array at line 2 column 5
at com.google.gson.Gson.fromJson(Gson.java:818)
at com.google.gson.Gson.fromJson(Gson.java:768)
at com.google.gson.Gson.fromJson(Gson.java:717)
at com.google.gson.Gson.fromJson(Gson.java:689)
at ru.iris.common.messaging.JsonMessaging.listenBroadcasts(JsonMessaging.java:164)
at ru.iris.common.messaging.JsonMessaging.access$000(JsonMessaging.java:37)
at ru.iris.common.messaging.JsonMessaging$1.run(JsonMessaging.java:63)
at java.lang.Thread.run(Thread.java:724)
Caused by: com.google.gson.stream.MalformedJsonException: Unterminated array at line 2 column 5
at com.google.gson.stream.JsonReader.syntaxError(JsonReader.java:1505)
at com.google.gson.stream.JsonReader.doPeek(JsonReader.java:465)
at com.google.gson.stream.JsonReader.hasNext(JsonReader.java:403)
at com.google.gson.internal.bind.CollectionTypeAdapterFactory$Adapter.read(CollectionTypeAdapterFactory.java:80)
at com.google.gson.internal.bind.CollectionTypeAdapterFactory$Adapter.read(CollectionTypeAdapterFactory.java:60)
at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$1.read(ReflectiveTypeAdapterFactory.java:93)
at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$Adapter.read(ReflectiveTypeAdapterFactory.java:172)
at com.google.gson.Gson.fromJson(Gson.java:803)
... 7 more
最佳答案
您遇到的 gson 错误是正确的,因为您提供的 JSON 无效。
使用类似 http://jsonlint.com/ 的工具检查您的 JSON。
我已通过格式化程序运行您提供的 JSON 并纠正了问题:
{
"devices": [
{
"name": "not set",
"node": 1,
"zone": 0,
"type": "Portable Remote Controller",
"internalType": "controller",
"manufName": "undefined",
"productName": "",
"uuid": "413e3621-f4eb-4469-b986-78699455541c",
"status": "Listening",
"source": "zwave",
"internalName": "zwave/controller/1",
"values": {
"Basic": "0"
}
},
{
"name": "not set",
"node": 4,
"zone": 0,
"type": "Routing Binary Sensor",
"internalType": "binarysensor",
"manufName": "undefined",
"productName": "",
"uuid": "0dd6743c-6250-4cda-ab5c-871844090642",
"status": "Sleeping",
"source": "zwave",
"internalName": "zwave/binarysensor/4",
"values": {
"Sensor": "false",
"Wake-up Interval": "3600"
}
},
{
"name": "not set",
"node": 3,
"zone": 0,
"type": "Multilevel Power Switch",
"internalType": "dimmer",
"manufName": "FIBARO System",
"productName": "FGD211 Universal Dimmer 500W",
"uuid": "c00228e5-45cc-4ac4-8371-9ac179857926",
"status": "Listening",
"source": "zwave",
"internalName": "zwave/dimmer/3",
"values": {
"Timeout": "0",
"1. Enable/Disable ALL ON/OFF": "null",
"20. ADVANCED Impulse length": "110",
"Frame Count": "0",
"17. 3-way switch": "null",
"8. Dimming step at automatic control": "1",
"Report": "null",
"Protocol Version": "3.34",
"Application Version": "1.06",
"Test": "null",
"13. Minimum dimmer level control": "2",
"10. Time of AUTOMATIC moving between the extreme dimming values": "1",
"Test Powerlevel": "null",
"Start Level": "0",
"Bright": "null",
"Library Version": "3",
"12. Maximum dimmer level control": "99",
"16. Saving state before power faillure": "null",
"Test Node": "0",
"39. ALARM FLASHING alarm time": "600",
"14. Inputs Button/Switch configuration": "null",
"Dim": "null",
"18. Synchronizing light level for associated devices": "null",
"7. Control key #2 behaviour": "null",
"11. Dimming step at manual control": "1",
"15. Double click option": "null",
"30. Alarm": "null",
"9. Time of MANUALLY moving between the extreme dimming values": "5",
"Powerlevel": "null",
"19. Change [On-Off] bi-stable keys": "null",
"Test Status": "null",
"Ignore Start Level": "true",
"Level": "99",
"Set Powerlevel": "null",
"Switch All": "null",
"6. Separation of association sending (key 1)": "null",
"Acked Frames": "64"
}
},
{
"name": "not set",
"node": 2,
"zone": 0,
"type": "Routing Alarm Sensor",
"internalType": "alarmsensor",
"manufName": "undefined",
"productName": "",
"uuid": "1588357d-fdf0-4813-b447-10158a72be80",
"status": "Sleeping",
"source": "zwave",
"internalName": "zwave/alarmsensor/2",
"values": {
"Application Version": "Unknown",
"Alarm Type": "0",
"Battery Level": "100",
"Wake-up Interval": "3600",
"Protocol Version": "Unknown",
"Alarm Level": "0",
"Library Version": "Unknown"
}
}
] }
关于java - GSON和反序列化错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20212232/
我目前正在对一个 mmorpg 的二进制网络协议(protocol)进行逆向工程。我正在用 java 实现该协议(protocol)。 对于每个数据包类型,我将创建一个表示二进制数据的类。 例如,聊天
我正在尝试围绕现有类编写半透明包装器,我希望它能够模仿其他类的序列化。 例如,给定以下类: class Foo { [JsonConverter(CustomConverter)] s
是否有使用 Jackson 序列化和反序列化枚举集的简单方法? private enum Type { YES, NO } @JacksonXmlProperty(localName = "t
我很想知道当我们反序列化一个对象时会发生什么。 例如,如果我的类对象由许多其他对象组成,对象创建过程如何在反序列化过程中发生 最佳答案 对象是用默认的初始化字段创建的,然后用从串行流中获取的属性值填充
我正在尝试序列化和反序列化(使用 QDataStream 但这与这里无关)一个 enum class变量: enum class Type : char { Trivial, Comp
我不确定这到底有什么问题...它不会为我编译,我将它从 c 翻译成 C++(或尝试)...是的,我是初学者。谢谢! #include #include using namespace std; i
我遇到的问题与此处描述的问题非常相似:Combining type and field serializers case class(id: Option[UUID], otherValue:Stri
我们知道base中的apply()可以对数组的边距应用一个函数,边距应该是行或列。我想将边距扩大到“对角线” 和“反对角线”。结构看起来像 diagApply <- function(x, FUN,
我找到了 JSON serialization and deserialization to objects in Flutter 的例子但是如何使用像这样的人员列表来做到这一点: [ {
我有一个相当大的terms聚合结果,这些结果被加载到下拉列表中以提供filter功能。 可以说,我的下拉列表中有4000多种动物。我的另一个下拉列表有4种动物颜色。 例, animal --> ["d
我需要将 C# (.NET Framework 4.5.2) 中的一个类与 XML 序列化(反序列化),该类具有 string 的字典属性。键和 string[]数组值。我正在使用 Serializa
[已解决]应用给定的解决方案,效果很好! 程序的目的:在用户打开和关闭程序时保存/重新加载以前的数据。 我曾经用一个对象(obj)成功(反)序列化,现在我有两个不同类的不同对象。 我试图通过查看其他帖
问题 假设我有一个代表某事或其他的枚举: public enum ResultState { Found, Deleted, NotFound } 在我的序列化 json 中,
是否有取消 JSON 字符串的功能?我猜它不会内置到 JQuery 中,但它可以通过编写一个操纵字符串的脚本来实现吗?我在下面遇到了这个问题。 我正在使用 NYTimes API,但它不支持 JSON
对于这个问题,假设当对象完全写入流并成功读出时,或者当对象部分写入流并且读回对象时发生异常时,序列化/反序列化是原子的。假设写操作可能无法成功完成,例如因为停电了。 在Serializable的描述中
有谁知道时序检查是否仍在检测虚拟环境?我尝试使用 rdtsc 指令来获取 cpu 周期并比较真实 linux 机器和在 virtualbox 上运行的 linux 之间的结果。但结果似乎不稳定。有时,
我正在对一个(外部给定的)XML 文件进行操作,该文件具有以下形式的元素 10 20 30 40 50 60 70 80 我知道如何将属性作为属性处理(通过使用 [XmlAttri
我有一个通用的序列化器和反序列化器,用于通过网络连接发送的消息: public static async Task SerializeObject(Object obj) {
我正在考虑将当前基于 WCF 的应用程序迁移到 protobuf-net.Grpc。这似乎是可行的,但是我无法在不包含所有具有 [ProtoInclude] 属性的派生类的情况下使(DTO 类)基类的
我正在尝试将一些数据保存到文件中,但文件保存到的目录不正确。 using (StreamWriter sw = new StreamWriter(dir + "\\temp" + x + ".txt"
我是一名优秀的程序员,十分优秀!