JsonUtils简单使用

package com.bbzn.device.client.utils;


import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.TypeReference;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.IOException;
import java.lang.reflect.Type;
import java.util.HashMap;
import java.util.Map;

/**
 * @Author wulongbo
 * @Date 2020/7/6 15:43
 * @Version 1.0
 */
public class JsonUtils {

    private static final Logger log = LoggerFactory.getLogger(JsonUtils.class);


    /**
     * byte数组转换成Json
     *
     * @param array
     * @return JSONObject
     */
    public static JSONObject byteToJson(byte[] array) {
        String body = JSONArray.toJSONString(array); // 先转换成Json String
        byte[] json = JSONObject.parseObject(body, byte[].class);//转json byte
        String convent = new String(json); // byte数组转换成String
        JSONObject jsonObject = JSONObject.parseObject(convent);
        return jsonObject;
    }

    /**
     * byte数组转换成Java bean
     *
     * @param array
     * @return JSONObject
     */
    public static  T byteToJaveBean(byte[] array, Class clazz) throws InstantiationException, IllegalAccessException {
        JSONObject jsonObject = byteToJson(array);
        T obj = clazz.newInstance();
        return jsonObject.toJavaObject((Type) obj.getClass());
    }

    /**
     * String转换成Json
     *
     * @param key
     * @return JSONObject
     */
    public static JSONObject stringToJson(String key) {
        return JSONObject.parseObject(key);
    }

    /**
     * Java对象转换成String
     *
     * @return
     */
    public static String objectToString(Object object) {
        return JSONArray.toJSONString(object);
    }

    /**
     * Java bean 转换成 JSONObject
     *
     * @return
     */
    public static JSONObject beanToJSONObject(Object object) {
        return JSONObject.parseObject(JSONObject.toJSON(object).toString());
    }

    /**
     * 将map转换成byte[]
     *
     * @return
     */
    public static byte[] changeMapToByte(Map map) {

        byte[] bytes = null;
        try {
            bytes = serilizableForMap(map).getBytes();
        } catch (Exception e) {
            log.error("map到byte[]转换异常", e);
        }
        return bytes;
    }

    /**
     * 将byte[]转换成map
     *
     * @return
     */
    public static Map changeByteToMap(byte[] bytes) {

        Map retmap = null;

        try {
            if (bytes != null) {
                retmap = deserilizableForMapFromFile(new String(bytes), String.class);
            } else {
                log.error("changeByteToMap中bytes为null");
            }

        } catch (Exception e) {
            log.error("byte到map转换异常", e);
        }

        return retmap;
    }

    /* 将HashMap序列化为字符串存入json文件中 */
    public static String serilizableForMap(Object objMap)
            throws IOException {
        String listString = JSON.toJSONString(objMap, true);// (maps,CityEntity.class);
        return listString;
    }

    /* 将json文件中的内容读取出来,反序列化为HashMap */
    public static  HashMap deserilizableForMapFromFile(String listString2, Class clazz) throws IOException {

        Map map = JSON.parseObject(listString2, new TypeReference>() {
        });

        return (HashMap) map;
    }

}

你可能感兴趣的