【问题标题】:Parsing a simple array with Spray-json使用 Spray-json 解析简单数组
【发布时间】:2015-04-16 06:55:16
【问题描述】:

我正在尝试(但失败了)了解 spray-json 如何将 json 提要转换为对象。如果我有一个简单的键 -> 值 json 提要,那么它似乎工作正常,但我想读取的数据出现在这样的列表中:

[{
    "name": "John",
    "age": "30"
},
{
    "name": "Tom",
    "age": "25"
}]

我的代码如下所示:

package jsontest

import spray.json._
import DefaultJsonProtocol._

object JsonFun {

  case class Person(name: String, age: String)
  case class FriendList(items: List[Person])

  object FriendsProtocol extends DefaultJsonProtocol {
    implicit val personFormat = jsonFormat2(Person)
    implicit val friendListFormat = jsonFormat1(FriendList)
  }

  def main(args: Array[String]): Unit = {

    import FriendsProtocol._

    val input = scala.io.Source.fromFile("test.json")("UTF-8").mkString.parseJson

    val friendList = input.convertTo[FriendList]

    println(friendList)
  }

}    

如果我更改我的测试文件,使其只有一个人不在数组中并运行val friendList = input.convertTo[Person],那么它可以工作并且所有内容都会解析,但是一旦我尝试解析数组,它就会失败并出现错误Object expected in field 'items'

谁能指出我做错的方向?

【问题讨论】:

  • 您能发布一个您尝试解码的 JSON 示例吗?

标签: scala spray spray-json


【解决方案1】:

嗯,通常是在花费数小时试图让某些东西正常工作之后,在向 StackOverflow 发布东西后立即采取的方式,我已经设法让它发挥作用。

FriendsProtocol 的正确实现是:

object FriendsProtocol extends DefaultJsonProtocol {
  implicit val personFormat = jsonFormat2(Person)
  implicit object friendListJsonFormat extends RootJsonFormat[FriendList] {
    def read(value: JsValue) = FriendList(value.convertTo[List[Person]])
    def write(f: FriendList) = ???
  } 
}

告诉 Spray 如何读/写(在我的例子中只是读)列表对象足以让它工作。

希望对其他人有所帮助!

【讨论】:

    【解决方案2】:

    为了使 Friend 数组更易于使用,通过实现适当的 apply 和 length 方法扩展 IndexedSeq[Person]trait。这将允许直接在 FriendsArray 实例本身上使用标准 Scala 集合 API 方法,如 map、filter 和 sortBy,而无需访问它包装的底层 Array[Person] 值。

    case class Person(name: String, age: String)
    
    // this case class allows special sequence trait in FriendArray class
    // this will allow you to use .map .filter etc on FriendArray
    case class FriendArray(items: Array[Person]) extends IndexedSeq[Person] {
        def apply(index: Int) = items(index)
        def length = items.length
    }
    
    object FriendsProtocol extends DefaultJsonProtocol {
      implicit val personFormat = jsonFormat2(Person)
      implicit object friendListJsonFormat extends RootJsonFormat[FriendArray] {
        def read(value: JsValue) = FriendArray(value.convertTo[Array[Person]])
        def write(f: FriendArray) = ???
      } 
    }
    
    import FriendsProtocol._
    
    val input = jsonString.parseJson
    val friends = input.convertTo[FriendArray]
    friends.map(x => println(x.name))
    println(friends.length)
    

    这将打印出来:

    John
    Tom
    2
    

    【讨论】:

      猜你喜欢
      • 2013-12-29
      • 1970-01-01
      • 2015-10-25
      • 2015-09-26
      • 1970-01-01
      • 2018-03-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多