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字符串 * @param path 需要解析json中的某个路径,例如:/user/type,如果为null,则默认转换这个json字符串 * @param clazz List中存储的对象类型 * @return */ public static <T> List<T> toObjectList(String json, String path, Class<T> clazz) { if(StringUtils.isBlank(json)) { return null; } ObjectMapper mapper = new ObjectMapper(); mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false); if (StringUtils.isBlank(path)) { 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); } } else { try { JsonNode jsonRoot = mapper.readTree(json); JsonNode jsonNode = jsonRoot.at(path); if (jsonNode.isMissingNode()) { return null; } JavaType javaType = mapper.getTypeFactory().constructParametricType(ArrayList.class, clazz); List<T> tList = mapper.readValue(jsonNode.toString(), javaType); return tList; } catch (IOException e) { e.printStackTrace(); throw new JsonUtilErrorException("toObjectList转换错误", e); } } } /** * * @description 将json字符串转换为Map * @param json 需要转换的JSON字符串 * @param keyClazz Map中key的类型 * @param valueClazz Map中value的类型 * @return */ public static <T, V> Map<T,V> toObjectMap(String json, Class<T> keyClazz, Class<V> valueClazz) { if(StringUtils.isBlank(json)) { return null; } ObjectMapper mapper = new ObjectMapper(); JavaType javaType = mapper.getTypeFactory().constructParametricType(Map.class, keyClazz, valueClazz); try { Map<T,V> map = mapper.readValue(json, javaType); return map; } catch (IOException e) { e.printStackTrace(); throw new JsonUtilErrorException("toObjectMap转换错误----->"+json, e); } } public static <T, V> TreeMap<T,V> toObjectTreeMap(String json, Class<T> keyClazz, Class<V> valueClazz) { if(StringUtils.isBlank(json)) { return null; } ObjectMapper mapper = new ObjectMapper(); JavaType javaType = mapper.getTypeFactory().constructParametricType(TreeMap.class, keyClazz, valueClazz); try { TreeMap<T,V> map = mapper.readValue(json, javaType); return map; } catch (IOException e) { e.printStackTrace(); throw new JsonUtilErrorException("toObjectTreeMap转换错误", e); } } /** * @description 将json字符串转换为泛型对象 * @param json 需要转换的JSON字符串 * @param keyClazz 对象类型 * @param valueClazz 对象类型的泛型 * @return */ public static <T> T toObjectT(String json, Class<T> keyClazz, Class<?>... valueClazz) { if(StringUtils.isBlank(json)) { return null; } ObjectMapper mapper = new ObjectMapper(); JavaType javaType = mapper.getTypeFactory().constructParametricType(keyClazz, valueClazz); try { T t = mapper.readValue(json, javaType); return t; } catch (IOException e) { e.printStackTrace(); throw new JsonUtilErrorException("toObjectT转换错误", e); } } /** * @description 向已有json字符串添加新节点 * @param json 原始的json字符串 * @param nodes 需要添加的到json字符串中的json节点 * @return */ public static String addJsonStrNode(String json, Map<String, Object> nodes) { ObjectMapper mapper = new ObjectMapper(); Map<String, Object> jsonMap = null; String jsonResult = null; try { jsonMap = toObjectMap(json, String.class, Object.class); jsonMap.putAll(nodes); jsonResult = mapper.writeValueAsString(jsonMap); return jsonResult; } catch (IOException e) { e.printStackTrace(); throw new JsonUtilErrorException("addJsonStrNode转换错误", e); } } public static String mergeToStr(String...jsons) { if(jsons==null) { return null; } Map<String, Object> jsonMapResult = Maps.newHashMap(); for (int i = 0; i < jsons.length; i++) { if(StringUtils.isNoneBlank(jsons[i])) { Map<String, Object> jsonMap = toObjectMap(jsons[i], String.class, Object.class); if(jsonMap!=null) { jsonMapResult.putAll(jsonMap); } } } String result = toJson(jsonMapResult); return result; } public static void main(String[] args) { String applyReason = "付费会员配置后台美豆规则统一---屏蔽返美豆数据缓存规划"; // applyReason = "{\"reason\":\""+applyReason+"\"}"; // Map<String, Object> map = toObjectMap(applyReason, String.class, Object.class); // System.out.println(map); applyReason="[[{\"seatPerId\":null,\"x\":0,\"y\":0,\"disable\":true,\"seatRecordId\":\"sajhahahhahh\"},{\"seatPerId\":null,\"x\":0,\"y\":1,\"disable\":true,\"seatRecordId\":null},{\"seatPerId\":null,\"x\":0,\"y\":2,\"disable\":true,\"seatRecordId\":null},{\"seatPerId\":null,\"x\":0,\"y\":3,\"disable\":true,\"seatRecordId\":null},{\"seatPerId\":null,\"x\":0,\"y\":4,\"disable\":false,\"seatRecordId\":null},{\"seatPerId\":null,\"x\":0,\"y\":5,\"disable\":false,\"seatRecordId\":null},{\"seatPerId\":null,\"x\":0,\"y\":6,\"disable\":true,\"seatRecordId\":null},{\"seatPerId\":null,\"x\":0,\"y\":7,\"disable\":true,\"seatRecordId\":null},{\"seatPerId\":null,\"x\":0,\"y\":8,\"disable\":true,\"seatRecordId\":null},{\"seatPerId\":null,\"x\":0,\"y\":9,\"disable\":true,\"seatRecordId\":null}],[{\"seatPerId\":null,\"x\":1,\"y\":0,\"disable\":true,\"seatRecordId\":null},{\"seatPerId\":null,\"x\":1,\"y\":1,\"disable\":true,\"seatRecordId\":null},{\"seatPerId\":null,\"x\":1,\"y\":2,\"disable\":false,\"seatRecordId\":null},{\"seatPerId\":null,\"x\":1,\"y\":3,\"disable\":false,\"seatRecordId\":null},{\"seatPerId\":null,\"x\":1,\"y\":4,\"disable\":false,\"seatRecordId\":null},{\"seatPerId\":null,\"x\":1,\"y\":5,\"disable\":false,\"seatRecordId\":null},{\"seatPerId\":null,\"x\":1,\"y\":6,\"disable\":false,\"seatRecordId\":null},{\"seatPerId\":null,\"x\":1,\"y\":7,\"disable\":false,\"seatRecordId\":null},{\"seatPerId\":null,\"x\":1,\"y\":8,\"disable\":true,\"seatRecordId\":null},{\"seatPerId\":null,\"x\":1,\"y\":9,\"disable\":true,\"seatRecordId\":null}],[{\"seatPerId\":null,\"x\":2,\"y\":0,\"disable\":false,\"seatRecordId\":null},{\"seatPerId\":null,\"x\":2,\"y\":1,\"disable\":false,\"seatRecordId\":null},{\"seatPerId\":null,\"x\":2,\"y\":2,\"disable\":false,\"seatRecordId\":null},{\"seatPerId\":null,\"x\":2,\"y\":3,\"disable\":false,\"seatRecordId\":null},{\"seatPerId\":null,\"x\":2,\"y\":4,\"disable\":false,\"seatRecordId\":null},{\"seatPerId\":null,\"x\":2,\"y\":5,\"disable\":false,\"seatRecordId\":null},{\"seatPerId\":null,\"x\":2,\"y\":6,\"disable\":false,\"seatRecordId\":null},{\"seatPerId\":null,\"x\":2,\"y\":7,\"disable\":false,\"seatRecordId\":null},{\"seatPerId\":null,\"x\":2,\"y\":8,\"disable\":false,\"seatRecordId\":null},{\"seatPerId\":null,\"x\":2,\"y\":9,\"disable\":false,\"seatRecordId\":null}],[{\"seatPerId\":null,\"x\":3,\"y\":0,\"disable\":false,\"seatRecordId\":null},{\"seatPerId\":null,\"x\":3,\"y\":1,\"disable\":false,\"seatRecordId\":null},{\"seatPerId\":null,\"x\":3,\"y\":2,\"disable\":false,\"seatRecordId\":null},{\"seatPerId\":null,\"x\":3,\"y\":3,\"disable\":false,\"seatRecordId\":null},{\"seatPerId\":null,\"x\":3,\"y\":4,\"disable\":false,\"seatRecordId\":null},{\"seatPerId\":null,\"x\":3,\"y\":5,\"disable\":false,\"seatRecordId\":null},{\"seatPerId\":null,\"x\":3,\"y\":6,\"disable\":false,\"seatRecordId\":null},{\"seatPerId\":null,\"x\":3,\"y\":7,\"disable\":false,\"seatRecordId\":null},{\"seatPerId\":null,\"x\":3,\"y\":8,\"disable\":false,\"seatRecordId\":null},{\"seatPerId\":null,\"x\":3,\"y\":9,\"disable\":false,\"seatRecordId\":null}],[{\"seatPerId\":null,\"x\":4,\"y\":0,\"disable\":false,\"seatRecordId\":null},{\"seatPerId\":null,\"x\":4,\"y\":1,\"disable\":false,\"seatRecordId\":null},{\"seatPerId\":null,\"x\":4,\"y\":2,\"disable\":false,\"seatRecordId\":null},{\"seatPerId\":null,\"x\":4,\"y\":3,\"disable\":false,\"seatRecordId\":null},{\"seatPerId\":null,\"x\":4,\"y\":4,\"disable\":false,\"seatRecordId\":null},{\"seatPerId\":null,\"x\":4,\"y\":5,\"disable\":false,\"seatRecordId\":null},{\"seatPerId\":null,\"x\":4,\"y\":6,\"disable\":false,\"seatRecordId\":null},{\"seatPerId\":null,\"x\":4,\"y\":7,\"disable\":false,\"seatRecordId\":null},{\"seatPerId\":null,\"x\":4,\"y\":8,\"disable\":false,\"seatRecordId\":null},{\"seatPerId\":null,\"x\":4,\"y\":9,\"disable\":false,\"seatRecordId\":null}],[{\"seatPerId\":null,\"x\":5,\"y\":0,\"disable\":false,\"seatRecordId\":null},{\"seatPerId\":null,\"x\":5,\"y\":1,\"disable\":false,\"seatRecordId\":null},{\"seatPerId\":null,\"x\":5,\"y\":2,\"disable\":false,\"seatRecordId\":null},{\"seatPerId\":null,\"x\":5,\"y\":3,\"disable\":false,\"seatRecordId\":null},{\"seatPerId\":null,\"x\":5,\"y\":4,\"disable\":false,\"seatRecordId\":null},{\"seatPerId\":null,\"x\":5,\"y\":5,\"disable\":false,\"seatRecordId\":null},{\"seatPerId\":null,\"x\":5,\"y\":6,\"disable\":false,\"seatRecordId\":null},{\"seatPerId\":null,\"x\":5,\"y\":7,\"disable\":false,\"seatRecordId\":null},{\"seatPerId\":null,\"x\":5,\"y\":8,\"disable\":false,\"seatRecordId\":null},{\"seatPerId\":null,\"x\":5,\"y\":9,\"disable\":false,\"seatRecordId\":null}],[{\"seatPerId\":null,\"x\":6,\"y\":0,\"disable\":false,\"seatRecordId\":null},{\"seatPerId\":null,\"x\":6,\"y\":1,\"disable\":false,\"seatRecordId\":null},{\"seatPerId\":null,\"x\":6,\"y\":2,\"disable\":false,\"seatRecordId\":null},{\"seatPerId\":null,\"x\":6,\"y\":3,\"disable\":false,\"seatRecordId\":null},{\"seatPerId\":null,\"x\":6,\"y\":4,\"disable\":false,\"seatRecordId\":null},{\"seatPerId\":null,\"x\":6,\"y\":5,\"disable\":false,\"seatRecordId\":null},{\"seatPerId\":null,\"x\":6,\"y\":6,\"disable\":false,\"seatRecordId\":null},{\"seatPerId\":null,\"x\":6,\"y\":7,\"disable\":false,\"seatRecordId\":null},{\"seatPerId\":null,\"x\":6,\"y\":8,\"disable\":false,\"seatRecordId\":null},{\"seatPerId\":null,\"x\":6,\"y\":9,\"disable\":false,\"seatRecordId\":null}],[{\"seatPerId\":null,\"x\":7,\"y\":0,\"disable\":false,\"seatRecordId\":null},{\"seatPerId\":null,\"x\":7,\"y\":1,\"disable\":false,\"seatRecordId\":null},{\"seatPerId\":null,\"x\":7,\"y\":2,\"disable\":false,\"seatRecordId\":null},{\"seatPerId\":null,\"x\":7,\"y\":3,\"disable\":false,\"seatRecordId\":null},{\"seatPerId\":null,\"x\":7,\"y\":4,\"disable\":false,\"seatRecordId\":null},{\"seatPerId\":null,\"x\":7,\"y\":5,\"disable\":false,\"seatRecordId\":null},{\"seatPerId\":null,\"x\":7,\"y\":6,\"disable\":false,\"seatRecordId\":null},{\"seatPerId\":null,\"x\":7,\"y\":7,\"disable\":false,\"seatRecordId\":null},{\"seatPerId\":null,\"x\":7,\"y\":8,\"disable\":false,\"seatRecordId\":null},{\"seatPerId\":null,\"x\":7,\"y\":9,\"disable\":false,\"seatRecordId\":null}],[{\"seatPerId\":null,\"x\":8,\"y\":0,\"disable\":false,\"seatRecordId\":null},{\"seatPerId\":null,\"x\":8,\"y\":1,\"disable\":false,\"seatRecordId\":null},{\"seatPerId\":null,\"x\":8,\"y\":2,\"disable\":false,\"seatRecordId\":null},{\"seatPerId\":null,\"x\":8,\"y\":3,\"disable\":false,\"seatRecordId\":null},{\"seatPerId\":null,\"x\":8,\"y\":4,\"disable\":false,\"seatRecordId\":null},{\"seatPerId\":null,\"x\":8,\"y\":5,\"disable\":false,\"seatRecordId\":null},{\"seatPerId\":null,\"x\":8,\"y\":6,\"disable\":false,\"seatRecordId\":null},{\"seatPerId\":null,\"x\":8,\"y\":7,\"disable\":false,\"seatRecordId\":null},{\"seatPerId\":null,\"x\":8,\"y\":8,\"disable\":false,\"seatRecordId\":null},{\"seatPerId\":null,\"x\":8,\"y\":9,\"disable\":false,\"seatRecordId\":null}],[{\"seatPerId\":null,\"x\":9,\"y\":0,\"disable\":false,\"seatRecordId\":null},{\"seatPerId\":null,\"x\":9,\"y\":1,\"disable\":false,\"seatRecordId\":null},{\"seatPerId\":null,\"x\":9,\"y\":2,\"disable\":false,\"seatRecordId\":null},{\"seatPerId\":null,\"x\":9,\"y\":3,\"disable\":false,\"seatRecordId\":null},{\"seatPerId\":null,\"x\":9,\"y\":4,\"disable\":false,\"seatRecordId\":null},{\"seatPerId\":null,\"x\":9,\"y\":5,\"disable\":false,\"seatRecordId\":null},{\"seatPerId\":null,\"x\":9,\"y\":6,\"disable\":false,\"seatRecordId\":null},{\"seatPerId\":null,\"x\":9,\"y\":7,\"disable\":false,\"seatRecordId\":null},{\"seatPerId\":null,\"x\":9,\"y\":8,\"disable\":false,\"seatRecordId\":null},{\"seatPerId\":null,\"x\":9,\"y\":9,\"disable\":false,\"seatRecordId\":null}]]"; // applyReason="[[{\"seatPerId\":898605032193265664,\"x\":0,\"y\":0,\"disable\":true,\"defect\":false,\"seatRecordId\":900352012082876416},{\\\"seatPerId\\\":null,\\\"x\\\":0,\\\"y\\\":1,\\\"disable\\\":true,\\\"seatRecordId\\\":null}]]"; ArrayList<ArrayList<Seat>> arrayLists = new ArrayList<>(); Seat seat = new Seat(); seat.setSeatPerId(12L); ArrayList<Seat> objects = new ArrayList<>(); objects.add(seat); arrayLists.add(objects); String s = JsonUtil.toJson(arrayLists); System.out.println(s); List<ArrayList> arrayLists1 = JsonUtil.toObjectList(applyReason, ArrayList.class); for (int i = 0; i < arrayLists1.size(); i++) { ArrayList arrayList = arrayLists1.get(i); for (int j = 0; j < arrayList.size(); j++) { Map<String,Object> item = (Map<String, Object>) arrayList.get(j); String seatRecordId = (String) item.get("seatRecordId"); System.out.println(seatRecordId); } } JSONArray jsonArray = JSONArray.parseArray(applyReason); System.out.println(jsonArray.size()); for (int i = 0; i < jsonArray.size(); i++) { JSONArray item = jsonArray.getJSONArray(i); for (int j = 0; j < item.size(); j++) { JSONObject seatS = (JSONObject)item.get(j); System.out.println(seatS.get("seatRecordId")); } } // System.out.println(seats.size()); } public static ArrayList<Seat> seatArrayList(String applyReason)throws Exception{ ArrayList<Seat> seatArrayList=new ArrayList<>(); JSONArray jsonArray = JSONArray.parseArray(applyReason); System.out.println(jsonArray.size()); for (int i = 0; i < jsonArray.size(); i++) { JSONArray item = jsonArray.getJSONArray(i); for (int j = 0; j < item.size(); j++) { JSONObject seatS = (JSONObject)item.get(j); Seat seatS1=new Seat(); seatS1.setDefect((Boolean) seatS.get("defect")); seatS1.setDisable((Boolean) seatS.get("disable")); seatS1.setSeatPerId((Long) seatS.get("seatPerId")); seatS1.setSeatRecordId((Long) seatS.get("seatRecordId")); seatS1.setX((Integer) seatS.get("x")); seatS1.setY((Integer) seatS.get("y")); seatArrayList.add(seatS1); } } return seatArrayList; } public static Seat seat(String applyReason,String seatPerId) throws Exception{ Seat seatS1=new Seat(); ArrayList<Seat> seatArrayList=new ArrayList<>(); JSONArray jsonArray = JSONArray.parseArray(applyReason); for (int i = 0; i < jsonArray.size(); i++) { JSONArray item = jsonArray.getJSONArray(i); for (int j = 0; j < item.size(); j++) { JSONObject seatS = (JSONObject)item.get(j); if(seatS.get("seatPerId").toString().equals(seatPerId)){ seatS1.setDefect((Boolean) seatS.get("defect")); seatS1.setDisable((Boolean) seatS.get("disable")); seatS1.setSeatPerId((Long) seatS.get("seatPerId")); seatS1.setSeatRecordId((Long) seatS.get("seatRecordId")); seatS1.setX((Integer) seatS.get("x")); seatS1.setY((Integer) seatS.get("y")); return seatS1; } } } return seatS1; } public static ArrayList<Seat> seatArrayListXY(String applyReason, String x, String y,Boolean disable) throws Exception { ArrayList<Seat> seatArrayList=new ArrayList<>(); JSONArray jsonArray = JSONArray.parseArray(applyReason); System.out.println(jsonArray.size()); for (int i = 0; i < jsonArray.size(); i++) { JSONArray item = jsonArray.getJSONArray(i); for (int j = 0; j < item.size(); j++) { JSONObject seatS = (JSONObject)item.get(j); if(seatS.get("x").toString().equals(x)&&seatS.get("y").toString().equals(y)){ Seat seatS1=new Seat(); seatS1.setDefect((Boolean) seatS.get("defect")); seatS1.setDisable(disable); seatS1.setSeatPerId((Long) seatS.get("seatPerId")); seatS1.setSeatRecordId((Long) seatS.get("seatRecordId")); seatS1.setX((Integer) seatS.get("x")); seatS1.setY((Integer) seatS.get("y")); seatArrayList.add(seatS1); } } } return seatArrayList; } public static ArrayList<Seat> seatArrayListDefect(String applyReason, String x, String y,Boolean defect) throws Exception { ArrayList<Seat> seatArrayList=new ArrayList<>(); JSONArray jsonArray = JSONArray.parseArray(applyReason); System.out.println(jsonArray.size()); for (int i = 0; i < jsonArray.size(); i++) { JSONArray item = jsonArray.getJSONArray(i); for (int j = 0; j < item.size(); j++) { JSONObject seatS = (JSONObject)item.get(j); if(seatS.get("x").toString().equals(x)&&seatS.get("y").toString().equals(y)){ Seat seatS1=new Seat(); seatS1.setDefect(defect); seatS1.setDisable((Boolean) seatS.get("disable")); seatS1.setSeatPerId((Long) seatS.get("seatPerId")); seatS1.setSeatRecordId((Long) seatS.get("seatRecordId")); seatS1.setX((Integer) seatS.get("x")); seatS1.setY((Integer) seatS.get("y")); seatArrayList.add(seatS1); } } } return seatArrayList; } }
package com.demo.common.util.json.exception; public class JsonUtilErrorException extends RuntimeException { public JsonUtilErrorException(String message, Throwable cause) { super(message, cause); } }