json格式返回前端状态码工具类

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/weixin_39643007/article/details/82081448
package com.yuezhu.utils;


import com.yuezhu.dto.PageDto;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import net.sf.json.util.JSONUtils;
import org.apache.commons.beanutils.BeanUtils;

import java.util.*;

public class JSONUtil{
    public static String packJsonStr(Object object){
        String result;
        String message;
        JSONObject jsonObject = new JSONObject();
        if (null!=object){
            result="1";
            message="获取数据成功";

        }else {
            result="0";
            message="获取数据失败";
        }
        jsonObject.element("result",result);
        jsonObject.element("message",message);
        jsonObject.element("object",object);
        return jsonObject.toString();
    }
    public static String packJsonStr(int num){
        String result;
        String message;
        JSONObject jsonObject = new JSONObject();
        if (num>0){
            result="1";
            message="操作数据成功";

        }else {
            result="0";
            message="操作数据失败";
        }
        jsonObject.element("result",result);
        jsonObject.element("message",message);
        jsonObject.element("dataChangeNum",num);
        return jsonObject.toString();
    }

    public static String packJsonStr(int num , String message){
        String result;
        JSONObject jsonObject = new JSONObject();

        if (num>0){
            result="1";
        }else {
            result="0";
        }
        jsonObject.element("result",result);
        jsonObject.element("message",message);
        jsonObject.element("dataChangeNum",num);
        return jsonObject.toString();
    }


    public static String packJsonStr(long id , int num , String message){
        String result;
        JSONObject jsonObject = new JSONObject();

        if (num>0){
            result="1";
        }else {
            result="0";
        }
        jsonObject.element(id+"",id);
        jsonObject.element("result",result);
        jsonObject.element("message",message);
        jsonObject.element("dataChangeNum",num);
        return jsonObject.toString();
    }
    public static String packJsonStr(String result , String message){
        JSONObject jsonObject = new JSONObject();
        jsonObject.element("result",result);
        jsonObject.element("message",message);
        return jsonObject.toString();
    }

    // 获取 page 对象的 json 数据
    public static String getPageJson(int totalNumber , String page , String pageSize , List data){
        PageDto pageDto = new PageDto();
        pageDto.setData(data);
        pageDto.setPage(Integer.valueOf(page));
        pageDto.setPageSize(Integer.valueOf(pageSize));
        pageDto.setTotalNumber(totalNumber);
        return JSONUtil.packJsonStr(pageDto);
    }

    /***
     * 将List对象序列化为JSON文本
     */
    public static <T> String toJSONString(List<T> list)
    {
        JSONArray jsonArray = JSONArray.fromObject(list);

        return jsonArray.toString();
    }

    /***
     * 将对象序列化为JSON文本
     * @param object
     * @return
     */
    public static String toJSONString(Object object)
    {
        JSONArray jsonArray = JSONArray.fromObject(object);

        return jsonArray.toString();
    }

    /***
     * 将JSON对象数组序列化为JSON文本
     * @param jsonArray
     * @return
     */
    public static String toJSONString(JSONArray jsonArray)
    {
        return jsonArray.toString();
    }

    /***
     * 将JSON对象序列化为JSON文本
     * @param jsonObject
     * @return
     */
    public static String toJSONString(JSONObject jsonObject)
    {
        return jsonObject.toString();
    }

    /***
     * 将对象转换为List对象
     * @param object
     * @return
     */
    public static List toArrayList(Object object)
    {
        List arrayList = new ArrayList();

        JSONArray jsonArray = JSONArray.fromObject(object);

        Iterator it = jsonArray.iterator();
        while (it.hasNext())
        {
            JSONObject jsonObject = (JSONObject) it.next();

            Iterator keys = jsonObject.keys();
            while (keys.hasNext())
            {
                Object key = keys.next();
                Object value = jsonObject.get(key);
                arrayList.add(value);
            }
        }

        return arrayList;
    }

    /***
     * 将对象转换为Collection对象
     * @param object
     * @return
     */
    public static Collection toCollection(Object object)
    {
        JSONArray jsonArray = JSONArray.fromObject(object);

        return null;
    }

    /***
     * 将对象转换为JSON对象数组
     * @param object
     * @return
     */
    public static JSONArray toJSONArray(Object object)
    {
        return JSONArray.fromObject(object);
    }

    /***
     * 将对象转换为JSON对象
     * @param object
     * @return
     */
    public static JSONObject toJSONObject(Object object)
    {
        return JSONObject.fromObject(object);
    }

    /***
     * 将对象转换为HashMap
     * @param object
     * @return
     */
    public static HashMap toHashMap(Object object)
    {
        HashMap<String, Object> data = new HashMap<String, Object>();
        JSONObject jsonObject = JSONObject.fromObject(object);
        Iterator it = jsonObject.keys();
        while (it.hasNext())
        {
            String key = String.valueOf(it.next());
            Object value = jsonObject.get(key);
            data.put(key, value);
        }

        return data;
    }

    /***
     * 将对象转换为List>
     * @param object
     * @return
     */
// 返回非实体类型(Map)的List
    public static List<Map<String, Object>> toList(Object object)
    {
        List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
        JSONArray jsonArray = JSONArray.fromObject(object);
        for (Object obj : jsonArray)
        {
            JSONObject jsonObject = (JSONObject) obj;
            Map<String, Object> map = new HashMap<String, Object>();
            Iterator it = jsonObject.keys();
            while (it.hasNext())
            {
                String key = (String) it.next();
                Object value = jsonObject.get(key);
                map.put((String) key, value);
            }
            list.add(map);
        }
        return list;
    }


}
展开阅读全文

没有更多推荐了,返回首页