scala 解析json字符串

scala中自带了一个

scala.util.parsing.json.JSON

然后可以通过JSON.parseFull(jsonString:String)来解析一个json字符串,如果解析成功的话则返回一个Some(map: Map[String, Any]),如果解析失败的话返回None。

所以我们可以通过模式匹配来处理解析结果:

    val str2 = "{\"et\":\"kanqiu_client_join\",\"vtm\":1435898329434,\"body\":{\"client\":\"866963024862254\",\"client_type\":\"android\",\"room\":\"NBA_HOME\",\"gid\":\"\",\"type\":\"\",\"roomid\":\"\"},\"time\":1435898329}"

    val b = JSON.parseFull(str2)
    b match {
      // Matches if jsonStr is valid JSON and represents a Map of Strings to Any
      case Some(map: Map[String, Any]) => println(map)
      case None => println("Parsing failed")
      case other => println("Unknown data structure: " + other)
    }


你可能感兴趣的:(scala 解析json字符串)