Java 如何读取JSON文件的引用$ref?
。。。。。更多的钥匙。。。。。。。。 我需要进入$ref文件位置,并将json放入一个映射中 请帮助我学会阅读。 我已经能够像这样读取主文件了Java 如何读取JSON文件的引用$ref?,java,json,jackson,Java,Json,Jackson,。。。。。更多的钥匙。。。。。。。。 我需要进入$ref文件位置,并将json放入一个映射中 请帮助我学会阅读。 我已经能够像这样读取主文件了 { "description": "Something", "id": "abc.def.xyzjson#", "type": "object", "properties": { "triggerTime": { "description": "Something", "$ref": "abc.def.xy
{
"description": "Something",
"id": "abc.def.xyzjson#",
"type": "object",
"properties": {
"triggerTime": {
"description": "Something",
"$ref": "abc.def.xyz.1.json#"
"required": true
},
"customerId": {
"type": "string",
"description": "Something",
"$ref": "abc.def.xyz.1.json#"
},
您可以尝试下面的代码来获取$ref值
File jsonInputFile = new File("location");
System.out.println("Keys : " + entry2.getKey());
输出为
abc.def.xyz.1.json#
您可以使用gson,检查以下链接什么是
entry2
?所有json解析都是相同的。从顶部开始。知道什么是对象和数组。这里可以没有数组,所以应该很容易获得customerId,它是内部引用
import org.json.simple.JSONArray;
import org.json.simple.JSONObject;
import org.json.simple.parser.JSONParser;
public class Test{
public static void main(String[] args) {
JSONParser parser = new JSONParser();
try {
Object obj = parser.parse(new FileReader(
"File location"));
JSONObject jsonObject = (JSONObject) obj;
JSONObject properties = (JSONObject) jsonObject.get("properties");
JSONObject triggerTime = (JSONObject) properties.get("triggerTime");
String ref = (String) triggerTime.get("$ref");//You can load this file to a map if required.
System.out.println(ref);
} catch (Exception e) {
e.printStackTrace();
}
}
}