簡體   English   中英

Scala 在使用lift web 提取json 值時拋出錯誤

[英]Scala throws an error when extract json values using lift web

我試圖在我的項目中提取對案例類的 json 響應,但我收到一條錯誤消息:

Error Message is : No usable value for httpCode Did not find value which can be converted into int

如果我調用 API 並提取它,就會發生這種情況,但是當我將json響應顯式設置為json解析器並提取它們時,它們運行良好。

我的json響應是:

{
    "httpCode": 200,
    "statusCode": "S_QUERY_1000",
    "description": "Operation successful",
    "data": [
        {
            "id": "5de0e1b3b2613f221986ff79",
            "serial": "36c51161f820c2bc5c5bb83ee08881bb",
            "type": "DOOR_OPEN",
            "deviceLabel": "Door Sensor 2",
            "locationLabel": "Support",
            "notificationIds": [
                {
                    "type": "FIREBASE",
                    "id": "1575018930989-6611da04-e2fa-4402-b16a-09c8ad8be2d5"
                },
                {
                    "type": "IVR",
                    "id": "48eb911aa838b54a96e6402947c48db4"
                }
            ],
            "acknowledgedBy": null,
            "acknowledgmentType": null,
            "acknowledgedDeviceLabel": null,
            "reason": null,
            "description": null,
            "processor": "alt_p_01",
            "initiatedDateTime": "2019-11-29T09:15:31.027Z",
            "acknowledgedDateTime": null,
            "sirenOnTime": null,
            "sirenOffTime": "2020-01-21T12:16:21.445+08:00",
            "updatedDateTime": "2020-01-21T12:16:21.923+08:00",
            "status": "PROCESSING"
        },
        {
            "id": "5de0dcf7b2613f221986ff78",
            "serial": "36c51161f820c2bc5c5bb83ee08881bb",
            "type": "MOTION_DETECTED",
            "deviceLabel": "Tri-Sensor 1",
            "locationLabel": "Lunch Room",
            "notificationIds": [
                {
                    "type": "FIREBASE",
                    "id": "1575017719760-53662549-d482-4759-94d7-5ec7fdb29309"
                },
                {
                    "type": "IVR",
                    "id": "a76d9424328efc7084add0cc46bcaa28"
                }
            ],
            "acknowledgedBy": null,
            "acknowledgmentType": null,
            "acknowledgedDeviceLabel": null,
            "reason": null,
            "description": null,
            "processor": "alt_p_01",
            "initiatedDateTime": "2019-11-29T08:55:19.807Z",
            "acknowledgedDateTime": null,
            "sirenOnTime": null,
            "sirenOffTime": null,
            "updatedDateTime": "2019-11-29T08:58:23.775Z",
            "status": "PROCESSING"
        },
        {
            "id": "5de0dc4bb2613f221986ff77",
            "serial": "36c51161f820c2bc5c5bb83ee08881bb",
            "type": "DOOR_OPEN",
            "deviceLabel": "Door Sensor 3",
            "locationLabel": "Home",
            "notificationIds": [
                {
                    "type": "FIREBASE",
                    "id": "1575017547742-d018d984-ed70-4fda-9458-8d84288fca00"
                },
                {
                    "type": "IVR",
                    "id": "8896fee125ed12d9cfbc3de07d3dbc6a"
                }
            ],
            "acknowledgedBy": null,
            "acknowledgmentType": null,
            "acknowledgedDeviceLabel": null,
            "reason": null,
            "description": null,
            "processor": "alt_p_01",
            "initiatedDateTime": "2019-11-29T08:52:27.803Z",
            "acknowledgedDateTime": null,
            "sirenOnTime": null,
            "sirenOffTime": null,
            "updatedDateTime": "2019-11-29T08:55:29.705Z",
            "status": "PROCESSING"
        },
        {
            "id": "5de0dc36b2613f221986ff76",
            "serial": "36c51161f820c2bc5c5bb83ee08881bb",
            "type": "DOOR_OPEN",
            "deviceLabel": "Door Sensor 2",
            "locationLabel": "Support",
            "notificationIds": [
                {
                    "type": "FIREBASE",
                    "id": "1575017526822-87d03515-21ca-45d4-b3f9-f12988a4a5b5"
                },
                {
                    "type": "IVR",
                    "id": "d10ab1e5cc802e6367989b9e29840e32"
                }
            ],
            "acknowledgedBy": null,
            "acknowledgmentType": null,
            "acknowledgedDeviceLabel": null,
            "reason": null,
            "description": null,
            "processor": "alt_p_01",
            "initiatedDateTime": "2019-11-29T08:52:06.857Z",
            "acknowledgedDateTime": null,
            "sirenOnTime": null,
            "sirenOffTime": null,
            "updatedDateTime": "2019-11-29T08:55:08.682Z",
            "status": "PROCESSING"
        },
        {
            "id": "5de0dbdab2613f221986ff75",
            "serial": "36c51161f820c2bc5c5bb83ee08881bb",
            "type": "MOTION_DETECTED",
            "deviceLabel": "Tri-Sensor 1",
            "locationLabel": "Lunch Room",
            "notificationIds": [
                {
                    "type": "FIREBASE",
                    "id": "1575017434604-19461ead-3b17-4648-bc70-d01ec8c113a5"
                },
                {
                    "type": "IVR",
                    "id": "289d0495bcc937ab30000a8c0c2253f3"
                }
            ],
            "acknowledgedBy": null,
            "acknowledgmentType": null,
            "acknowledgedDeviceLabel": null,
            "reason": null,
            "description": null,
            "processor": "alt_p_01",
            "initiatedDateTime": "2019-11-29T08:50:34.635Z",
            "acknowledgedDateTime": null,
            "sirenOnTime": null,
            "sirenOffTime": null,
            "updatedDateTime": "2019-11-29T08:53:35.641Z",
            "status": "PROCESSING"
        },
        {
            "id": "5de0db0cb2613f221986ff74",
            "serial": "36c51161f820c2bc5c5bb83ee08881bb",
            "type": "MOTION_DETECTED",
            "deviceLabel": "Tri-Sensor 5",
            "locationLabel": "Lobby",
            "notificationIds": [
                {
                    "type": "FIREBASE",
                    "id": "1575017228218-85607147-9414-40d7-b7dc-e4e7acd717db"
                },
                {
                    "type": "IVR",
                    "id": "6cef5f5fde5f3209d845390db04bc402"
                }
            ],
            "acknowledgedBy": null,
            "acknowledgmentType": null,
            "acknowledgedDeviceLabel": null,
            "reason": null,
            "description": null,
            "processor": "alt_p_01",
            "initiatedDateTime": "2019-11-29T08:47:08.284Z",
            "acknowledgedDateTime": null,
            "sirenOnTime": null,
            "sirenOffTime": null,
            "updatedDateTime": "2019-11-29T08:50:11.561Z",
            "status": "PROCESSING"
        },
        {
            "id": "5de0db09b2613f221986ff73",
            "serial": "36c51161f820c2bc5c5bb83ee08881bb",
            "type": "DOOR_OPEN",
            "deviceLabel": "Door Sensor 2",
            "locationLabel": "Support",
            "notificationIds": [
                {
                    "type": "FIREBASE",
                    "id": "1575017225719-cd828cfb-2570-4e9d-a60e-e0714a14830b"
                },
                {
                    "type": "IVR",
                    "id": "9c30bf0ea2ed111788f72fdef8b3e295"
                }
            ],
            "acknowledgedBy": null,
            "acknowledgmentType": null,
            "acknowledgedDeviceLabel": null,
            "reason": null,
            "description": null,
            "processor": "alt_p_01",
            "initiatedDateTime": "2019-11-29T08:47:05.755Z",
            "acknowledgedDateTime": null,
            "sirenOnTime": null,
            "sirenOffTime": null,
            "updatedDateTime": "2019-11-29T08:50:08.541Z",
            "status": "PROCESSING"
        },
        {
            "id": "5de0da9bb2613f221986ff72",
            "serial": "36c51161f820c2bc5c5bb83ee08881bb",
            "type": "DOOR_OPEN",
            "deviceLabel": "Door Sensor 2",
            "locationLabel": "Support",
            "notificationIds": [
                {
                    "type": "FIREBASE",
                    "id": "1575017115516-15600a8b-f387-46c7-8aa8-6c2575c0ab42"
                },
                {
                    "type": "IVR",
                    "id": "41e45e6f791bee847e73ff083fce43c5"
                }
            ],
            "acknowledgedBy": null,
            "acknowledgmentType": null,
            "acknowledgedDeviceLabel": null,
            "reason": null,
            "description": null,
            "processor": "alt_p_01",
            "initiatedDateTime": "2019-11-29T08:45:15.547Z",
            "acknowledgedDateTime": null,
            "sirenOnTime": null,
            "sirenOffTime": null,
            "updatedDateTime": "2019-11-29T08:48:17.485Z",
            "status": "PROCESSING"
        },
        {
            "id": "5de0da5ab2613f221986ff71",
            "serial": "36c51161f820c2bc5c5bb83ee08881bb",
            "type": "DOOR_OPEN",
            "deviceLabel": "Door Sensor 2",
            "locationLabel": "Support",
            "notificationIds": [
                {
                    "type": "FIREBASE",
                    "id": "1575017050570-97586921-28d7-447d-b237-53e62e2f9c2c"
                },
                {
                    "type": "IVR",
                    "id": "1aa425cb42031e9f2eb028cb8ec16aed"
                }
            ],
            "acknowledgedBy": null,
            "acknowledgmentType": null,
            "acknowledgedDeviceLabel": null,
            "reason": null,
            "description": null,
            "processor": "alt_p_01",
            "initiatedDateTime": "2019-11-29T08:44:10.655Z",
            "acknowledgedDateTime": null,
            "sirenOnTime": null,
            "sirenOffTime": null,
            "updatedDateTime": "2019-11-29T08:47:14.454Z",
            "status": "PROCESSING"
        },
        {
            "id": "5de0da4bb2613f221986ff70",
            "serial": "36c51161f820c2bc5c5bb83ee08881bb",
            "type": "DOOR_OPEN",
            "deviceLabel": "Door Sensor 2",
            "locationLabel": "Support",
            "notificationIds": [
                {
                    "type": "FIREBASE",
                    "id": "1575017035919-35b2b84f-f11d-4bd3-9ce1-0a094cb57c8c"
                },
                {
                    "type": "IVR",
                    "id": "89c98b5781bd3e8df415230fc9ce19c5"
                }
            ],
            "acknowledgedBy": null,
            "acknowledgmentType": null,
            "acknowledgedDeviceLabel": null,
            "reason": null,
            "description": null,
            "processor": "alt_p_01",
            "initiatedDateTime": "2019-11-29T08:43:55.956Z",
            "acknowledgedDateTime": null,
            "sirenOnTime": null,
            "sirenOffTime": null,
            "updatedDateTime": "2019-11-29T08:46:59.435Z",
            "status": "PROCESSING"
        }
    ],
    "meta": {
        "currentPage": 1,
        "nextPage": 2,
        "numOfPages": 244,
        "previousPage": 0
    }
}

我處理這個json響應的case classes是:

case class Alerts(httpCode: String, statusCode: String, description: String, data: List[Data])

case class Data(id: String, serial: String, `type`: String, deviceLabel: String, locationLabel: String, notificationIds: List[NotificationIds]
                , acknowledgedBy: String, acknowledgmentType: String, acknowledgedDeviceLabel: String, reason: String, description: String
                , processor: String, initiatedDateTime: String, acknowledgedDateTime: String, sirenOnTime: String, sirenOffTime: String,
                updatedDateTime: String, status: String)

case class NotificationIds(`type`: String, id: String)

我提取的方式是:

implicit val format = DefaultFormats
//this api cal returns the response I stated above
val url = ServerConfigs.thingHttpHost+ServerConfigs.thingPathForActiveAlerts+serial
val jsonResponse = scala.io.Source.fromURL(url).mkString

val resData = jsonResponse.extract[Alerts]

請協助。

我會說你有不一致之處。 正如錯誤消息告訴您的那樣,您正在發送整數"httpCode": 200 ,但在 case class 中,您需要 String httpCode:String 只需將其更改為httpCode:Int即可。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM