一、在maven项目中导入依赖
<!--yaml文件转化成json格式-->
<dependency>
<groupId>org.yaml</groupId>
<artifactId>snakeyaml</artifactId>
<version>1.29</version>
</dependency>
<!--json格式转化-->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.4</version>
</dependency>
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.13.0</version>
</dependency>
二、编辑yanl文件
apiName: token
description: 获取用户登录测试环境需要的token
url: http://cas-uat.kye.com
request:
method: POST
path: /cas_server/v2/oauth/token
headers:
Content-Type: application/json
body:
service: http://edge.uat.erp.com/#/
account: 612528
password: i7ytg4VKUaOAnzQvBw6hlixDQgz5
device_id: fb516938c8227cc5ca1ec5e0be661a34
app_id: gj_xdbvffz-hYOdFXSRlMg==
grant_type: password
data: mJ2T6sNCN1koP808YI6SCw3guuKr
validate:
status_code: 200
response_body:
- key: success
value: true
type: string
- key: code
value: 0
type: integer
三、读取yaml文件并转化成json格式字符串
/**
* @param filename yaml文件名称
* @return 返回yaml文件中对应的json数据
*/
public static String yamlToJson(String filename) {
String jsonData;
try {
// 创建Yaml对象
Yaml yaml = new Yaml();
// 打开文件输入流
FileInputStream input = new FileInputStream(filename);
// 读取整个文件为一个Map对象,如果yaml文件为列表,则数据类型为list
Map<String, Object> 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字符串中制定的key值
//处理json格式数据
public static String handleJsonResponse(String response, String field) {
if (StringUtils.isBlank(field)) {
return response;
}
//将json格式的字符串转化成json对象
JSONObject jsonObject = JSON.parseObject(response);
return getFieldValue(jsonObject, field);
}
//提取json对象中某个字段的值
public static String getFieldValue(JSONObject jsonObject, String fieldName) {
for (String key : jsonObject.keySet()) {
Object value = jsonObject.get(key);
if (key.equals(fieldName)) {
return value.toString();
} else if (value instanceof JSONObject) {
String fieldValue = getFieldValue((JSONObject) value, fieldName);
if (fieldValue != null) {
return fieldValue;
}
} else if (value instanceof JSONArray jsonArray) {
String fieldValue = getFieldValue(jsonArray, fieldName);
if (fieldValue != null) {
return fieldValue;
}
}
}
return null;
}
//提取json对象中某个字段的值
public static String getFieldValue(JSONArray jsonArray, String fieldName) {
for (Object objectValue : jsonArray) {
if (objectValue instanceof JSONObject) {
String fieldValue = getFieldValue((JSONObject) objectValue, fieldName);
if (fieldValue != null) {
return fieldValue;
}
} else if (objectValue instanceof JSONArray) {
String fieldValue = getFieldValue((JSONArray) objectValue, fieldName);
if (fieldValue != null) {
return fieldValue;
}
}
}
return null;
}
标签:return,String,yaml,json,fieldName,java,fieldValue
From: https://www.cnblogs.com/faith007/p/17925764.html