2016-07-15 63 views
1

我有JSON(例如)提取領域:斯卡拉json4s我如何通過條件

{ 
    "name": "", 
    "count": 2, 
    "children": { 
    "app_open": { 
     "name": "app_open", 
     "count": 1, 
     "children": { 
     "session_end": { 
      "name": "session_end", 
      "count": 1, 
      "children": {} 
     } 
     } 
    }, 
    "app_install": { 
     "name": "app_install", 
     "count": 2, 
     "children": { 
     "session_end": { 
      "name": "session_end", 
      "count": 2, 
      "children": {} 
     } 
     } 
    }, 
    "app_instal1l": { 
     "name": "app_instal1l", 
     "count": 3, 
     "children": { 
     "app_open": { 
      "name": "app_open", 
      "count": 3, 
      "children": { 
      "session_end": { 
       "name": "session_end", 
       "count": 3, 
       "children": {} 
      } 
      } 
     } 
     } 
    } 
    } 
} 

我需要提取所有的計數,其中「名」 =「app_open」。

我試圖做到這一點與json4s庫:

val name = jsonInput filterField { 
      case JField("name", "app_open") => true 
      case _ => false 
     } 
println("name = " + URL) 

我和輸出建議我得帶徒「app_open」的東西,但我得到:

name = List((name,JString(app_open)), (name,JString(session_end)), 
(name,JString(app_open)), (name,JString(session_end))) 

我」什麼在這裏做錯了嗎? 謝謝!

回答

2

編譯器錯誤是相當清楚的:

Error: type mismatch; 
found : String("app_open") 
required: org.json4s.JsonAST.JValue 
    case JField("name", "app_open") => true 
        ^

這是因爲type JField = (String, JValue)。使用JValue代替String這樣的:

val name = jsonInput filterField { 
    case JField("name", JString("app_open")) => true 
    case _ => false 
} 
+0

注意JField是一個類型別名'''(字符串,JValue)'''而且可以在不JField –

+0

嗯匹配,我已經得到了,而編譯沒有錯誤,但您的解決方案正在運行)謝謝! – ANTVirGEO