使用Circe解析Json数组

时间:2017-10-15 00:20:45

标签: json scala circe

我是Circe的初学者,我想从这个JSon中检索信息

[  
   {  
      "sha":"7fd1a60b01f91b314f59955a4e4d4e80d8edf11d",
      "commit":{  
         "author":{  
            "name":"The Octocat",
            "email":"octocat@nowhere.com",
            "date":"2012-03-06T23:06:50Z"
         },
         "committer":{  
            "name":"The Octocat",
            "email":"octocat@nowhere.com",
            "date":"2012-03-06T23:06:50Z"
         },
         "message":"Merge pull request #6 from Spaceghost/patch-1\n\nNew line at end of file.",
      },
      "url":"https://api.github.com/repos/octocat/Hello-World/commits/7fd1a60b01f91b314f59955a4e4d4e80d8edf11d",
   },
   {  
      "sha":"762941318ee16e59dabbacb1b4049eec22f0d303",
      "commit":{  
         "author":{  
            "name":"Johnneylee Jack Rollins",
            "email":"johnneylee.rollins@gmail.com",
            "date":"2011-09-14T04:42:41Z"
         },
         "committer":{  
            "name":"Johnneylee Jack Rollins",
            "email":"johnneylee.rollins@gmail.com",
            "date":"2011-09-14T04:42:41Z"
         },
         "message":"New line at end of file. --Signed off by Spaceghost",
      },
      "url":"https://api.github.com/repos/octocat/Hello-World/commits/762941318ee16e59dabbacb1b4049eec22f0d303",
   },
]

我不明白此代码如何捕获有关“作者”的信息

val doc= parse(response.json.toString()).getOrElse(Json.Null)
doc.hcursor.downArray.downField("commit").right.as[Seq[String]] match {
   case Left(failure) => println("Fail")
   case Right(json) => println("Ok")
}

你有什么想法吗?

提前感谢,

1 个答案:

答案 0 :(得分:3)

你的json在某些地方包含尾随逗号。这是against规范。

error_reporting(E_ALL);
ini_set('display_errors', 1);

和你的代码一样:

  val json =
  """[
  {
    "sha":"7fd1a60b01f91b314f59955a4e4d4e80d8edf11d",
    "commit":{
      "author":{
      "name":"The Octocat",
      "email":"octocat@nowhere.com",
      "date":"2012-03-06T23:06:50Z"
    },
      "committer":{
      "name":"The Octocat",
      "email":"octocat@nowhere.com",
      "date":"2012-03-06T23:06:50Z"
    },
      "message":"Merge pull request #6 from Spaceghost/patch-1\n\nNew line at end of file."
    },
    "url":"https://api.github.com/repos/octocat/Hello-World/commits/7fd1a60b01f91b314f59955a4e4d4e80d8edf11d"
  },
  {
    "sha":"762941318ee16e59dabbacb1b4049eec22f0d303",
    "commit":{
      "author":{
      "name":"Johnneylee Jack Rollins",
      "email":"johnneylee.rollins@gmail.com",
      "date":"2011-09-14T04:42:41Z"
    },
      "committer":{
      "name":"Johnneylee Jack Rollins",
      "email":"johnneylee.rollins@gmail.com",
      "date":"2011-09-14T04:42:41Z"
    },
      "message":"New line at end of file. --Signed off by Spaceghost"
    },
    "url":"https://api.github.com/repos/octocat/Hello-World/commits/762941318ee16e59dabbacb1b4049eec22f0d303"
  }
  ]"""

  case class Author(name: String, email: String, date: String)
  case class Committer(name: String, email: String, date: String)
  case class Commit(author: Author, committer: Committer, message: String)
  case class Record(sha: String, commit: Commit, url: String)

  decode[Seq[Record]](json) match {
      case Right(records) => records.foreach(record => println(record.commit.author))
      case Left(error) => println(error)
  }

//Author(The Octocat,octocat@nowhere.com,2012-03-06T23:06:50Z)
//Author(Johnneylee Jack Rollins,johnneylee.rollins@gmail.com,2011-09-14T04:42:41Z)