![](/img/trans.png)
[英]lift json :net.liftweb.json.MappingException: unknown error
[英]Scala Json Lift - MappingException error for one record
我在解析 json 時遇到問題。 僅當存在一組記錄時才會出現此問題,無論所有列都有值還是一列或多列為空值。 其他情況下它工作正常。
我的輸入案例類如下:
case class Source(AccountNo: String, Name: String)
case class HitsIndex(_index: String, _type: String, _id: String, _score: Double, _source: Source)
case class HitsCount(total: Int, max_score: Double, hits: List[HitsIndex])
case class Shards(total: Int, successful: Int, failed: Int, hits: HitsCount)
case class ParseJson(took: Int, timed_out: Boolean, _shards: Shards)
導入語句:
import scala.collection.mutable._
import net.liftweb.json._
import net.liftweb.json.DefaultFormats
import net.liftweb.json.Serialization.write
下面是我測試的代碼:
case class Uiresult(AccountNo: String, Name: String)
val json = parse(jsonString)
val elements = (json \\ "_source").children
for (acct <- elements) {
val m = acct.extract[Source]
val res = write(Uiresult(m.accountNo, (m.firstName.toString() + m.lastName.toString()))
println(res)
}
一條記錄的期望輸出:
注意:姓氏為空
[
{"AccountNo":"1234","Name":"Augustin "}
]
注意:所有列都有值
[
{"AccountNo":"1234","Name":"Augustin Wagstuff "}
]
但收到如下錯誤消息:
> Exception in thread "main" net.liftweb.json.MappingException: No usable value for AccountNo
Did not find value which can be converted into java.lang.String
at net.liftweb.json.Meta$.fail(Meta.scala:191)
at net.liftweb.json.Extraction$.mkValue$1(Extraction.scala:357)
at net.liftweb.json.Extraction$.net$liftweb$json$Extraction$$build$1(Extraction.scala:317)
at net.liftweb.json.Extraction$$anonfun$14.apply(Extraction.scala:253)
at net.liftweb.json.Extraction$$anonfun$14.apply(Extraction.scala:253)
at scala.collection.immutable.List.map(List.scala:273)
at net.liftweb.json.Extraction$.instantiate$1(Extraction.scala:253)
at net.liftweb.json.Extraction$.newInstance$1(Extraction.scala:286)
at net.liftweb.json.Extraction$.net$liftweb$json$Extraction$$build$1(Extraction.scala:315)
at net.liftweb.json.Extraction$.net$liftweb$json$Extraction$$extract0(Extraction.scala:366)
at net.liftweb.json.Extraction$.net$liftweb$json$Extraction$$extract0(Extraction.scala:199)
at net.liftweb.json.Extraction$.extract(Extraction.scala:43)
at net.liftweb.json.JsonAST$JValue.extract(JsonAST.scala:312)
at com.aexp.prospect.atul.sample_01.JsonRead$$anonfun$1.apply(JsonRead.scala:115)
at com.aexp.prospect.atul.sample_01.JsonRead$$anonfun$1.apply(JsonRead.scala:114)
at scala.collection.immutable.List.map(List.scala:273)
at com.aexp.prospect.atul.sample_01.JsonRead$.main(JsonRead.scala:114)
at com.aexp.prospect.atul.sample_01.JsonRead.main(JsonRead.scala)
Caused by: net.liftweb.json.MappingException: Did not find value which can be converted into java.lang.String
at net.liftweb.json.Meta$.fail(Meta.scala:191)
at net.liftweb.json.Extraction$.convert(Extraction.scala:403)
at net.liftweb.json.Extraction$.net$liftweb$json$Extraction$$build$1(Extraction.scala:314)
at net.liftweb.json.Extraction$.mkValue$1(Extraction.scala:351)
... 16 more
我的輸入 Json:(姓氏為空)
注意:所有列都有值
{
"took": 1,
"timed_out": false,
"_shards": {
"total": 3,
"successful": 3,
"failed": 0
},
"hits": {
"total": 1,
"max_score": 2.2488084,
"hits": [
{
"_index": "test_02",
"_type": "test_type",
"_id": "123456",
"_score": 2.2488084,
"_source": {
"AccountNo": "06009625297",
"firstName": "Augustin",
"lastName": "Wagstuff "
}
}
]
}
}
注意:姓氏為空
{
"took": 1,
"timed_out": false,
"_shards": {
"total": 3,
"successful": 3,
"failed": 0
},
"hits": {
"total": 1,
"max_score": 2.2488084,
"hits": [
{
"_index": "test_02",
"_type": "test_type",
"_id": "123456",
"_score": 2.2488084,
"_source": {
"AccountNo": "06009625297",
"firstName": "Augustin",
"lastName": ""
}
}
]
}
}
Json 對象打印:
示例 1:
在這里,我注意到所有的列都有價值。 我只選擇了一組有問題的記錄。
json JObject(List(JField(took,JInt(1)),JField(timed_out,JBool(false)),JField(_shards,JObject(List(JField(total,JInt(3)),JField(successful,JInt(3)) )), JField(failed,JInt(0)))), JField(hits,JObject(List(JField(total,JInt(1)), JField(max_score,JDouble(2.2488084)), JField(hits,JArray(列表(JObject(List(JField(_index,JString(test_02)), JField(_type,JString(test_type)), JField(_id,JString(123456)), JField(_score,JDouble(2.2488084)), JField(_source, JObject(List(JField(AccountNo,JString(06009625297)), JField(firstName,JString(Augustin)), JField(lastName,JString()))))))))))))))
示例:2 在這里,我注意到 lastName 是空的。 我只選擇了一組有問題的記錄。 有人可以幫助我如何處理這種情況。
json JObject(List(JField(took,JInt(1)),JField(timed_out,JBool(false)),JField(_shards,JObject(List(JField(total,JInt(3)),JField(successful,JInt(3)) )), JField(failed,JInt(0)))), JField(hits,JObject(List(JField(total,JInt(1)), JField(max_score,JDouble(2.2488084)), JField(hits,JArray(列表(JObject(List(JField(_index,JString(test_02)), JField(_type,JString(test_type)), JField(_id,JString(123456)), JField(_score,JDouble(2.2488084)), JField(_source, JObject(List(JField(AccountNo,JString(06009625297)), JField(firstName,JString(Augustin)), JField(lastName,JString()))))))))))))))
輸入中有 2 個或更多記錄集的示例:
輸入有 2 組記錄(僅提取 2 個而不是 1 個):這不會產生任何問題並產生所需的輸出。 如果您注意到第一條記錄與之前的示例相同:2 輸入但在這里它不會產生任何錯誤。
{
"took": 1,
"timed_out": false,
"_shards": {
"total": 3,
"successful": 3,
"failed": 0
},
"hits": {
"total": 2,
"max_score": 2.2488084,
"hits": [
{
"_index": "test_02",
"_type": "test_type",
"_id": "123456",
"_score": 2.2488084,
"_source": {
"AccountNo": "06009625297",
"firstName": "Augustin",
"lastName": "Wagstuff"
},
"hits": [
{
"_index": "test_02",
"_type": "test_type",
"_id": "789078",
"_score": 2.2188084,
"_source": {
"AccountNo": "06009625297",
"firstName": "Augustin",
"lastName": "Jhonson"
}
}
]
}
}
輸出:正如預期的那樣。
[
{"AccountNo":"06009625297","Name":"Augustin Wagstuff "},
{"AccountNo":"06009625297","Name":"Augustin Jhonson"},
]
代碼中的 acct 值:
當我嘗試在輸出中有一組記錄時在 acct 中顯示競爭(參考代碼中的變量)時,我只看到第一列,即使輸入中的所有其他列都具有有效值。
acct JField(AccountNo,JString(1234))
當我嘗試在輸出中有兩組記錄時在 acct 中顯示競爭(參考代碼中的變量)時,即使所有列都具有有效值,我也只看到第一列。
acct JField(_source,JObject(List(JField(AccountNo,JString(06009625297)), JField(firstName,JString(Augustin)),JField(lastName,JString(Wagstuff)))))
acct JField(_source,JObject(List(JField(AccountNo,JString(06009625297)), JField(firstName,JString(Augustin)),JField(lastName,JString(Jhonson)))))
我在迭代列表時懷疑這個問題。 請有人幫我解決。
我創建了一個代碼來處理有 1 個或零個輸入記錄。 2 個或更多記錄正在工作(感謝 Shaido)。 下面是代碼,有人可以建議,是否可以以更好的方式構造此代碼。
case class Source(AccountNo: fnameString, : String, lname: String)
case class HitsIndex(_index: String, _type: String, _id: String, _score: Double, _source: Source)
case class HitsCount(total: Int, max_score: Double, hits: List[HitsIndex])
case class Shards(total: Int, successful: Int, failed: Int)
case class ParseJson(took: Int, timed_out: Boolean, _shards: Shards, hits: HitsCount)
case class Uiresult(masterid: String, value: String)
case class ErrorMsg(searchType: String, error_message: String)
def formatResult(jsonString: String, searchType: String): String = {
implicit val formats = DefaultFormats
val json = JsonParser.parse(jsonString)
val elements = (json \\ "_source").children
if (elements.isEmpty == true) {
implicit val formats = DefaultFormats
return (List(write(ErrorMsg(searchType, "No Matching Record Found"))).mkString("[\n", ",\n", "\n]"))
}
if (json.extract[ParseJson].hits.total == 1) {
val m = (json \\ "_source").extract[Source]
val list = List(write(Uiresult(m.AccountNo.toString(), (m.fname.toString() + m.lname.toString() ))))
return (list.mkString("[\n", ",\n", "\n]"))
} else {
val elements = (json \\ "_source").children
val list = for (source <- elements) yield {
println(" source " + source)
val m = source.extract[Source]
val list = List(write(Uiresult(m.AccountNo.toString(), (m.fname.toString() + m.lname.toString() ))))
}
return (list.mkString("[\n", ",\n", "\n]"))
}
}
在Parse json 中使用了 Scala 中的 Liftweb 進行了類似的討論,但沒有可用的價值
當我使用lift-json_2.11版本為3.2.0時我也遇到了這個問題經過多次試驗最終我使用2.6.3版本解決了這個問題
<dependency>
<groupId>net.liftweb</groupId>
<artifactId>lift-json_2.11</artifactId>
<version>2.6.3</version>
</dependency>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.