后台怎么接收处理从url 客户端传来的json数
2018-01-16 · 知道合伙人数码行家
知道合伙人数码行家
向TA提问 私信TA
后台解析前端传来的json数据格式听简单的#
比如客户端传来的参数格式url
从最外边可以知道我们传给的最外面的参数是data所以后台接收到的也只是data的参数
所以我们现在需要做的就是解析传给后台的json数据格式
代码如下:
// 谷歌产生json对象的工具
Gson json = new Gson();
// 产生 P{} 里面参数接收对象
Param p = new Param(); Param对象里的属性是pageNum,type,sessionKey(注意的是该对象指的是p,对象里面的属性也必须和客户端中json数据格式中的字段名称一致)
//需要将客户端传来的json数据转换成什么对象
p = json.fromJson(data, Param.class); data为最外层的json数据对象
获取对象中的参数的值
p.getType();就能获取到客户端传来的参数的值了###
很简单的 API一看就明白了###
最外层当然为
gson.jar包
public class Base {
private String m;
private String c;
public String getM() {
return m;
}
public void setM(String m) {
this.m = m;
}
public String getC() {
return c;
}
public void setC(String c) {
this.c = c;
}
}
对象中的对象
package com.ismartinfo.ilife.api.entities.parameter;
public class Param extends Base{
private ParamValue p;
public ParamValue getP() {
return p;
}
public void setP(ParamValue p) {
this.p = p;
}
}
package com.ismartinfo.ilife.api.entities.parameter;
public class ParamValue {
private int pageNum;
private int type;
private int groupInfoId;
private int groupInfoReplyId;
private String sessionKey;
private int groupTitle;
private String groupContent;
private String phone;
private int buttonType;
private int replayId;
private int disclosure;
public int getPageNum() {
return pageNum;
}
public void setPageNum(int pageNum) {
this.pageNum = pageNum;
}
public String getSessionKey() {
return sessionKey;
}
public void setSessionKey(String sessionKey) {
this.sessionKey = sessionKey;
}
public int getType() {
return type;
}
public void setType(int type) {
this.type = type;
}
public int getGroupInfoId() {
return groupInfoId;
}
public void setGroupInfoId(int groupInfoId) {
this.groupInfoId = groupInfoId;
}
public int getGroupInfoReplyId() {
return groupInfoReplyId;
}
public void setGroupInfoReplyId(int groupInfoReplyId) {
this.groupInfoReplyId = groupInfoReplyId;
}
public int getGroupTitle() {
return groupTitle;
}
public void setGroupTitle(int groupTitle) {
this.groupTitle = groupTitle;
}
public String getGroupContent() {
return groupContent;
}
public void setGroupContent(String groupContent) {
this.groupContent = groupContent;
}
public String getPhone() {
return phone;
}
public void setPhone(String phone) {
this.phone = phone;
}
public int getButtonType() {
return buttonType;
}
public void setButtonType(int buttonType) {
this.buttonType = buttonType;
}
public int getReplayId() {
return replayId;
}
public void setReplayId(int replayId) {
this.replayId = replayId;
}
public int getDisclosure() {
return disclosure;
}
public void setDisclosure(int disclosure) {
this.disclosure = disclosure;
}
}