繁体   English   中英

将 Json(改造响应)存储到房间数据库中

[英]Store Json (Retrofit response) in to Room Database

我是 Room 数据库的新手,我有来自 MSGraph API 的 JSON 响应,这是日历事件列表。

我想将此响应存储在 Room 数据库中。

来自 retrofit 数据的示例响应

{
   "@odata.context":"",
   "value":[
      {   
    
         "@odata.etag":"W/\"arcvR4W/==\"",
         "id":"AAvR4W-QUGEzDhwKmVNegAAFPBq3AAAEA==",
         "createdDateTime":"2020-05-28T11:15:19.3397025Z",
         "lastModifiedDateTime":"2020-09-08T15:57:16.2356808Z",
         "changeKey":"arcvR4W/==",
         "categories":[
            
         ],
         "transactionId":null,
         "originalStartTimeZone":"UTC",
         "originalEndTimeZone":"UTC",
         "iCalUId":"040000008200E00074C5B7101A82E831",
         "reminderMinutesBeforeStart":15,
         "isReminderOn":true,
         "hasAttachments":false,
         "subject":"Canceled:  discussion",
         "bodyPreview":"time didnt set correctly, so cancelling the call",
         "importance":"high",
         "sensitivity":"normal",
         "isAllDay":true,
         "isCancelled":true,
         "isOrganizer":false,
         "responseRequested":true,
         "seriesMasterId":"AAMkADU3MAAAAENAABqty9Hhb9BQYTMOHAqZU16AAAU8GrcAAA=",
         "showAs":"free",
         "type":"occurrence",
         "webLink":"https://outlook.office365.com/",
         "onlineMeetingUrl":null,
         "isOnlineMeeting":true,
         "onlineMeetingProvider":"teams",
         "allowNewTimeProposals":true,
         "isDraft":false,
         "hideAttendees":false,
         "responseStatus":{
            "response":"accepted",
            "time":"2020-05-28T11:15:00Z"
         },
         "body":{
            "contentType":"html",
            "content":"<html>\r\n<head>\r\n<meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\">\r\n<meta content=\"text/html; charset=us-ascii\">\r\n</head>\r\n<body>\r\n<div>time didnt set correctly, so cancelling the call</div>\r\n</body>\r\n</html>\r\n"
         },
         "start":{
            "dateTime":"2021-03-11T00:00:00.0000000",
            "timeZone":"UTC"
         },
         "end":{
            "dateTime":"2021-03-12T00:00:00.0000000",
            "timeZone":"UTC"
         },
         "location":{
            "displayName":"",
            "locationType":"default",
            "uniqueIdType":"unknown",
            "address":{
               
            },
            "coordinates":{
               
            }
         },
         "locations":[
            
         ],
         "recurrence":null,
         "attendees":[
            {
               "type":"required",
               "status":{
                  "response":"none",
                  "time":"0001-01-01T00:00:00Z"
               },
               "emailAddress":{
                  "name":"abc",
                  "address":"abc@mail.com"
               }
            },
            {
               "type":"required",
               "status":{
                  "response":"none",
                  "time":"0001-01-01T00:00:00Z"
               },
               "emailAddress":{
                  "name":"xyz",
                  "address":"xyz@mail.com"
               }
            }
         ],
         "organizer":{
            "emailAddress":{
               "name":"abc",
               "address":"abc@mail.com"
            }
         },
         "onlineMeeting":{
            "joinUrl":""
         }
      },
      
      more elements from list
      
      ]
      }

我搜索了论坛,但没有得到任何正确的解决方案。 我想将此响应存储在 Room 数据库中。 有人可以帮助我遵循正确的方法。

谢谢

您应该创建“CalendarEvent”object 并做出 retrofit 的响应就像这样调用之后,您需要从模型创建实体并创建 dao(包含插入、获取、更新和删除数据库中的数据的方法)和存储库中的何时you receive the data map it to your db entity with your mapper (you can use extension to map your own data) I convert that json to kotlin data classes and you can create db entites inspired from those models


data class Address()

data class Attendees(val type: String?, val status: Status?, val emailAddress: EmailAddress?)

data class CalendarEvent(val id: String?, val createdDateTime: String?, val lastModifiedDateTime: String?, val changeKey: String?, val categories: List<Any>?, val transactionId: Any?, val originalStartTimeZone: String?, val originalEndTimeZone: String?, val iCalUId: String?, val reminderMinutesBeforeStart: Number?, val isReminderOn: Boolean?, val hasAttachments: Boolean?, val subject: String?, val bodyPreview: String?, val importance: String?, val sensitivity: String?, val isAllDay: Boolean?, val isCancelled: Boolean?, val isOrganizer: Boolean?, val responseRequested: Boolean?, val seriesMasterId: String?, val showAs: String?, val type: String?, val webLink: String?, val onlineMeetingUrl: Any?, val isOnlineMeeting: Boolean?, val onlineMeetingProvider: String?, val allowNewTimeProposals: Boolean?, val isDraft: Boolean?, val hideAttendees: Boolean?, val responseStatus: ResponseStatus?, val body: Body?, val start: Start?, val end: End?, val location: Location?, val locations: List<Any>?, val recurrence: Any?, val attendees: List<Attendees>?, val organizer: Organizer?, val onlineMeeting: OnlineMeeting?)

data class Body(val contentType: String?, val content: String?)

data class Coordinates()

data class EmailAddress(val name: String?, val address: String?)

data class End(val dateTime: String?, val timeZone: String?)

data class Location(val displayName: String?, val locationType: String?, val uniqueIdType: String?, val address: Address?, val coordinates: Coordinates?)

data class OnlineMeeting(val joinUrl: String?)

data class Organizer(val emailAddress: EmailAddress?)

data class ResponseStatus(val response: String?, val time: String?)

data class Start(val dateTime: String?, val timeZone: String?)

data class Status(val response: String?, val time: String?)



暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM