Scala выдает ошибку при извлечении json значений с помощью лифта web - PullRequest
1 голос
/ 21 января 2020

Я пытаюсь извлечь ответ 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
    }
}

Мой case classes ответ на этот json ответ:

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]

Пожалуйста, помогите.

1 Ответ

0 голосов
/ 23 января 2020

Я бы сказал, что у вас там несоответствие. Как говорится в сообщении об ошибке, вы отправляете целое число "httpCode": 200, но в случае класса вы ожидаете String httpCode:String. Просто измените его на httpCode:Int, и вы хороши на go.

...