Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/json/15.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
Json 如何从Scala:Right(Vector(“abc”)中获取实际值_Json_Scala_Vector_Gatling_Jsonpath - Fatal编程技术网

Json 如何从Scala:Right(Vector(“abc”)中获取实际值

Json 如何从Scala:Right(Vector(“abc”)中获取实际值,json,scala,vector,gatling,jsonpath,Json,Scala,Vector,Gatling,Jsonpath,我使用Gatling来测试基于WebSocket的服务,为了解析json响应,我使用以下方法: val initiator = JsonPath.query("$.header.initiator", json).right.map(_.toVector) 打印发起人告诉我这是: Right(Vector(guest3075085133639688955@example.com)) 现在是初学者Scala的问题: 如何获取实际字符串值“guest3075085133639688955@exa

我使用Gatling来测试基于WebSocket的服务,为了解析json响应,我使用以下方法:

val initiator = JsonPath.query("$.header.initiator", json).right.map(_.toVector)
打印发起人告诉我这是:

Right(Vector(guest3075085133639688955@example.com))
现在是初学者Scala的问题:

如何获取实际字符串值“guest3075085133639688955@example.com“

我知道右边只是一个容器,它包含一个向量和一个值(我想要的值),但是我怎么才能得到它呢?!:)

我试过这个,但它只打印相同的东西(对(Vect…):

干杯, 尼古拉斯
  • 您可以使用
    right
    获取
    RightProjection
    get
    获取右侧
    中的值
  • 您可以使用
    head
    向量
    中获取第一个(仅适用于您的情况)元素
  • 这看起来像:

    val initiator: Either[String, Vector[String]] = 
      Right(Vector("guest3075085133639688955@example.com"))
    
    initiator.right.get.head
    // String = guest3075085133639688955@example.com
    
    您还可以进行模式匹配:

    val Right(Vector(email)) = initiator
    // email: String = guest3075085133639688955@example.com
    
    请注意,只有当
    initiatior
    向量
    内部至少有一个元素时,这两种方法才起作用。您的问题并不清楚如何处理
    和/或空的
    向量


    如果要以另一种方式处理所有失败案例,可以使用:

    initiator match {
      case Right(Vector(email)) => email // ok
      case _ => // default -> fail
    }
    
    • 您可以使用
      right
      获取
      RightProjection
      get
      获取右侧
      中的值
    • 您可以使用
      head
      向量
      中获取第一个(仅适用于您的情况)元素
    这看起来像:

    val initiator: Either[String, Vector[String]] = 
      Right(Vector("guest3075085133639688955@example.com"))
    
    initiator.right.get.head
    // String = guest3075085133639688955@example.com
    
    您还可以进行模式匹配:

    val Right(Vector(email)) = initiator
    // email: String = guest3075085133639688955@example.com
    
    请注意,只有当
    initiatior
    向量
    内部至少有一个元素时,这两种方法才起作用。您的问题并不清楚如何处理
    和/或空的
    向量


    如果要以另一种方式处理所有失败案例,可以使用:

    initiator match {
      case Right(Vector(email)) => email // ok
      case _ => // default -> fail
    }
    

    initiator.right.get
    来自其中一个是不安全的。因为如果它是左值,它会抛出一个
    java.util.NoSuchElementException

    通过模式匹配:

    initiator match {
      // when it is a right value, which is a vector with single element
      case Right(Vector(s)) => println(s)
      // when it is a right value, which is a vector with empty or two or more elements.
      case Right(v) => ???
      // when it is a left value
      case Left(l) => ???
    }
    

    initiator.right.get
    来自其中一个是不安全的。因为如果它是左值,它会抛出一个
    java.util.NoSuchElementException

    通过模式匹配:

    initiator match {
      // when it is a right value, which is a vector with single element
      case Right(Vector(s)) => println(s)
      // when it is a right value, which is a vector with empty or two or more elements.
      case Right(v) => ???
      // when it is a left value
      case Left(l) => ???
    }
    

    在注释中,您阐明了这是针对测试用例场景的。如果您使用的是ScalaTest matchers,您可以执行以下操作:

    initiator should matchPattern{case Right(Vector("expected value")) => }
    

    在注释中,您阐明了这是针对测试用例场景的。如果您使用的是ScalaTest matchers,您可以执行以下操作:

    initiator should matchPattern{case Right(Vector("expected value")) => }
    

    这是一个测试用例场景,意味着这里的一个异常将使测试失败,这是可以的。如果有一些匹配的“默认值”,我可能会使用它使测试失败得更漂亮。这是一个测试用例场景,意味着这里的一个异常将使测试失败,这是可以的。如果有一些匹配的“默认值”,我可能会告诉我们我认为左向量和空向量会使我的测试用例失败,这实际上还可以。除了右向量以外的任何东西(向量))我想会失败得更漂亮一些。我认为左向量和空向量会使我的测试用例失败,这实际上还可以。但是,除了正确的(向量)之外的任何东西都可能失败得更漂亮一点。