diff --git a/.idea/jsonSchemas.xml b/.idea/jsonSchemas.xml
new file mode 100644
index 0000000..44c4a03
--- /dev/null
+++ b/.idea/jsonSchemas.xml
@@ -0,0 +1,26 @@
+
+
+
+
+
+
+
+
\ No newline at end of file
diff --git a/.idea/php.xml b/.idea/php.xml
new file mode 100644
index 0000000..7c9e914
--- /dev/null
+++ b/.idea/php.xml
@@ -0,0 +1,8 @@
+
+
+
+
+
+
+
+
\ No newline at end of file
diff --git a/pom.xml b/pom.xml
index 695e51a..c65eb05 100644
--- a/pom.xml
+++ b/pom.xml
@@ -22,6 +22,23 @@
system
${project.basedir}/libs/snakeyaml-2.6.jar
+
+
+ com.alibaba
+ fastjson
+ 1.2.83
+
+
+ org.apache.httpcomponents
+ httpclient
+ 4.5.13
+
+
+ com.fasterxml.jackson.core
+ jackson-databind
+ 2.13.4.2
+
+
diff --git a/src/main/java/main/YamlLoad/json.java b/src/main/java/main/YamlLoad/json.java
new file mode 100644
index 0000000..6bf1fec
--- /dev/null
+++ b/src/main/java/main/YamlLoad/json.java
@@ -0,0 +1,70 @@
+package main.YamlLoad;
+
+import com.alibaba.fastjson.JSONArray;
+import com.alibaba.fastjson.JSONObject;
+import com.fasterxml.jackson.databind.ObjectMapper;
+import com.fasterxml.jackson.databind.SerializationFeature;
+import org.yaml.snakeyaml.Yaml;
+
+import java.io.FileInputStream;
+import java.util.ArrayList;
+import java.util.List;
+import java.util.Map;
+
+import static main.util.tools.echo;
+
+public class json {
+ public static String yamlToJson(String filename) {
+ String jsonData;
+ try {
+ // 创建Yaml对象
+ Yaml yaml = new Yaml();
+ // 打开文件输入流
+ FileInputStream input = new FileInputStream(filename);
+ // 读取整个文件为一个Map对象,如果yaml文件为列表,则数据类型为list
+ Map data = yaml.load(input);
+ // 创建ObjectMapper对象用于将数据转换为JSON
+ ObjectMapper mapper = new ObjectMapper();
+ // 启用格式化输出
+ mapper.enable(SerializationFeature.INDENT_OUTPUT);
+ // 将数据转换为JSON字符串
+ jsonData = mapper.writeValueAsString(data);
+ // 返回读取的数据
+ return jsonData;
+ } catch (Exception e) {
+ e.printStackTrace();
+ }
+ return null;
+ }
+
+ // 解析Json数据,输入JsonKey返回JsonValue
+ public static String getJsonValue(String Json, String JsonKey) {
+ try {
+ Object jsonObject = JSONObject.parse(Json);
+
+ String[] keys = JsonKey.split("\\.");
+ Object currentValue = jsonObject;
+
+ for (String key : keys) {
+ if (currentValue instanceof JSONObject) {
+ currentValue = ((JSONObject) currentValue).get(key);
+ } else if (currentValue instanceof JSONArray) {
+ int index = Integer.parseInt(key);
+ currentValue = ((JSONArray) currentValue).get(index);
+ } else {
+ return null;
+ }
+
+ if (currentValue == null) {
+ return null;
+ }
+ }
+
+ return currentValue.toString();
+ } catch (Exception e) {
+ e.printStackTrace();
+ return null;
+ }
+ }
+
+}
diff --git a/src/main/java/main/YamlLoad/yaml.java b/src/main/java/main/YamlLoad/yaml.java
deleted file mode 100644
index 932db6f..0000000
--- a/src/main/java/main/YamlLoad/yaml.java
+++ /dev/null
@@ -1,26 +0,0 @@
-package main.YamlLoad;
-
-import org.yaml.snakeyaml.Yaml;
-import java.io.FileInputStream;
-import java.io.InputStream;
-import java.util.Map;
-
-public class yaml {
- public String StringYaml(String YamlFile, String GetKey) {
- String YamlValue = "";
-
- try (InputStream input = new FileInputStream(YamlFile)) {
- Yaml yaml = new Yaml();
- Map data = yaml.load(input);
-
- if (data != null && data.containsKey(GetKey)) {
- Object value = data.get(GetKey);
- YamlValue = value != null ? value.toString() : "";
- }
- } catch (Exception e) {
- throw new RuntimeException("读取YAML文件失败: " + e.getMessage(), e);
- }
-
- return YamlValue;
- }
-}
diff --git a/src/main/java/main/main.java b/src/main/java/main/main.java
index 5c6e009..f78dd2c 100644
--- a/src/main/java/main/main.java
+++ b/src/main/java/main/main.java
@@ -1,6 +1,7 @@
package main;
-import main.YamlLoad.yaml;
+import static main.YamlLoad.json.getJsonValue;
+import static main.YamlLoad.json.yamlToJson;
import static main.util.tools.echo;
@@ -11,25 +12,30 @@ public class main {
boolean debug = new main().debug;
if (debug) {
// 打印当前运行环境的系统类型和版本
- System.out.println("系统类型: " + System.getProperty("os.name") + " " + "系统版本: " + System.getProperty("os.version"));
- System.out.println("Java版本: " + System.getProperty("java.version"));
- System.out.println("Java Vendor版本: " + System.getProperty("java.vendor"));
- System.out.println("Java Home位置: " + System.getProperty("java.home"));
- System.out.println("Java Class位置: " + System.getProperty("java.class.path"));
- System.out.println("运行的用户: " + System.getProperty("user.name"));
+ System.out.println("系统类型: " + System.getProperty("os.name") + "\n");
+ System.out.println("系统版本: " + System.getProperty("os.version") + "\n");
+ System.out.println("Java版本: " + System.getProperty("java.version") + "\n");
+ System.out.println("Java Vendor版本: " + System.getProperty("java.vendor") + "\n");
+ System.out.println("Java Home位置: " + System.getProperty("java.home") + "\n");
+ System.out.println("Java Class位置: " + System.getProperty("java.class.path") + "\n");
+ System.out.println("运行的用户: " + System.getProperty("user.name") + "\n");
// 获取当前的运行目录
- System.out.println("当前运行目录: " + System.getProperty("user.dir"));
+ System.out.println("当前运行目录: " + System.getProperty("user.dir") + "\n");
}
- echo("\nHello World!\n");
+ echo("Hello World!\n");
new main().YamlTest();
}
private void YamlTest() {
- yaml y = new yaml();
- String YamlValue = y.StringYaml("test.yml", "test");
- System.out.println(YamlValue);
+ String json = yamlToJson("test.yaml");
+ echo("Yaml解析Json: \n" + json);
+ String yaml = getJsonValue(json, "yaml");
+ String config = getJsonValue(yaml, "config");
+ String string = getJsonValue(config, "string");
+ String bool = getJsonValue(config, "bool");
+ echo("Json解析的内容: \n" + string + "\n" + bool);
}
public static String Version() {
diff --git a/src/main/resources/json.java b/src/main/resources/json.java
new file mode 100644
index 0000000..3e970c4
--- /dev/null
+++ b/src/main/resources/json.java
@@ -0,0 +1,26 @@
+package main.YamlLoad;
+
+import com.fasterxml.jackson.databind.ObjectMapper;
+import com.fasterxml.jackson.databind.SerializationFeature;
+import org.yaml.snakeyaml.Yaml;
+
+import java.io.FileInputStream;
+import java.util.Map;
+import java.util.Objects;
+
+public class json {
+ public static String YamlToJson(String FileName) {
+ String jsonData;
+ try {
+ Yaml yaml = new Yaml();
+ FileInputStream input =new FileInputStream(FileName);
+ Map data = yaml.load(input);
+ ObjectMapper mapper = new ObjectMapper();
+ mapper.enable(SerializationFeature.INDENT_OUTPUT);
+ jsonData = mapper.writeValueAsString(data);
+ }catch (Exception err) {
+ err.printStackTrace();
+ }
+ return jsonData;
+ }
+}
diff --git a/test.yaml b/test.yaml
index eb574e8..b9e9cf4 100644
--- a/test.yaml
+++ b/test.yaml
@@ -1 +1,4 @@
-test: 123456
\ No newline at end of file
+yaml:
+ config:
+ string: 123456
+ bool: true
\ No newline at end of file
diff --git a/test.yml b/test.yml
deleted file mode 100644
index eb574e8..0000000
--- a/test.yml
+++ /dev/null
@@ -1 +0,0 @@
-test: 123456
\ No newline at end of file