Java实现微信菜单json字符串拼接

278 篇文章 1 订阅
7 篇文章 0 订阅

Java实现微信菜单json字符串拼接

微信菜单拼接json字符串方法

 

>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>.

蕃薯耀 2016年3月31日 22:22:53 星期四

http://fanshuyao.iteye.com/

 

微信菜单json拼接方法:

 

 

private String getWxButtonJson(List<WxButton> wxButtonsParent){
		StringBuffer sb = new StringBuffer("{");
		sb.append("\"").append("button").append("\"").append(":").append("[");
		for (WxButton wxButton : wxButtonsParent) {
			sb.append("{");
			sb.append("\"").append("name").append("\"").append(":").append("\"").append(wxButton.getName()).append("\"").append(",");
			if(wxButton.getSub_buttons().size() > 0){
				sb.append("\"").append("sub_button").append("\"").append(":").append("[");
				StringBuffer subStringBuffer = new StringBuffer("");
				for (WxButton sub : wxButton.getSub_buttons()) {
					subStringBuffer.append("{");
					subStringBuffer.append("\"").append("name").append("\"").append(":").append("\"").append(sub.getName()).append("\"").append(",");
					subStringBuffer.append("\"").append("type").append("\"").append(":").append("\"").append(sub.getType().toLowerCase()).append("\"").append(",");
					if(ButtonType.CLICK.toString().equalsIgnoreCase(sub.getType())){
						subStringBuffer.append("\"").append("key").append("\"").append(":").append("\"").append(sub.getKey()).append("\"").append(",");
					}else{
						subStringBuffer.append("\"").append("url").append("\"").append(":").append("\"").append(sub.getUrl()).append("\"").append(",");
					}
					if(subStringBuffer.indexOf(",") > -1){
						subStringBuffer.deleteCharAt(subStringBuffer.lastIndexOf(","));
					}
					subStringBuffer.append("}").append(",");
				}
				if(subStringBuffer.indexOf(",") > -1){
					subStringBuffer.deleteCharAt(subStringBuffer.lastIndexOf(","));
				}
				sb.append(subStringBuffer).append("]");
			}else{
				sb.append("\"").append("type").append("\"").append(":").append("\"").append(wxButton.getType().toLowerCase()).append("\"").append(",");
				if(ButtonType.CLICK.toString().equalsIgnoreCase(wxButton.getType())){
					sb.append("\"").append("key").append("\"").append(":").append("\"").append(wxButton.getKey()).append("\"").append(",");
				}else{
					sb.append("\"").append("url").append("\"").append(":").append("\"").append(wxButton.getUrl()).append("\"").append(",");
				}
				if(sb.indexOf(",") > -1){
					sb.deleteCharAt(sb.lastIndexOf(","));
				}
			}
			sb.append("}").append(",");
		}
		if(sb.indexOf(",") > -1){
			sb.deleteCharAt(sb.lastIndexOf(","));
		}
		sb.append("]}");
		return sb.toString();
	}

 

 

WxButton 菜单按钮类:

 

 

package com.lqy.spring.iwx.bean.weixin;

import java.util.ArrayList;
import java.util.List;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.Transient;

import com.lqy.spring.iwx.bean.BaseBean;

@Entity(name="wx_button")
public class WxButton extends BaseBean {
	
	private static final long serialVersionUID = -8474453778646031334L;

	
	/**
	 * 菜单标题,不超过16个字节,子菜单不超过40个字节
	 * 一级菜单最多4个汉字,二级菜单最多7个汉字
	 */
	protected String name;
	/**
	 * 菜单的响应动作类型
	 */
	protected String type;
	/**
	 * 菜单KEY值,用于消息接口推送,不超过128字节
	 */
	protected String key;
	/**
	 * 网页链接,用户点击菜单可打开链接,不超过1024字节
	 */
	protected String url;
	/**
	 * 调用新增永久素材接口返回的合法media_id
	 */
	protected String media_id;
	/**
	 * 二级菜单数组,个数应为1~5个
	 */
	protected List<WxButton> sub_buttons = new ArrayList<WxButton>();
	
	protected WxButton wxButtonParent;
	
	@Column(length=20,nullable=false)
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	
	@Column(nullable=false)
	public String getType() {
		return type;
	}
	public void setType(String type) {
		this.type = type;
	}
	
	@Column(length=64,unique=true,name="`key`")
	public String getKey() {
		return key;
	}
	public void setKey(String key) {
		this.key = key;
	}
	
	@Column
	public String getUrl() {
		return url;
	}
	public void setUrl(String url) {
		this.url = url;
	}
	
	@Column(unique=true)
	public String getMedia_id() {
		return media_id;
	}
	public void setMedia_id(String media_id) {
		this.media_id = media_id;
	}
	
	@Transient
	public List<WxButton> getSub_buttons() {
		return sub_buttons;
	}
	public void setSub_buttons(List<WxButton> sub_buttons) {
		this.sub_buttons = sub_buttons;
	}
	
	@ManyToOne(fetch=FetchType.EAGER, optional=true)
	@JoinColumn(name="wx_button_parent_uid")
	public WxButton getWxButtonParent() {
		return wxButtonParent;
	}
	
	public void setWxButtonParent(WxButton wxButtonParent) {
		this.wxButtonParent = wxButtonParent;
	}
	
	
	@Override
	public String toString() {
		return "WxButton [name=" + name + ", type=" + type + ", key=" + key
				+ ", url=" + url + ", media_id=" + media_id + ", sub_buttons="
				+ sub_buttons + ", wxButtonParent=" + wxButtonParent + ", uid="
				+ uid + ", createTime=" + createTime + ", createUser="
				+ createUser + ", updateTime=" + updateTime + ", updateUser="
				+ updateUser + ", startTime=" + startTime + ", endTime="
				+ endTime + ", statusType=" + statusType + ", ipAddress="
				+ ipAddress + ", remark=" + remark + "]";
	}
	
	
	
	
	
	
	
}

 

 

>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>.

蕃薯耀 2016年3月31日 22:22:53 星期四

http://fanshuyao.iteye.com/

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值