首页 / JSON / 将java类数据转换为JSON格式?
将java类数据转换为JSON格式?
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了将java类数据转换为JSON格式?,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含3701字,纯文字阅读大概需要6分钟。
内容图文
我正在使用spring进行Java项目.所以我使用Jackson库转换为获取JSON格式.
我的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格式获取此类的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<InteractionInfo> interactions;
内容总结
以上是互联网集市为您收集整理的将java类数据转换为JSON格式?全部内容,希望文章能够帮你解决将java类数据转换为JSON格式?所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。