用Jackson反序列化JSON - 为什么JsonMappingException“没有合适的构造函数”? [英] Deserializing JSON with Jackson - Why JsonMappingException "No suitable constructor"?
问题描述
我在使用Jackson反序列化JSON字符串时遇到问题(但我将对象序列化为JSON没有问题)。
I have a problem deserializing a JSON string using Jackson (but I have no problem serializing an object to JSON).
下面我介绍我使用的类。当我接收一个JSON字符串(一个在其他地方序列化并通过webservice检索的ProtocolContainer)并想要反序列化它时,问题出现了:
Below I present the classes I use. The problem comes when I rececive a JSON-string (a ProtocolContainer that was serialized elsewhere and retrieved via webservice) and want to de-serialize it:
JSON-string:
JSON-string:
{DataPacketJSONString:null,DataPacketType:MyPackage.DataPackets.LoginRequestReply,MessageId:6604,SenderUsername: null,SubPacket:{__ type:LoginRequestReply:#MyPackage.DataPackets,Reason:错误的通行证或用户名,成功:false,用户名:User1}}
{"DataPacketJSONString":null,"DataPacketType":"MyPackage.DataPackets.LoginRequestReply","MessageId":6604,"SenderUsername":null,"SubPacket":{"__type":"LoginRequestReply:#MyPackage.DataPackets","Reason":"Wrong pass or username","Success":false,"Username":"User1"}}
我尝试反序列化如下:
ProtocolContainer ret = ProtocolContainer.Create(jsonString);
以及在ProtocolContainer中执行的代码如下所示。例外:
and the code that executes in ProtocolContainer can be seen below. The exception:
org.codehaus.jackson.map.JsonMappingException:找不到合适的构造函数
类型[simple type,class
MyPackage.ProtocolContainer]:不能
从JSON对象实例化(需要添加/启用类型信息吗?)
at [来源:java.io.StringReader@4059dcb0; line:1,column:2]
org.codehaus.jackson.map.JsonMappingException: No suitable constructor found for type [simple type, class MyPackage.ProtocolContainer]: can not instantiate from JSON object (need to add/enable type information?) at [Source: java.io.StringReader@4059dcb0; line: 1, column: 2]
我真的很感激这里的一些输入=)Thx!
ProtocolContainer.java - 封装我的SubPackets的容器类:
import java.io.IOException;
import org.codehaus.jackson.JsonGenerationException;
import org.codehaus.jackson.JsonParseException;
import org.codehaus.jackson.map.JsonMappingException;
import org.codehaus.jackson.map.ObjectMapper;
import MyPackage.DataPackets.*;
public class ProtocolContainer
{
public String SenderUsername;
public String DataPacketType;
public long MessageId;
public String DataPacketJSONString;
public DataPacket SubPacket;
public ProtocolContainer(DataPacket dp)
{
DataPacketType = dp.getClass().toString().substring(6);
SubPacket = dp;
}
public String toJSON()
{
try {
if (SubPacket != null)
this.DataPacketJSONString = ProtocolContainer.mapper.writeValueAsString(SubPacket);
return ProtocolContainer.mapper.writeValueAsString(this);
} catch (JsonGenerationException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (JsonMappingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}
public static ObjectMapper mapper = new ObjectMapper();
public static ProtocolContainer Create(String jsonString)
{
ProtocolContainer pc = null;
try {
pc = mapper.readValue(jsonString, ProtocolContainer.class); // error here!
} catch (JsonParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (JsonMappingException e) {
// TODO Auto-generated catch block
e.printStackTrace(); // Exception when deserializing
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try
{
if (pc != null && pc.DataPacketType == "LoginRequest")
pc.SubPacket = mapper.readValue(jsonString, LoginRequest.class);
}
catch (JsonParseException e)
{
e.printStackTrace();
}
catch (JsonMappingException e)
{
e.printStackTrace();
}
catch (IOException e)
{
e.printStackTrace();
}
return pc;
}
}
DataPacket.java - 所有人的超类我的datapackets
public class DataPacket
{
}
LoginRequestReply.java - 一个DataPacket
package MyPackage.DataPackets;
import MyPackage.DataPacket;
public class LoginRequestReply extends DataPacket
{
public boolean LoginOK;
public int UserId;
}
推荐答案
错误消息说明了一切,您的ProtocolContainer没有默认构造函数,因此Jackson无法创建它的实例。 (因为创建ProtocolContainer的唯一当前方法是传入DataPacket。)
The error messages says it all, your ProtocolContainer does not have a default constructor so Jackson is unable to create an instance of it. (Since the only current way of creating a ProtocolContainer is by passing in a DataPacket.)
这篇关于用Jackson反序列化JSON - 为什么JsonMappingException“没有合适的构造函数”?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!