Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/asp.net-core/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用Scala Argonaut解析JSON数组_Json_Scala_Argonaut - Fatal编程技术网

使用Scala Argonaut解析JSON数组

使用Scala Argonaut解析JSON数组,json,scala,argonaut,Json,Scala,Argonaut,我正在使用Scala&Argonaut,试图解析以下JSON: [ { "name": "apple", "type": "fruit", "size": 3 }, { "name": "jam", "type": "condiment", "size": 5 }, { "name": "beef", "type": "meat"

我正在使用Scala&Argonaut,试图解析以下JSON:

[
    {
        "name": "apple",
        "type": "fruit",
        "size": 3
    },
    {
        "name": "jam",
        "type": "condiment",
        "size": 5
    },
    {
        "name": "beef",
        "type": "meat",
        "size": 1
    }
]
努力研究如何迭代并将值提取到
列表[MyType]
中,其中
MyType
将具有名称、类型和大小属性

我将很快发布更具体的代码(我尝试了很多东西),但基本上我希望了解光标如何工作,以及如何遍历数组等。我尝试使用
\\
(downArray)移动到数组的头部,然后
:->-
遍历数组,然后
-\
(downField)不可用(至少IntelliJ不这么认为)。 所以问题是我如何:

  • 导航到阵列
  • 遍历数组(并知道何时完成)
  • 为每个字段提取字符串、整数等值-
    jdecode[string]
    <代码>作为[字符串]

    • 最简单的方法是为
      MyType
      定义一个编解码器。然后,编译器将愉快地为
      列表[MyType]
      等构建一个解码器。我将在这里使用一个普通类(而不是case类)来明确发生了什么:

      class MyType(val name: String, val tpe: String, val size: Int)
      
      import argonaut._, Argonaut._
      
      implicit def MyTypeCodec: CodecJson[MyType] = codec3(
        (name: String, tpe: String, size: Int) => new MyType(name, tpe, size),
        (myType: MyType) => (myType.name, myType.tpe, myType.size)
      )("name", "type", "size")
      
      codec3
      采用两个参数列表。第一个参数有两个参数,可以告诉您如何从
      Tuple3
      创建
      MyType
      的实例,反之亦然。第二个参数列表用于指定字段的名称

      现在您可以编写如下内容(如果
      json
      是您的字符串):


      您已经完成了。

      因为您不需要编码,只需要查看解码,您可以按照Travis的建议执行,但可以实现另一个隐式:MyTypeDecodeJson

      implicit def MyTypeDecodeJson: DecodeJson[MyType] = DecodeJson(
          raw => for {
          name     <- raw.get[String]("name")
          type     <- raw.get[String]("type")
          size     <- raw.get[Int]("size")
        } yield MyType(name, type, size))
      

      假设
      MyType
      是一个case类,下面的方法也适用:

      case class MyType(name: String, type: String, size: Int)
      
      object MyType {
          implicit val createCodecJson: CodecJson[MyType] = CodecJson.casecodec3(apply, unapply)(
              "name",
              "type",
              "size"
          )
      }
      

      你试了什么?你能展示你的代码吗?首先,你的JSON是无效的。也许这会导致错误?或者你的计划是什么?有关JSON验证,请参阅。我建议使用casecodec而不是codec隐式def MyTypeCodec=casecodec3(MyType.apply,MyType.unapply)(“名称”、“类型”、“大小”)
      Parse.decodeValidation[List[MyType]](jsonString)
      
      case class MyType(name: String, type: String, size: Int)
      
      object MyType {
          implicit val createCodecJson: CodecJson[MyType] = CodecJson.casecodec3(apply, unapply)(
              "name",
              "type",
              "size"
          )
      }