FastJson读取resources下的json文件并且转成对象
读取resources下的json文件并且转成对象
json文件路径是:
读取代码
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.TypeReference;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.io.IOUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.io.Resource;
import org.springframework.core.io.ResourceLoader;
import org.springframework.web.bind.annotation.*;
import java.io.IOException;
import java.io.InputStream;
import java.net.URISyntaxException;
import java.nio.charset.StandardCharsets;
import java.util.Collections;
import java.util.List;
import java.util.Map;
@Autowired
private ResourceLoader resourceLoader;
/**
* @return
*/
@GetMapping(value = "/test01")
public List<JSONObject> test() throws URISyntaxException, IOException {
Resource resource = resourceLoader.getResource("classpath:test/TestSimulatedSessionData.json");
String jsonContent;
try (InputStream inputStream = resource.getInputStream()) {
jsonContent = IOUtils.toString(inputStream, StandardCharsets.UTF_8);
}
return JSON.parseObject(jsonContent, new TypeReference<List<JSONObject>>() {
});
}