数组,List 和Json

1.数组和list区别

Array 和 List 都属于顺序表。因为Array 长度不可变,所以有了List。

存储结构

1.Array 是一段连续的存储结构

int[ ]  array =new int[3]

array 其实记录的是数组的首地址,而array[1] 其实相当于在i的地址的基础上加上1个整数的地址偏移,然后再取这块地址中的值。

2.List  是不连续的存储结构,List的每一个节点都有着一个next属性,这个属性则记录着他的下一个节点的地址。

也就是说当我们想找第100个节点的时候,他还是需要从第一个节点,然后做99次next 操作,才能找到list[99]节点。

另外,cpu缓存会把一片连续的内存空间读入,因为数组结构是连续的内存地址,所以数组全部或者部分元素被连续存在CPU缓存里面,平均读取每个元素的时间只要3个CPU时钟时间。而链表的节点分散在堆空间里面。

空间扩展

数组必须要在初始化时分配固定的大小,比如说int[ ]=new int[3];如果我们仅仅写 int[ ] a=new int[ ];编译器就会报错。但是List由于空间不必连续,所以无须指定初始大小。

存储内容

Array 数组可以包含基本类型和对象类型。

ArrayList 却只能包含对象类型。但是需要注意的是:Array 数组在存放的时候一定是同种类型的元素。ArrayList 就不一定了,因为ArrayList可以Object.

适用场景

保存一些在整个程序运行期间都会存在而且不变的数据,可以存放在一个全局数组里。

LinkedList :元素进行频繁的移动或删除,或者处理的是超大量的数据。 

2.Json 

JSONArray personLists = JSONArray.parseArray(persons);//以JSON的格式解析

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.demo.common.util.json.bean.AllList;
import com.demo.common.util.json.bean.SeatList;
import com.demo.common.util.json.bean.SeatS;
import com.demo.common.util.json.exception.JsonUtilErrorException;
import com.demo.meet.Seat;
import com.fasterxml.jackson.databind.ObjectMapper;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.JavaType;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.module.SimpleModule;
import com.fasterxml.jackson.databind.ser.std.ToStringSerializer;
import com.google.common.collect.Maps;
import org.apache.commons.lang3.StringUtils;


import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;

public class JsonUtil {


    /**
     * @description bean转json
     * @param t
     * @return
     */
    public static <T> String toJson(T t) {
        if(t==null) {
            return null;
        }
        ObjectMapper mapper = new ObjectMapper();
        String json = null;
        try {
            json = mapper.writeValueAsString(t);
        } catch (JsonProcessingException e) {
            e.printStackTrace();
            throw new JsonUtilErrorException("toJson转换错误", e);
        }
        return json;
    }

    /**
     * @description bean转json的同时,将Long类型转为String
     * @param t
     * @return
     */
    public static <T> String toJsonAndLongToString(T t) {
        if(t==null) {
            return null;
        }
        ObjectMapper mapper = new ObjectMapper();
        /**
         * 序列换成json时,将所有的long变成string
         * 因为js中得数字类型不能包含所有的java long值
         */
        SimpleModule simpleModule = new SimpleModule();
        simpleModule.addSerializer(Long.class, ToStringSerializer.instance);
        simpleModule.addSerializer(Long.TYPE, ToStringSerializer.instance);
        mapper.registerModule(simpleModule);
        String json = null;
        try {
            json = mapper.writeValueAsString(t);
        } catch (JsonProcessingException e) {
            e.printStackTrace();
            throw new JsonUtilErrorException("toJsonAndLongToString转换错误", e);
        }
        return json;
    }

    /**
     * @description 根据path将json中的某个部分转化为bean
     * @param json 需要解析的json字符串
     * @param path 需要解析json中的某个路径,例如:/user/type,如果为null,则默认转换这个json字符串
     * @param clazz 需要转换的class type
     * @return
     */
    public static <T> T toObject(String json, String path, Class<T> clazz) {
        if(StringUtils.isBlank(json)) {
            return null;
        }
        ObjectMapper mapper = new ObjectMapper();
        T t = null;
        if (StringUtils.isBlank(path)) {
            try {
                t = mapper.readValue(json, clazz);
            } catch (IOException e) {
                e.printStackTrace();
                throw new JsonUtilErrorException("toObject转换错误", e);
            }
            return t;
        }
        try {
            JsonNode jsonRoot = mapper.readTree(json);
            JsonNode jsonNode = jsonRoot.at(path);
            if (jsonNode.isMissingNode()) {
                return t;
            }
            t = mapper.readValue(jsonNode.toString(), clazz);
        }  catch (IOException e) {
            e.printStackTrace();
            throw new JsonUtilErrorException("toObject转换错误", e);
        }
        return t;
    }

    /**
     * @description 将json转化为bean
     * @param json 需要解析的json字符串
     * @param clazz 需要转换的class type
     * @return
     */
    public static <T> T toObject(String json, Class<T> clazz) {
        if(StringUtils.isBlank(json)) {
            return null;
        }
        return toObject(json, null, clazz);
    }

    /**
     *
     * @description 将json字符串转为对象List
     * @param json 需要解析的json字符串
     * @param clazz List中存储的对象类型
     * @return
     */
    public static <T> List<T> toObjectList(String json, Class<T> clazz) {
        if(StringUtils.isBlank(json)) {
            return null;
        }
        ObjectMapper mapper = new ObjectMapper();
        JavaType javaType = mapper.getTypeFactory().constructParametricType(ArrayList.class, clazz);
        try {
            List<T> tList = mapper.readValue(json, javaType);
            return tList;
        } catch (IOException e) {
            e.printStackTrace();
            throw new JsonUtilErrorException("toObjectList转换错误", e);
        }
    }

    /**
     * @description 根据path将json中的某个部分转化为List<Bean>
     * @param json 需要解析的json字符串
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值