•In entities, files similar to dictionary tables are usually used to represent properties. Most of the files are configured in configuration files, or they can be static files. This time, we record how to read the required fields from the static json file.
1. File format and path
2. Load the file
import org.springframework.beans.factory.annotation.Value;import org.springframework.core.io.Resource;@Value("classpath:static/data/area.json")private Resource areaRes;3. Read the file
Note: Because Chinese exists when reading the file, you need to set the encoding format.
@Override public void test(){ for (int i = 1; i < 8; i ++) { try { String areaData = IOUtils.toString(areaRes.getInputStream(), Charset.forName("UTF-8")); List<String> districtNames = JsonPath.read(areaData, "$.districts[?(@.id == " + i + ")].name"); String district = districtNames.get(0); System.out.println("number"+i+" represents the administrative region as:" + district); }catch (IOException e){ e.printStackTrace(); } } }Output result
The administrative district represented by the number 1 is: the administrative district represented by the number 2 in Yaohai District is: the administrative district represented by the number 3 in Luyang District is: the administrative district represented by the number 4 in Shushan District is: the administrative district represented by the number 5 in Baohe District is: the administrative district represented by the number 6 in Economic and Technological Development Zone is: the administrative district represented by the number 7 in High-tech Industrial Development Zone is: Xinzhan High-tech Industrial Development Zone
Summarize
The above is the fields required for reading data from static json files introduced by the editor. I hope it will be helpful to everyone. If you have any questions, please leave me a message and the editor will reply to everyone in time. Thank you very much for your support to Wulin.com website!