读取json文件

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.HashMap;
import java.util.List;

import com.alibaba.fastjson.JSON;
public class Demo {
        public static void main(String[] args){
            String json = "null";
            try {
                json = readJsonData("C:\\Users\\0\\Desktop\\数据接入\\机组日指标.txt");
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            List<HashMap> list =JSON.parseArray(json, HashMap.class);
            for(int i=0;i<list.size();i++){
              System.out.println("insert into jizurizhibiao VALUES "+"("+"'"+list.get(i).get("code")+"'"+","+"'"+list.get(i).get("unit")+"'"+","+"'"+list.get(i).get("date")+"'"+","+list.get(i).get("value")+","+"'"+list.get(i).get("name")+"'"+","+"'"+list.get(i).get("id")+"'"+")"+";");;
            }
        }
        
        public static String readJsonData(String pactFile) throws IOException {
            // 读取文件数据
            //System.out.println("读取文件数据util");
            
            StringBuffer strbuffer = new StringBuffer();
            File myFile = new File(pactFile);//"D:"+File.separatorChar+"DStores.json"
            if (!myFile.exists()) {
                System.err.println("Can't Find " + pactFile);
            }
            try {
                FileInputStream fis = new FileInputStream(pactFile);
                InputStreamReader inputStreamReader = new InputStreamReader(fis, "UTF-8");
                BufferedReader in  = new BufferedReader(inputStreamReader);
                
                String str;
                while ((str = in.readLine()) != null) {
                    strbuffer.append(str);  //new String(str,"UTF-8")
                }
                in.close();
            } catch (IOException e) {
                e.getStackTrace();
            }
            //System.out.println("读取文件结束util");
            return strbuffer.toString();
        }
        
    

}

Spring Boot 读写 JSON 文件通常涉及使用 Jackson 库,它是 Spring Boot 默认的 JSON 处理工具。在 Spring Boot 应用中,可以通过以下步骤来读写 JSON 文件: 1. 创建数据模型:首先定义 Java 类来表示 JSON 数据结构。Jackson 会自动将这些类的实例序列化为 JSON 格式,或者将 JSON 反序列化为这些类的实例。 ```java public class User { private String name; private int age; // getter 和 setter 方法 // 构造函数 } ``` 2. 自动配置:Spring Boot 会自动配置 Jackson,并将 Java 对象转换为 JSON 格式。因此,通常不需要额外的配置。 3. 写入 JSON 文件: - 使用 `ObjectMapper` 类的 `writeValue` 方法可以将 Java 对象写入到 JSON 文件中。 ```java import com.fasterxml.jackson.databind.ObjectMapper; import java.nio.file.Files; import java.nio.file.Paths; import java.io.IOException; // ... ObjectMapper objectMapper = new ObjectMapper(); User user = new User("张三", 30); // 将对象写入到指定的文件路径 objectMapper.writeValue(Paths.get("user.json").toFile(), user); ``` 4. 读取 JSON 文件: - 使用 `ObjectMapper` 类的 `readValue` 方法可以将 JSON 文件内容转换为 Java 对象。 ```java import com.fasterxml.jackson.databind.ObjectMapper; // ... ObjectMapper objectMapper = new ObjectMapper(); User user = objectMapper.readValue(new File("user.json"), User.class); ``` 确保在项目中加入了 Jackson 的依赖,如果你使用 Maven,可以在 `pom.xml` 文件中添加如下依赖: ```xml <dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-databind</artifactId> <version>2.13.0</version> </dependency> ``` 确保使用与项目匹配的版本号。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值