后台怎么接收处理从url 客户端传来的json数据格式

来源:互联网 发布:win10不能打开软件 编辑:程序博客网 时间:2024/06/06 00:30


最近做项目用到了 一个新的客户端传参的方式,主要采用的是客户端以json数据格式的方式想后台传递数据,所以,后台接收的参数也是json格式的,刚开始不知道怎么做,

到最后才找到了解决的办法就是利用google提供的gson.jar就能很好的解决。 其实就是把前端的json数据格式转换成对象#

gson.jar下载地址  http://www.java2s.com/Code/Jar/g/Downloadgson224jar.htm 最新版本 2.24



开始看了google提供的API我就能知道其实,后台解析前端传来的json数据格式听简单的#

比如客户端传来的参数格式url 

url:   http://10.16.16.104:8081/ilifeGroupinfo_getGroupinfoPager.action?data={"c":"groupInfo","m":"list","p":{"pageNum":1,"type":0,"sessionKey":1}}   

从最外边可以知道我们传给的最外面的参数是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;
}  
}

0 0
原创粉丝点击