首页>代码>ssm开发大学校园兼职平台项目(可以作为毕设)>/moonlighting/src/com/work/common/utils/JsonUtil.java
package com.work.common.utils;

import java.util.List;
import java.util.Map;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;

public class JsonUtil {
	public static String objectToJson(Object obj) {
		Gson gson = new GsonBuilder().setDateFormat("yyyy-MM-dd HH:mm:ss").create();
		String jsonString = "{";
		jsonString += "\"code\": \"0\", ";
		jsonString += "\"msg\": \"处理成功\",";
		jsonString += "\"objects\":";
		jsonString += gson.toJson(obj);
		jsonString += "} ";
		return jsonString;
	}

	// 返回总记录数
	public static String buildJsonByTotalCount(List list, int totalCount) {
		Gson gson = new GsonBuilder().setDateFormat("yyyy-MM-dd HH:mm:ss").create();
		String jsonString = "{";
		String JsonContext = "";
		int count = 0;
		if (!CommonUtil.isEmpty(list)) {
			JsonContext = gson.toJson(list);
			count = totalCount;
			jsonString += "\"code\": \"0\", ";
			jsonString += "\"msg\": \"处理成功\",";
		} else {
			JsonContext = "[] ";
			count = 0;
			jsonString += "\"code\": \"1\", ";
			jsonString += "\"msg\": \"此查询无数据\",";
		}
		jsonString += "\"count\": " + count + ",";
		jsonString += "\"data\":";
		jsonString += JsonContext;
		jsonString += "} ";
		return jsonString;
	}

	public static String buildJson(List list) {
		Gson gson = new GsonBuilder().setDateFormat("yyyy-MM-dd HH:mm:ss").create();
		String jsonString = "{";
		String JsonContext = "";
		int count = 0;
		if (!CommonUtil.isEmpty(list)) {
			JsonContext = gson.toJson(list);
			count = list.size();
			jsonString += "\"code\": \"0\", ";
			jsonString += "\"msg\": \"处理成功\",";
		} else {
			JsonContext = "[] ";
			count = 0;
			jsonString += "\"code\": \"1\", ";
			jsonString += "\"msg\": \"处理失败,没有数据\",";
		}
		jsonString += "\"count\": " + count + ",";
		jsonString += "\"objects\":";
		jsonString += JsonContext;
		jsonString += "} ";
		return jsonString;
	}

	public static String buildFalseJson(int code, String message) {
		String jsonString = "{";
		jsonString += "\"code\": \"" + code + "\", ";
		jsonString += "\"msg\": \"" + message + "\" ";
		jsonString += "}";
		return jsonString;
	}
	public static String buildFalseJson2(int code, String message1,String message2) {
		String jsonString = "{";
		jsonString += "\"code\": \"" + code + "\", ";
		jsonString += "\"url\": \"" + message1 + "\",";
		jsonString += "\"fileName\": \"" + message2 + "\" ";
		jsonString += "}";
		return jsonString;
	}

	/**
	 * 检测jar包
	 * 
	 * @param result
	 * @param detail
	 * @param content
	 * @return
	 */
	public static String buildCheckJarJson(Boolean result, String detail, String content) {
		String jsonString = "{";
		jsonString += "\"result\": " + result + ", ";
		jsonString += "\"detail\": \"" + detail + "\" ";
		jsonString += "\"content\": \"" + content + "\"";
		jsonString += "}";
		return jsonString;
	}

	public static String MapToJson(Map<String, Object> m) {
		Gson gson = new GsonBuilder().setDateFormat("yyyy-MM-dd HH:mm:ss").create();
		String jsonString = "{";
		String JsonContext = "";
		int count = 0;
		JsonContext = gson.toJson(m);
		count = m.size();
		jsonString += "\"code\": \"0\", ";
		jsonString += "\"msg\": \"处理成功\",";
		jsonString += "\"count\": " + count + ",";
		jsonString += "\"data\":";
		jsonString += JsonContext;
		jsonString += "} ";
		return jsonString;
	}
}
最近下载更多
leipengshen  LV8 2024年12月27日
翻斗花园胡英俊  LV2 2024年12月8日
bridge_44  LV2 2024年10月28日
穿山甲1001  LV6 2024年9月12日
TY0165  LV20 2024年6月21日
微信网友_7024879466991616  LV2 2024年6月6日
yyhrhv  LV8 2024年5月31日
吞吞吐吐她  LV6 2024年4月21日
860421  LV3 2024年4月18日
zolscy  LV24 2024年3月24日
最近浏览更多
chengguo123 4月2日
暂无贡献等级
哪里的完整版  LV8 3月10日
暂无贡献等级
暂无贡献等级
mymzmymzhhuu 1月5日
暂无贡献等级
leipengshen  LV8 2024年12月27日
2435058517 2024年12月2日
暂无贡献等级
短巷拥猫  LV12 2024年12月2日
暂无贡献等级
顶部 客服 微信二维码 底部
>扫描二维码关注最代码为好友扫描二维码关注最代码为好友