我有以下数据结构:
val jsonStr = """
{
"data1": {
"field1": "data1",
"field2": 1.0,
"field3": true
},
"data211": {
"field1": "data211",
"field2": 4343.0,
"field3": false
},
"data344": {
"field1": "data344",
"field2": 436778.51,
"field3": true
},
"data41": {
"field1": "data41",
"field2": 14348.0,
"field3": true
}
}
"""
我要提取它。下面是我运气不佳的情况:
#1.
case class Fields(field1: String, field2: Double, field3: Boolean)
json.extract[Map[String, Map[Fields, String]]]
//org.json4s.package$MappingException: Do not know how to convert JBool(true)
//into class java.lang.String
#2.
json.extract[Map[String, Map[String, Fields]]
//java.lang.InternalError: Malformed class name
#3.
json.extract[Map[String, Map[String, Any]]]
//org.json4s.package$MappingException: No information known about type
#4.
json.extract[Map[String, Map[String, String]]]
//org.json4s.package$MappingException: Do not know
//how to convert JBool(true) into class java.lang.String
我该怎么做呢?
注:——实际上,这是https://github.com/json4s/json4s,但这并不重要,因为lift
有相同的API关于json提取。
UPDATE:它可能需要使用transform方法。我将如何使用它?
val json = parse(jsonStr) transform {
case //.... what should be here to catch JBool -- "field3"?
}
更新2 :
#5
json.extract[Map[String, Map[String, JValue]]]
// Works! but it's not what I'm looking for, I need to use a pure Java/Scala type
scala> val jsonStr = """
| {
| "data1": {
| "field1": "data1",
| "field2": 1.0,
| "field3": true
| },
| "data211": {
| "field1": "data211",
| "field2": 4343.0,
| "field3": false
| },
| "data344": {
| "field1": "data344",
| "field2": 436778.51,
| "field3": true
| },
| "data41": {
| "field1": "data41",
| "field2": 14348.0,
| "field3": true
| }
| }
| """
jsonStr: java.lang.String =
"
{
"data1": {
"field1": "data1",
"field2": 1.0,
"field3": true
},
"data211": {
"field1": "data211",
"field2": 4343.0,
"field3": false
},
"data344": {
"field1": "data344",
"field2": 436778.51,
"field3": true
},
"data41": {
"field1": "data41",
"field2": 14348.0,
"field3": true
}
}
"
scala> import net.liftweb.json._
import net.liftweb.json._
scala> implicit val formats = DefaultFormats
formats: net.liftweb.json.DefaultFormats.type = net.liftweb.json.DefaultFormats$@361ee3df
scala> val json = parse(jsonStr)
json: net.liftweb.json.package.JValue = JObject(List(JField(data1,JObject(List(JField(field1,JString(data1)), JField(field2,JDouble(1.0)), JField(field3,JBool(true))))), JField(data211,JObject(List(JField(field1,JString(data211)), JField(field2,JDouble(4343.0)), JField(field3,JBool(false))))), JField(data344,JObject(List(JField(field1,JString(data344)), JField(field2,JDouble(436778.51)), JField(field3,JBool(true))))), JField(data41,JObject(List(JField(field1,JString(data41)), JField(field2,JDouble(14348.0)), JField(field3,JBool(true)))))))
scala> case class Fields(field1: String, field2: Double, field3: Boolean)
defined class Fields
scala> json.extract[Map[String, Fields]]
res1: Map[String,Fields] = Map(data1 -> Fields(data1,1.0,true), data211 -> Fields(data211,4343.0,false), data344 -> Fields(data344,436778.51,true), data41 -> Fields(data41,14348.0,true))