java cef3 和前台通讯_服务器和前台采用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;   //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 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串互转

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值