当前位置: 首页 > 知识库问答 >
问题:

将Json(改装响应)存储到Room数据库中

翟永春
2023-03-14

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

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

来自改型数据的示例响应

{
   "@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数据库中。谁能帮我找到正确的方法。

谢谢

共有1个答案

郭逸清
2023-03-14

您应该创建“CalendarEvent”对象并使修改响应像这个调用那样,然后您需要从您模型中创建实体,创建dao(包含从db中插入、获取、更新和删除数据的方法)并在您的存储库中,当您接收到数据时,使用映射器将其映射到您的db实体(您可以使用扩展来映射您自己的数据)。我将json转换为kotlin数据类,您可以从这些模型中创建db实体


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?)



 类似资料:
  • 问题内容: 有人可以告诉我,在以下情况下如何进行? 接收文件(MS文件,ODS,PDF) 通过Apache Tika提取公元核心元数据+通过jackrabbit-content-extractors提取内容 使用Jackrabbit将文档(内容)及其元数据存储到存储库中 ? 检索文档+元数据 我对第3点和第4点感兴趣… 详细信息:该应用程序正在以交互方式处理文档(一些分析-语言检测,单词计数等。+

  • 我是android studio的初学者,希望在room数据库中存储语言数组列表。有人能建议我怎么做吗?

  • 我希望使用枚举将值映射到数据库表行: 我想使用status=active从FE中搜索和获取状态,但只存储到数据库行字段中的符号A。 我如何将枚举键A存储到数据库中?

  • 问题内容: 我有一个具有表的应用程序,当您单击表中的项目时,它会使用其数据(FieldGroup)填充一组文本字段,然后您可以选择保存更改, 我想知道如何保存更改用户对我的postgres数据库进行的更改 。我正在为此应用程序使用vaadin和hibernate模式。到目前为止,我已经尝试做 我努力了 而且我也尝试过 最后两个给我以下错误 问题答案: 我已经弄清楚了如何对数据库进行更改,下面是一些

  • 我创建了一个TextView,它以多行显示值 ,我希望将该值保留在SQLite数据库中。这是我使用的代码: 问题是当我保存值时,整个值被插入到一个单元格中。我希望每行的值分开,然后插入到每行的单个单元格中,尽管我使用扫描器方法插入数据,但它不起作用。那有什么办法吗?

  • 我试图将一个Azure Blob存储容器挂载到一个DataBricks实例上,虽然挂载确实有效,但它似乎没有使用存储容器。 我在这里漏掉了什么?