共计 2297 个字符,预计需要花费 6 分钟才能阅读完成。
package me.zhengjie;
import cn.hutool.core.collection.ListUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.core.io.Resource;
import org.springframework.test.context.junit4.SpringRunner;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.HashMap;
import java.util.List;
import java.util.stream.Collectors;
@RunWith(SpringRunner.class)
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
public class EladminSystemApplicationTests {private final static List<String> managerList = ListUtil.toList("User","Role","Timing","Menu");
private final static List<String> guesterList = ListUtil.toList("Dept");
private final static HashMap<String,List<String>> hashMap = new HashMap<String,List<String>>(){{put("manager",managerList);
put("guester",guesterList);
}};
@Value(value="classpath:menu.json")
private Resource resource;
@Test
public void contextLoads() throws IOException {BufferedReader br = new BufferedReader(new InputStreamReader(resource.getInputStream()));
StringBuffer message=new StringBuffer();
String line = null;
while((line = br.readLine()) != null) {message.append(line);
}
String defaultString=message.toString();
String result=defaultString.replaceAll("","");
JSONArray objects = JSON.parseArray(result);
JSONArray children = objects.getJSONObject(0).getJSONArray("children");
JSONArray jResult = new JSONArray();
List<String> manager = hashMap.get("manager");
children.forEach(i ->{JSONObject json = (JSONObject) i;
for (String key :manager){if (key.equals(json.getString("name"))){System.out.println("add -" + key);
}else {jResult.add(json);
}
}
});
System.out.println(jResult.toJSONString());
// List<String> manager = hashMap.get("manager");
//
// JSONArray jResult = null;
// for (String key :manager){// jResult = children.stream().filter(i -> {// JSONObject json = (JSONObject)i;
// String name = json.getString("name");
// System.out.println(name + "===" + key);
// if (name.equals(key)){
// return false;
// }else {
// return true;
// }
// }).collect(Collectors.toCollection(JSONArray::new));
// }
//
// System.out.println(manager.toString());
// System.out.println(jResult);
}
public static void main(String[] args) {}}
正文完
发表至: javascript
2021-08-26