json - 使用 spray.json 获取 Json 对象

标签 json scala spray spray-json

我正在使用 spray,我需要通过一个方法返回一个 json 对象。

val route = 

path("all-modules") {
        get {
          respondWithMediaType(`text/html`) {
            complete( configViewer.findAllModules.toString)
          }
        }
      }

这打印 ConfigResults(S1000,Success,List(testDataTypes, mandate, sdp))

但我需要将其作为 json 对象获取。我该怎么做?

我试过这种方式

 val route =

    path("all-modules") {
      get {
        respondWithMediaType(`application/json`) {
          complete{
            configViewer.findAllModules
          }
        }
      }
    }

它给出编译错误无法找到参数编码器的隐式值:spray.httpx.marshalling.ToResponseMarshaller

最佳答案

您需要告诉 Spray 它应该如何序列化您的案例类。

只需配置类似

object JsonSupport {
    implicit val formatConfigResults = jsonFormat3(ConfigResults)
}

jsonFormat'number'中的数字代表你的案例类中的成员数。

然后你只需要导入你的路由,你定义这个隐式的类。

import JsonSupport._

关于json - 使用 spray.json 获取 Json 对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22090671/

相关文章:

python - 查找单词的频率并绘制结果

php - 通过 JSON 将分隔字符串发送到 PHP 以获取准备好的语句

scala - 通过调用 toSet 缺少参数类型错误?

scala - 找不到隐式 json 格式的证据参数的隐式值

scala - Spray.can.server.request-timeout 属性无效

javascript - 使用 Javascript 选中复选框时如何从 JSON 文件获取数据?

c# - 使用 Elasticsearch NEST C# 索引 Json 文档

scala - Spark sql 在进行数据类型转换时将坏记录转换为 Null

scala - 为特定成员类型扩展scala集合

scala - 如何在测试中运行服务器?