小编典典

用Jackson来反序列化JSON-为什么JsonMappingException“没有合适的构造函数”?

json

我在使用Jackson来反序列化JSON字符串时遇到问题(但是我没有问题将对象序列化为JSON)。

下面,我介绍我使用的类。当我接收到JSON字符串(已在其他地方进行序列化并通过Web服务检索的ProtocolContainer)并想反序列化时,就会出现问题:

JSON字符串:

{“ DataPacketJSONString”:null,“ DataPacketType”:“
MyPackage.DataPackets.LoginRequestReply”,“ MessageId”:6604,“
SenderUsername”:null,“ SubPacket”:{“ __ type”:“
LoginRequestReply:#MyPackage.DataPackets”,“原因”:“错误的密码或用户名”,“成功”:false,“用户名”:“
User1”}}

我尝试像这样反序列化:

ProtocolContainer ret = ProtocolContainer.Create(jsonString);

在ProtocolContainer中执行的代码如下所示。例外:

org.codehaus.jackson.map.JsonMappingException:在类型[简单类型,MyPackage.ProtocolContainer类]上找不到合适的构造函数:无法从JSON对象实例化(需要添加/启用类型信息?),位于[来源:java.io。
StringReader @ 4059dcb0; 行:1,列:2]

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-我所有数据包的超类

public class DataPacket 
{

}

LoginRequestReply.java-一个数据包

package MyPackage.DataPackets;

import MyPackage.DataPacket;

public class LoginRequestReply extends DataPacket
{
    public boolean LoginOK;
    public int UserId;
}

阅读 331

收藏
2020-07-27

共1个答案

小编典典

错误消息说明了一切,您的ProtocolContainer没有默认的构造函数,因此Jackson无法创建它的实例。(由于当前创建ProtocolContainer的唯一方法是传入DataPacket。)

2020-07-27