我是靠谱客的博主 等待狗,最近开发中收集的这篇文章主要介绍Java 将类转换成json_将java类数据转换为JSON格式?,觉得挺不错的,现在分享给大家,希望可以做个参考。

概述

我正在使用spring进行Java项目.所以我使用Jackson库转换为获取JSON格式.

我的java类将是,

public class ChatInteraction extends Interaction{

private int ticketId;

private String name;

private String interactionType ;

private LinkedList interactions;

public ChatInteraction(Message response) {

super(response);

interactions = new LinkedList();

}

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 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格式获取此类的interactionType和interact属性,如:

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

注意 :

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

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

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

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

解决方法:

您可以使用@JsonIgnore注释不需要的字段 – 请参阅Jackson的manual on annotations.这就是它的样子,使用您的代码:

public class ChatInteraction extends Interaction{

@JsonIgnore

private int ticketId;

@JsonIgnore

private String name;

private String interactionType ;

private LinkedList interactions;

标签:java,spring,json,jackson

来源: https://codeday.me/bug/20190723/1508596.html

最后

以上就是等待狗为你收集整理的Java 将类转换成json_将java类数据转换为JSON格式?的全部内容,希望文章能够帮你解决Java 将类转换成json_将java类数据转换为JSON格式?所遇到的程序开发问题。

如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(45)

评论列表共有 0 条评论

立即
投稿
返回
顶部