使用spray.json获取Json对象 [英] Get Json Object using spray.json

查看:129
本文介绍了使用spray.json获取Json对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Spray,我需要通过一种方法返回一个json对象.

I'm using spray and I need to return a json object through a method.

val route = 

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

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

但是我需要将此作为json对象.我该怎么办?

But I need get this as the json object. how can I do it?

我以此方式尝试过

 val route =

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

出现编译错误could not find implicit value for parameter marshaller: spray.httpx.marshalling.ToResponseMarshaller

推荐答案

您需要告诉Spray如何对您的案例类进行序列化.

You need to tell Spray how it should serialize your case class.

只需配置

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

jsonFormat'number'中的数字代表案例类中的成员数量.

The number in jsonFormat'number' stands for the number of members in your case class.

然后,您只需要在定义该隐式的类中导入路由即可.

Then you just need to import into your route, the class where you define this implicit.

import JsonSupport._

这篇关于使用spray.json获取Json对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆