首先 - 對於愚蠢的問題感到抱歉。 我從DB一些JSON字符串,並希望與json4s解析所有的人:如何繼續編程如果發現JsonParse錯誤
val df = sqlContext.sql("SELECT * FROM analytic.test").repartition(22)
val df_base = df.map(f => {
implicit val formats = DefaultFormats
val jsonString = f(5).toString
val tempJSON = parse(jsonString)
val mainJsonArray = tempJSON \ "events"
(
f(2).toString,
makeEventArray(mainJsonArray)
)
}).cache()
都好,我得到的Json的,但有時在DB出現一些失敗的JSON時,它帶我到錯誤:
com.fasterxml.jackson.core.JsonParseException: Unexpected end-of-input: was expecting closing '"' for name
第一個問題 - 我該如何逃避與腐敗的JSON這一行,並繼續我的程序?
我試圖環繞解析與嘗試\趕上,但在這種情況下:
var tempJSON = json4s.JsonAST.JValue
try {
tempJSON = parse(f(5).toString)
} catch {
case e: Exception => println("Error on JSON parser. " + e)
}
但考慮錯誤:
Error:(51, 25) type mismatch;
found: org.json4s.JValue (which expands to) org.json4s.JsonAST.JValue
required: org.json4s.JsonAST.JValue.type tempJSON = parse(f(5).toString)
^
第二個問題 - 如何聲明tempJson吧?
或者我必須在解析之前驗證Json?怎麼樣?
謝謝!正是我在找什麼 – ANTVirGEO