标签:
Java服务端收到前端字符串转json:
import com.alibaba.fastjson.JSONObject; String s = "{\"userLoginId\":\"sysman\",\"mobileIds\":\"bdzfzl001,sgbj001\"}"; JSONObject jsonobj = JSON.parseObject(s); System.out.println(jsonobj.get("userLoginId")); System.out.println(jsonobj.get("mobileIds")); String mobileIds = (String) jsonobj.get("mobileIds"); String[] arrMobileId = mobileIds.split(","); System.out.println(arrMobileId[0]+"|"+arrMobileId[1]);
Java服务器端向前端发送:
JsonUtil.java
package com.zhxjz.framework.util.common; import java.util.ArrayList; import java.util.Iterator; import java.util.List; import org.apache.commons.lang.StringUtils; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.codehaus.jackson.JsonNode; import org.codehaus.jackson.map.ObjectMapper; import com.zhxjz.framework.model.json.JsonResult; /** * json处理工具类 * * @author caozj */ public class JsonUtil { private JsonUtil() { } private static final ObjectMapper mapper = new ObjectMapper(); private static final Log logger = LogFactory.getLog(JsonUtil.class); /** * Object -> json * * @param obj * @return */ public static String toJson(Object obj) { try { return mapper.writeValueAsString(obj); } catch (Exception e) { logger.error("Object转化成Json失败", e); } return null; } /** * json -> Object * * @param <T> * @param content * @param valueType * @return */ public static <T> T toObject(String content, Class<T> valueType) { if (StringUtils.isEmpty(content)) { return null; } try { return mapper.readValue(content, valueType); } catch (Exception e) { logger.error("Json转化成Object失败", e); } return null; } /** * json -> List * * @param <T> * @param content * @param valueType * @return */ public static <T> List<T> toList(String content, Class<T> valueType) { JsonNode node = null; try { node = mapper.readTree(content); } catch (Exception e) { logger.error("Json转化成List失败", e); } if (node == null) { return new ArrayList<T>(0); } Iterator<JsonNode> iterator = node.getElements(); List<T> list = new ArrayList<T>(); while (iterator.hasNext()) { String json = iterator.next().toString(); T e = toObject(json, valueType); list.add(e); } return list; } /** * success json * * @return - {success:true} */ public static String successJson() { return toJson(new JsonResult()); } /** * result json * * @param success * - 操作成功与否 * @param message * - 结果信息 * @return - {success:true,message:‘test‘} */ public static String resultJson(boolean success, String message) { return toJson(new JsonResult(message, success ? JsonResult.successCode : JsonResult.failCode)); } /** * result json * * @param code * - 状态码 * @param message * - 结果信息 * @param data * - 结果数据 * @return */ public static String resultJson(int code, String message, Object data) { return toJson(new JsonResult(data, message, code)); } }
JsonResult.java
package com.zhxjz.framework.model.json; import com.zhxjz.framework.util.common.JsonUtil; /** * 返回给页面的json格式数据 * * @author caozj * */ public class JsonResult { /** * 成功码 */ public static final int successCode = 200; /** * 错误码 */ public static final int failCode = 400; /** * 状态码(200代表成功,其他可自行定义) */ private int code; /** * 提示消息 */ private String msg; /** * 数据对象 */ private Object data; public int getCode() { return code; } public void setCode(int code) { this.code = code; } public String getMsg() { return msg; } public void setMsg(String msg) { this.msg = msg; } public Object getData() { return data; } public void setData(Object data) { this.data = data; } public JsonResult() { code = successCode; } public JsonResult(Object data) { this.data = data; code = successCode; } public JsonResult(Object data, String msg) { this.data = data; this.code = successCode; this.msg = msg; } public JsonResult(Object data, String msg, int code) { this.data = data; this.code = code; this.msg = msg; } public JsonResult(String msg, int code) { this.code = code; this.msg = msg; } public JsonResult(int code) { this.code = code; } public String toJson() { return JsonUtil.toJson(this); } }
List<Map<String, Object>> dataList = userFuncService.getUserFunc(account, arrMobileId);
String message = new JsonResult(dataList).toJson();
标签:
原文地址:http://my.oschina.net/u/555061/blog/487269