@Test public void doubleS() { String s = "[{\"阿杰\":254.18813760723296},{\"爸爸\":124.01170081383111},{\"宝宝\":69.11225944625576},{\"父母\":67.96744799486767},{\"孩子\":53.443821294871626},{\"游戏\":52.93198900792951},{\"妈妈\":52.59076748330145},{\"广州\":46.073973611627814}]"; JSONArray objects = JSON.parseArray(s.toString()); Map map = new HashMap(); for (Object object : objects) { System.out.println(object.toString()); JSONObject obj = JSONObject.parseObject(object.toString()); for (String key : obj.keySet()) { System.out.println(key + ":" + obj.getString(key)); map.put(key, obj.get(key)); } } }
/** * JsonObject 转map * * @param jsonObject * @return * @author Roy<*****@*****.**> * @since 2016年2月2日 */ public static Map<String, Object> toMap(JSONObject jsonObject) { Map<String, Object> map = new HashMap<>(); Set<String> keys = jsonObject.keySet(); if (CollectionUtils.isEmpty(keys)) { return null; } for (String key : keys) { Object value = jsonObject.get(key); if (value instanceof JSONArray) { value = toList((JSONArray) value); } else if (value instanceof JSONObject) { value = toMap((JSONObject) value); } map.put(key, value); } return map; }
private boolean satisfy(JSONObject rule, JSONObject user) { String key = null, value = null; for (String k : rule.keySet()) { key = k; value = rule.getString(k); } // 左参数的真实值集合 Set<Object> leftValueSet = getRealValues(key, user); // 右参数的真实值集合 Set<Object> rightValueSet = getRealValues(value.substring(1), user); switch (value.charAt(0)) { case '*': return !leftValueSet.isEmpty(); case '!': return !leftValueSet.isEmpty() && !rightValueSet.isEmpty() && leftValueSet.stream().noneMatch(v -> rightValueSet.contains(v)); case '=': return leftValueSet.stream().anyMatch(v -> rightValueSet.contains(v)); } throw new MalformedConfigException("At configuration rules.properties"); }
public JSONArray generate(JSONObject user) { lock.readLock().lock(); JSONArray array = new JSONArray(); try { array.addAll( nameRules .keySet() .stream() .map(ruleKey -> nameRules.getJSONArray(ruleKey)) .filter(rules -> check(rules, user)) .map(rules -> rules.getString(0)) .flatMap(rule -> groups(rule, user).parallelStream()) .map(value -> appendPostfix(value, user)) .collect(Collectors.toList())); array.add(user.getString("nickname")); array.add("匿名用户"); } catch (Exception e) { throw e; } finally { lock.readLock().unlock(); } return array; }