我在解析 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(success,JInt(3)), JField(failed,JInt(0)))), JField(hits,JObject(List(JField(total,JInt(1)), JField(max_score,JDouble(2.2488084)), JField(hits,JArray(List(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 在这里,我注意到这里的姓氏是空的。我只选择了一组记录,这是一个问题。有人可以帮我如何处理这个案子吗?
json JObject(List(JField(took,JInt(1)),JField(timed_out,JBool(false)),JField(_shards,JObject(List(JField(total,JInt(3)),JField(success,JInt(3)), JField(failed,JInt(0)))), JField(hits,JObject(List(JField(total,JInt(1)), JField(max_score,JDouble(2.2488084)), JField(hits,JArray(List(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 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>