• Em entidades, arquivos semelhantes às tabelas de dicionário geralmente são usados para representar propriedades. A maioria dos arquivos é configurada em arquivos de configuração ou pode ser arquivos estáticos. Desta vez, registramos como ler os campos necessários do arquivo JSON estático.
1. Formato de arquivo e caminho
2. Carregue o arquivo
importar org.springframework.beans.factory.annotation.value; importar org.springframework.core.io.resource; @Value ("ClassPath: static/data/are.json") recursos privados areares;3. Leia o arquivo
Nota: Como existe chinês ao ler o arquivo, você precisa definir o formato de codificação.
@Override public void test () {for (int i = 1; i <8; i ++) {try {string Areadata = ioutils.toString (Areares.getInputStream (), charset.formorne ("utef-8"); List <String> distritoNames = jsonPath.read (Areadata, "$ .Districts [? (@. Id ==" + i + ")]. Nome"); String distrito = DistrictNames.get (0); System.out.println ("número"+i+"representa a região administrativa como:"+distrito); } catch (ioexception e) {e.printStackTrace (); }}}Resultado de saída
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 Zona de Desenvolvimento Industrial é: Zona de Desenvolvimento Industrial de alta tecnologia Xinzhan
Resumir
O exposto acima são os campos necessários para a leitura de dados dos arquivos JSON estáticos introduzidos pelo editor. Espero que seja útil para todos. Se você tiver alguma dúvida, deixe -me uma mensagem e o editor responderá a todos a tempo. Muito obrigado pelo seu apoio ao site wulin.com!