服务器和前台采用JSON通讯

简介: 封装实体类(服务器返回信息都存放在该实体中)package com.chinaseacom.store.common;public class ResponseBody { public static final int CODE_SUCCESS=1; public static final int CODE_FAIL=0; private int code=1;  


封装实体类(服务器返回信息都存放在该实体中)

package com.chinaseacom.store.common;

public class ResponseBody {
	
	public static final int CODE_SUCCESS=1;
	public static final int CODE_FAIL=0;
	
	
	private int code=1;   //0:failtrue; 1:success
	private String message; //
	private String errors;
	private String accessToken;
	private Object result;
	
	
	public ResponseBody(int code, String message, String errors, Object result, String accessToken) {
		super();
		this.code = code;
		this.message = message;
		this.errors = errors;
		this.result = result;
		this.accessToken = accessToken;
	}
	
	public String getAccessToken() {
		return accessToken;
	}
	public void setAccessToken(String accessToken) {
		this.accessToken = accessToken;
	}
	/**
	 * @return the code
	 */
	public int getCode() {
		return code;
	}
	/**
	 * @param code the code to set
	 */
	public void setCode(int code) {
		this.code = code;
	}
	/**
	 * @return the message
	 */
	public String getMessage() {
		return message;
	}
	/**
	 * @param message the message to set
	 */
	public void setMessage(String message) {
		this.message = message;
	}
	/**
	 * @return the errors
	 */
	public String getErrors() {
		return errors;
	}
	/**
	 * @param errors the errors to set
	 */
	public void setErrors(String errors) {
		this.errors = errors;
	}
	/**
	 * @return the result
	 */
	public Object getResult() {
		return result;
	}
	/**
	 * @param result the result to set
	 */
	public void setResult(Object result) {
		this.result = result;
	}
	
	
}


服务器返回json字符串

  protected void writeResponseByJsonStr(Object javabean,int code, String message, String error){
    	
    	try {
    		HttpServletResponse response= getResponse();
    		response.setContentType("application/json; charset=utf-8");
    		response.setHeader("Cache-Control", "no-cache"); //取消浏览器缓存
    		PrintWriter out = response.getWriter();
    		out.print(JSON.toJSONString(new ResponseBody(code,message,error,javabean,accessToken)  ));//实体对象转换为json字符串传递
    		out.flush();
    		out.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
    }


前台解析json字符串(采用fastjson)步骤

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.URL;
import java.util.List;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;

import com.chinaseacom.store.customer.model.Notice;

public class MobileClient {

	public static void main(String[] args) throws IOException {
		BufferedReader reader = null;
		try {
			URL url = new URL("http://localhost:8080/customer/notice/list");

			reader = new BufferedReader(new InputStreamReader(url.openStream()));
			String jsonString = reader.readLine();
			System.out.println(jsonString);
			JSONObject jsonObject = JSON.parseObject(jsonString); // 得到json对象

			JSONArray jsonArray = (JSONArray) jsonObject.get("result");// 根据key得到json数组
			System.out.println(jsonArray.toJSONString());
			List<Notice> nlist = JSON.parseArray(jsonArray.toJSONString(),
					Notice.class); // json数组转换成对象集合
			for (Notice notice : nlist) {
				System.out.println(notice.getUpdateDate());
			}
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			reader.close();
		}
	}

}



参考文章:使用FastJSON,将对象或数组和JSON串互转

http://blog.csdn.net/gaojinshan/article/details/30260707


本文出自 “点滴积累” 博客,请务必保留此出处http://tianxingzhe.blog.51cto.com/3390077/1704002

目录
相关文章
|
5月前
|
JSON 小程序 前端开发
小程序模拟请求服务器json数据
小程序模拟请求服务器json数据
183 0
|
4月前
|
XML JSON Apache
【Android】如何获得Apache服务器的JSON文件数据
【Android】如何获得Apache服务器的JSON文件数据
56 0
|
10月前
|
XML JSON 前端开发
Qt+QtWebApp开发笔记(五):http服务器html中使用json触发ajax与后台交互实现数据更新传递
前面完成了页面的跳转、登录,很多时候不刷新页面就想刷新局部数据,此时ajax就是此种技术,且是异步的。   本篇实现网页内部使用js调用ajax实现异步交互数据。   在js中使用 ajax是通过XMLHttpRequest来实现的。
|
11月前
|
JSON 小程序 数据格式
零基础学小程序003----请求服务器数据,请求后台json数据
零基础学小程序003----请求服务器数据,请求后台json数据
198 0
|
JSON NoSQL MongoDB
分布式服务器框架之Servers.Core库中实现MongoDB的ObjectId和Json转换
分布式服务器框架之Servers.Core库中实现MongoDB的ObjectId和Json转换
|
JSON 编解码 网络协议
海量用户通讯信息-服务器接收长度|学习笔记
快速学习海量用户通讯信息-服务器接收长度
123 0
海量用户通讯信息-服务器接收长度|学习笔记
|
JSON 网络协议 测试技术
海量用户通讯系统-服务器接收消息2|学习笔记
快速学习海量用户通讯系统-服务器接收消息2
86 0
海量用户通讯系统-服务器接收消息2|学习笔记
|
网络协议 测试技术 Go
海量用户通讯系统-服务器接收消息1
海量用户通讯系统-服务器接收消息1
72 0
海量用户通讯系统-服务器接收消息1
案例分享:Qt西门子PLC调试模拟工具(包含PLC上位机通讯,PLC服务器,读写Byte、Int、DInt、Real)(持续更新,当前v1.5.0)
案例分享:Qt西门子PLC调试模拟工具(包含PLC上位机通讯,PLC服务器,读写Byte、Int、DInt、Real)(持续更新,当前v1.5.0)
案例分享:Qt西门子PLC调试模拟工具(包含PLC上位机通讯,PLC服务器,读写Byte、Int、DInt、Real)(持续更新,当前v1.5.0)
|
IDE 物联网 测试技术
项目实战:Qt并发服务器通讯,受同一时刻最大线程数限制(笔者本本同一时刻600多)
项目实战:Qt并发服务器通讯,受同一时刻最大线程数限制(笔者本本同一时刻600多)
项目实战:Qt并发服务器通讯,受同一时刻最大线程数限制(笔者本本同一时刻600多)

热门文章

最新文章