所以这是两个问题。让我们从第二个开始。
为什么"""json"""中传递的字符串是这样的?
Scala 允许多行字符串文字(或包含换行符、引号等的字符串)的特殊语法。所以你可以做
val s = """Welcome home!
How are you today?"""
现在回到主要问题
如何在 postData 字段中传递 lat、long 和 radius 作为参数?
我想你是在这种情况下:
val lat = "39.6270025"
val long = "-90.1994042"
你想把它传递给postData
函数,与其他一些可能固定的字符串混合。
Scala 提供的另一个特性是所谓的string interpolation
. 简单的例子
val name = "Mark" // output on the REPL would be: name: String = Mark
val greeting = s"Hello $name!" // output on the REPL would be: greeting: String = Hello Mark!
所以在你的情况下你可以做同样的事情
val result = Http("http:xxxx/xxx/xxxxx")
.postData(s"""{"latitude":$lat,"longitude":$long,"radius":"0"}""")
.asString