3

我正在用spring做Java项目。所以我正在使用库来转换以获取格式。JacksonJSON

我的java类将是,

public class ChatInteraction extends Interaction{

    private int ticketId;
    private String name;

    private String interactionType ;
    private LinkedList<InteractionInfo> interactions;

    public ChatInteraction(Message response) {
        super(response);
        interactions = new LinkedList<InteractionInfo>();
    }


    public int getTicketId() {
        return ticketId;
    }

    public void setTicketId(int ticketId) {
        this.ticketId = ticketId;
        System.out.println("Ticket Id for Interaction : "+this.ticketId);
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
        System.out.println("Name for Interaction : "+this.name);
    }


    public LinkedList<InteractionInfo> getInteractions() {
        return interactions;
    }

    public String getInteractionType() {
        return interactionType;
    }

    public void setInteractionType(String interactionType) {
        this.interactionType = interactionType;
    }


    public void addInteraction(InteractionInfo interaction) {
        this.interactions.add(interaction);
    }


    public void accept(int proxyId,String intxnId,int ticketId){

        RequestAccept reqAccept = RequestAccept.create();
        reqAccept.setProxyClientId(proxyId);
        reqAccept.setInteractionId(intxnId);
        reqAccept.setTicketId(ticketId);

        System.out.println("New Chat RequestAccept Request Object ::: "+reqAccept.toString());

        try{
            if(intxnProtocol.getState() == ChannelState.Opened){

                Message response = intxnProtocol.request(reqAccept);

                System.out.println("New Chat RequestAccept Response ::: "+response.toString());

                if(response != null ){

                    if( response.messageId() == EventAck.ID){
                        System.out.println("Accept new chat  success !");
                        //EventAccepted accept = (EventAccepted)response;
                        //return "New chat Interaction accepted";
                    }else if(response.messageId() == EventError.ID){
                        System.out.println("Accept new chat Failed !");
                        //return "New chat Interaction rejected";
                    }
                }

            }else{
                System.out.println("RequestAccept failure due to Interaction protocol error  !"); 
            }


        }catch(Exception acceptExcpetion){
            acceptExcpetion.printStackTrace();
        }

    }


    public void join(String sessionId, String subject) {

        RequestJoin join = RequestJoin.create();
        join.setMessageText(MessageText.create(""));
        join.setQueueKey("Resources:"); //Add the chat-inbound-key in multimedia of the optional tab values of the softphone application in CME
        join.setSessionId(sessionId);
        join.setVisibility(Visibility.All);
        join.setSubject(subject);
        KeyValueCollection kvc = new KeyValueCollection();
        join.setUserData(kvc);

        System.out.println("Join Request Object ::: "+join.toString());

        try {

            if(basicProtocol != null && basicProtocol.getState() == ChannelState.Opened){
                Message response = basicProtocol.request(join);

                if(response != null){

                    System.out.println("RequestJoin response ::: "+response);

                    if (response.messageId() == EventSessionInfo.ID) {
                        System.out.println("Join Request success !");
                    }else{
                        System.out.println("Join Request Failed !");
                    }
                }
            }else{
                System.out.println("BasicChat protocol Error !");
                //return "BasicChat protocol Error !";
            }
        } catch (ProtocolException e) {
            e.printStackTrace();
        } catch (IllegalStateException e) {
            e.printStackTrace();
        }

    }


}

我只需要以 JSON 格式获取此类and属性interactionTypeinteractions例如,

 {"interactionType":"invite","interactions" : [{"xx":"XX","yy":"YY"},{"xx":"XX","yy":"YY"}]} 

笔记 :

  1. 我不需要这个类的其他属性。

  2. 交互属性也没有SETTER。而不是我有addInteractions()方法。这会影响JSON转换的任何行为吗?

  3. 我还有其他一些方法,例如accept(...)Join(...)

  4. 我正在使用jackson-all-1.9.0.jar

4

3 回答 3

4

您可以注释不需要的字段@JsonIgnore- 请参阅 Jackson 的注释手册。这就是它的样子,使用您的代码:

public class ChatInteraction extends Interaction{
    @JsonIgnore
    private int ticketId;
    @JsonIgnore
    private String name;

    private String interactionType ;
    private LinkedList<InteractionInfo> interactions;
于 2013-09-18T15:56:23.590 回答
3

您可以通过使用可在类级别@JsonIgnoreProperties使用的注释来实现此目的。

来自JavaDoc

可用于抑制属性序列化(序列化期间)或忽略 JSON 属性读取处理(反序列化期间)的注释。

例子:

 // to prevent specified fields from being serialized or deserialized
 // (i.e. not include in JSON output; or being set even if they were included)
 \@JsonIgnoreProperties({ "internalId", "secretKey" })

例如,在您的情况下:

@JsonIgnoreProperties({ "ticketId", "name" })
public class ChatInteraction extends Interaction{
    ....
}
于 2013-09-18T15:57:41.170 回答
2

最后我在线程中的其他答案和stackoverflow中的类似答案得到了解决方案,

  1. 我在fvu建议的子类和超类@JsonIgnore中的不需要的字段中标记了。

  2. myObjectMapper.setVisibility(JsonMethod.FIELD, Visibility.ANY);在我的objectMapper中使用了其他线程中建议的,例如,

    ObjectMapper mapp = new ObjectMapper();
    mapp.setVisibility(JsonMethod.FIELD, Visibility.ANY);
    try {
        json = mapp.writeValueAsString(info);
        info.clear();
        System.out.println("Chat Info in JSON String is :::>  "+json);
    } catch (Exception e) {
        e.printStackTrace();
    }
    
于 2013-09-19T07:14:16.920 回答