[英]value keys is not a member of play.api.libs.json.JsValue
I'm try to get the head from the keys of JsValue type in Scala. 我试图从Scala的JsValue类型的键中弄清楚。 I googled a lot to know how to get the head key from JsValue type. 我在Google上搜索了很多,以了解如何从JsValue类型获取头键。
Finally, I found that result.keys.head is the way to get the head key, but it throws error value keys is not a member of play.api.libs.json.JsValue . 最后,我发现result.keys.head是获取head键的方法,但是它抛出错误 值键不是play.api.libs.json.JsValue的成员 。
And my result variable has the below form of data: 我的结果变量具有以下数据形式:
{
"intents": [{
"intent": "feeling",
"confidence": 0.1018563217175903
}],
"entities": [],
"input": {
"text": "{reset-encounter}"
},
"output": "Good"
}
Code: 码:
import play.api.libs.json._
val jsonStr = """
{
"intents": [{
"intent": "feeling",
"confidence": 0.1018563217175903
}],
"entities": [],
"input": {
"text": "{reset-encounter}"
},
"output": "Good"
}
"""
val result = Json.parse(jsonStr)
println("key: ", result.keys.head)
At result.keys.head line, throws error. 在result.keys.head行上,引发错误。
I'm not sure but I think, may be I'm doing something wrong here. 我不确定,但我认为可能是我在这里做错了。
What are you trying to get? 你想得到什么? That's not the way to deal with play.api.Json
objects. 那不是处理play.api.Json
对象的方法。
.keys would result in a Map
, not in a JsValue. .keys将导致Map
而不是JsValue。
Check the documentation: https://www.playframework.com/documentation/2.5.x/ScalaJson 检查文档: https : //www.playframework.com/documentation/2.5.x/ScalaJson
If you want to access a specific key ( https://www.playframework.com/documentation/2.5.x/ScalaJson#Traversing-a-JsValue-structure ) you should try: 如果要访问特定的密钥( https://www.playframework.com/documentation/2.5.x/ScalaJson#Traversing-a-JsValue-structure ),则应尝试:
result \ "keyName"
or for a recursive search: 或进行递归搜索:
result \\ "keyName"
Json.parse
produces a JsValue
, which could represent any type of json object (boolean, number, array, etc). Json.parse
产生一个JsValue
,它可以表示任何类型的json对象(布尔值,数字,数组等)。 If you know you're working with an object, you can use .as[JsObject]
: 如果知道您正在使用对象,则可以使用.as[JsObject]
:
import play.api.libs.json._
val result = Json.parse(jsonStr).as[JsObject]
println("key: " + result.keys.head)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.