feat: 新增预测模型接口
This commit is contained in:
65
src/test/java/mjkf/xinke/GetPredictDataTest.java
Normal file
65
src/test/java/mjkf/xinke/GetPredictDataTest.java
Normal file
@@ -0,0 +1,65 @@
|
||||
package mjkf.xinke;
|
||||
|
||||
import cn.hutool.core.io.FileUtil;
|
||||
import cn.hutool.core.io.file.FileReader;
|
||||
import cn.hutool.json.JSONException;
|
||||
import cn.hutool.json.JSONObject;
|
||||
import cn.hutool.json.JSONUtil;
|
||||
import lombok.Data;
|
||||
|
||||
import java.io.File;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
public class GetPredictDataTest {
|
||||
|
||||
@org.junit.Test
|
||||
public void test() throws Exception {
|
||||
// 指定目录路径
|
||||
String directoryPath = "C:\\Users\\Administrator\\Documents\\WeChat Files\\wslshanlin\\FileStorage\\File\\2024-07";
|
||||
// 获取目录下的所有文件
|
||||
List<File> files = FileUtil.loopFiles(directoryPath, file -> file.getName().endsWith(".json"));
|
||||
for (File file : files) {
|
||||
// 读取文件内容
|
||||
FileReader fileReader = new FileReader(file);
|
||||
String jsonContent = fileReader.readString();
|
||||
// 解析 JSON 内容为 JSONObject
|
||||
JSONObject jsonObject;
|
||||
try {
|
||||
jsonObject = JSONUtil.parseObj(jsonContent);
|
||||
parseJson(jsonObject);
|
||||
// 打印解析后的 JSONObject
|
||||
System.out.println("File: " + file.getName());
|
||||
System.out.println("JSONObject: " + jsonObject);
|
||||
} catch (JSONException e) {
|
||||
System.out.println("JSONException: " + file.getName());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private List<DataPredict> parseJson(JSONObject jsonObject) {
|
||||
var month = jsonObject.getStr("预测月份");
|
||||
var data = jsonObject.getJSONArray("数据");
|
||||
var result = new ArrayList<DataPredict>();
|
||||
for (var item: data) {
|
||||
var dataPredict = new DataPredict((JSONObject)item);
|
||||
result.add(dataPredict);
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
@Data
|
||||
class DataPredict {
|
||||
private Integer year;
|
||||
private Integer month;
|
||||
private Double price;
|
||||
private String materialId;
|
||||
private String name;
|
||||
public DataPredict () {}
|
||||
public DataPredict (JSONObject item) {
|
||||
this.materialId = item.getStr("材料编号");
|
||||
this.name = item.getStr("材料名称");
|
||||
this.price = item.getDouble("预测价格");
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user