scala解析JSON列

1374 ワード

import scala.util.parsing.json.JSON;
import scala.io.Source;
import java.io.PrintWriter;
import java.io.File;

object GraphXTest1 {
  def main(args:Array[String]): Unit ={
    val str2: String = "{\"people_url\": \"https://www.douban.com/people/109023597/\", \"people_name\": \"Ssuwm\", \"group_url\": \"https://www.douban.com/group/515813/\", \"group_name\": \"\\u9752\\u65c5\\u5ba2\\u6808\\u6c42\\u804c\\u62db\\u8058\\u3001\\u4e49\\u5de5\\u3001\\u6253\\u5de5\\u65c5\\u884c\", \"group_tags\": [\"\\u9752\\u65c5\\u6253\\u5de5\\u6362\\u5bbf\", \"\\u9752\\u65c5\\u6c42\\u804c\\u62db\\u8058\", \"\\u4e49\\u5de5\\u65c5\\u884c\", \"\\u9752\\u5e74\\u65c5\\u820d\\u4e49\\u5de5\", \"\\u65c5\\u884c\"], \"time\": \"2016-05-11 09:06:36\"}";
    val b:Option[Any] = JSON.parseFull(str2);
    val c = b match{
      case Some(map:Map[String,String])=>map;
      case None => println("parse failed");None;
      case other => println("unknown data structure");None;
    };

    println(c);
    val d = c.asInstanceOf[Map[String,String]];
    println(d.get("people_name").get);
    println(d.get("group_name").get);
    println(d.get("time").get);
  }
}