scala ujson.read() returns ujson.Obj

scala ujson.read() returns ujson.Obj

我正在尝试使用 Li Haoyi 的 ujson 读取 json 字符串。这是字符串:

{
  "dataflows": [
    {
      "name": "test",
      "sources": [
        {
          "name": "person_inputs",
          "path": "/data/input/events/person/*",
          "format": "JSON"
        }
      ],
      "transformations": [
        {
          "name": "validation",
          "type": "validate_fields",
          "params": {
            "input": "person_inputs",
            "validations": [
              {
                "field": "office",
                "validations": [
                  "notEmpty"
                ]
              },
              {
                "field": "age",
                "validations": [
                  "notNull"
                ]
              }
            ]
          }
        },
        {
          "name": "ok_with_date",
          "type": "add_fields",
          "params": {
            "input": "validation_ok",
            "addFields": [
              {
                "name": "dt",
                "function": "current_timestamp"


              }
            ]
          }
        }
      ],
      "sinks": [
        {
          "input": "ok_with_date",
          "name": "raw-ok",
          "paths": [
            "/data/output/events/person"
          ],
          "format": "JSON",
          "saveMode": "OVERWRITE"
        },
        {
          "input": "validation_ko",
          "name": "raw-ko",
          "paths": [
            "/data/output/discards/person"
          ],
          "format": "JSON",
          "saveMode": "OVERWRITE"
        }
      ]
    }
  ]
}

我是这样读的:

val j = os.read(os.pwd/RelPath("src/main/scala/metadata.json"))
val jsonData = ujson.read(j)

但是,return 类型是ujson.Obj,而不是预期的Arr(ArrayBuffer(Obj),这样当我尝试获取jsonData(0),我得到的是json.Value$InvalidData: Expected ujson.Arr.

我问这个问题是因为我曾尝试使用 ujson 对象创建一个 upickle 对象,但我不能,我怀疑这是因为这个初始错误。

知道为什么会这样吗?任何帮助将不胜感激!提前致谢!!

你的JSON的外层元素不是数组,它是一个对象,只有一个元素dataflows,其值是一个数组。试试 jsonData("dataflows")(0).