- 首页 > it技术 > >
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 managerList = ListUtil.toList("User","Role","Timing","Menu");
private final static List guesterList = ListUtil.toList("Dept");
private final static HashMap> hashMap = new HashMap>(){{
put("manager",managerList);
put("guester",guesterList);
}};
@Value(value="https://www.it610.com/article/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 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 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) {
}
}
推荐阅读