我有一個問題反序列化使用傑克遜的JSON字符串(但我沒有問題序列化對象到JSON)。用Jackson反序列化JSON - 爲什麼JsonMappingException「沒有合適的構造函數」?
下面我介紹我使用的類。問題是當我rececive一個JSON字符串(這是其他地方的序列化,並通過Web服務檢索的ProtocolContainer),並希望反序列化:
JSON字符串:
{"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中執行的代碼可以在下面看到。例外:
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: [email protected]; line: 1, column: 2]
我真的很感激這裏的一些輸入=)Thx!
ProtocolContainer.java - 封裝我的 「子包」 容器類:
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 - a DataPacket
package MyPackage.DataPackets;
import MyPackage.DataPacket;
public class LoginRequestReply extends DataPacket
{
public boolean LoginOK;
public int UserId;
}
後續:經過一些搞亂之後,我沒有收到以下錯誤: * JsonMappingException:無法從JSON字符串實例化[simple type,class MyPackage.ProtocolContainer]的值;沒有單個字符串的構造函數/工廠方法*。如果我添加一個構造函數接受一個字符串,那麼沒有錯誤,但對象是「空的」......我不認爲應該有必要在構造函數中添加實現支持。 **我應該如何解決這個問題?** – Ted