【问题标题】:How to read reference $ref of JSON file? [duplicate]如何读取 JSON 文件的参考 $ref? [复制]
【发布时间】:2017-10-12 11:45:41
【问题描述】:
{
  "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 文件位置并将该 json 写入地图 请帮助我如何阅读。 我已经可以像

一样读取主文件了
File jsonInputFile = new File("location");
System.out.println("Keys : " + entry2.getKey());

【问题讨论】:

  • 可以使用gson,查看以下链接github.com/google/gson
  • 什么是entry2
  • 所有的json解析都是一样的。从顶部开始。知道什么是对象和数组。这里可以没有数组,所以customerId应该很容易得到,而且是内部ref

标签: java json jackson


【解决方案1】:

您可以尝试下面的代码来获取 $ref 值

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();
        }
    }
}

输出是

abc.def.xyz.1.json#

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-08-15
    • 1970-01-01
    • 1970-01-01
    • 2016-07-06
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多