服务器和前台采用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

目录
相关文章
|
JSON 小程序 前端开发
小程序模拟请求服务器json数据
小程序模拟请求服务器json数据
458 0
|
9月前
|
JSON JavaScript 前端开发
怎么安装JSON服务器?JSON服务器最新安装教程
JSON Server是一款轻量级工具,基于Node.js运行,可通过单个JSON文件快速模拟RESTful API,适用于开发与测试场景。其支持CRUD操作、自定义路由及高级功能如排序、搜索、分页等,同时允许通过中间件扩展功能。为确保安全性,建议在受控环境中使用,并避免处理敏感数据。此外,还有Mirage JS、Mockoon、WireMock和Postman等替代方案,可根据项目需求选择合适的工具。
339 0
|
JSON 数据处理 数据安全/隐私保护
Ktor库的高级用法:代理服务器与JSON处理
Ktor库的高级用法:代理服务器与JSON处理
|
JSON API 数据格式
基于服务器响应的实时天气数据进行JSON解析的详细代码及其框架
【8月更文挑战第25天】这段资料介绍了一个使用Python从服务器获取实时天气数据并解析JSON格式数据的基本框架。主要分为三个部分:一是安装必要的`requests`库以发起HTTP请求获取数据,同时利用Python内置的`json`库处理JSON数据;二是提供了具体的代码实现,包括获取天气数据的`get_weather_data`函数和解析数据的`parse_weather_data`函数;三是对代码逻辑进行了详细说明,包括如何通过API获取数据以及如何解析这些数据来获取温度和天气描述等信息。用户需要根据实际使用的天气API调整代码中的API地址、参数和字段名称。
362 0
|
XML JSON Apache
【Android】如何获得Apache服务器的JSON文件数据
【Android】如何获得Apache服务器的JSON文件数据
321 0
|
2月前
|
弹性计算 运维 安全
阿里云轻量应用服务器与云服务器ECS啥区别?新手帮助教程
阿里云轻量应用服务器适合个人开发者搭建博客、测试环境等低流量场景,操作简单、成本低;ECS适用于企业级高负载业务,功能强大、灵活可扩展。二者在性能、网络、镜像及运维管理上差异显著,用户应根据实际需求选择。
276 10
|
2月前
|
运维 安全 Ubuntu
阿里云渠道商:服务器操作系统怎么选?
阿里云提供丰富操作系统镜像,涵盖Windows与主流Linux发行版。选型需综合技术兼容性、运维成本、安全稳定等因素。推荐Alibaba Cloud Linux、Ubuntu等用于Web与容器场景,Windows Server支撑.NET应用。建议优先选用LTS版本并进行测试验证,通过标准化镜像管理提升部署效率与一致性。
|
2月前
|
弹性计算 ice
阿里云4核8g服务器多少钱一年?1个月和1小时价格,省钱购买方法分享
阿里云4核8G服务器价格因实例类型而异,经济型e实例约159元/月,计算型c9i约371元/月,按小时计费最低0.45元。实际购买享折扣,1年最高可省至1578元,附主流ECS实例及CPU型号参考。
421 8
|
2月前
|
存储 监控 安全
阿里云渠道商:云服务器价格有什么变动?
阿里云带宽与存储费用呈基础资源降价、增值服务差异化趋势。企业应结合业务特点,通过阶梯计价、智能分层、弹性带宽等策略优化成本,借助云监控与预算预警机制,实现高效、可控的云资源管理。
|
2月前
|
弹性计算 运维 安全
区别及选择指南:阿里云轻量应用服务器与ECS云服务器有什么区别?
阿里云轻量应用服务器适合个人开发者、学生搭建博客、测试环境,易用且性价比高;ECS功能更强大,适合企业级应用如大数据、高流量网站。根据需求选择:轻量入门首选,ECS专业之选。
269 2