# KVData
### KVData
#### 屬性
##### string key
數據的 key
##### string value
數據的 value
#### 將排行榜顯示在小游戲中心
若開發者希望把游戲的排行榜顯示于小游戲中心,則需要把排行榜數據存儲到對應的key/value中,一個排行榜數據對應一個key,多個排行榜則多個key。同時在mp.weixin.qq.com的小游戲管理后臺“設置-游戲-排行榜設置”下配置對應的key以及相關排行榜屬性。且value的內容必須是JSON Object格式序列化的字符串,該JSON Object頂層必須包含 `wxgame` 字段,定義如下:
屬性名類型必填說明scoreInt32是該榜單對應分數值update\_timeInt64是該分數最后更新時間,Unix時間戳注意: `wxgame`下開發者不可自定義其他字段, `wxgame`同級開發者可自由定義,比如定義一個`detail` 字段,用于存儲取得該分數的中間狀態。
### 舉例
比如某小游戲有一個分數排行榜,分數排行榜需要記錄分數以及獲得分數的耗時(游戲內的排行榜需要展示耗時),可以在`wxgame`同級別定義一個`cost_ms`字段,存儲耗時的毫秒數。分配一個不和已定義的托管數據的key相沖突的key作為分數排行榜的key,如 "score"。
在玩家耗時36500ms后,獲得本周最高分16分,則需要更新分數,假設當前時間戳為1513080573, 則完整 value在序列化之前的內容如下:
```
<pre class="prettyprint"><ol class="linenums"><li class="l"><code class="pcalibre9 pcalibre8 pcalibre7"><span class="pun">{</span></code></li>
<li class="l1"><code class="pcalibre9 pcalibre8 pcalibre7"><span class="pln"> </span><span class="str">"wxgame"</span><span class="pun">:</span><span class="pln"> </span><span class="pun">{</span></code></li>
<li class="l"><code class="pcalibre9 pcalibre8 pcalibre7"><span class="pln"> </span><span class="str">"score"</span><span class="pun">:</span><span class="lit">16</span><span class="pun">,</span></code></li>
<li class="l1"><code class="pcalibre9 pcalibre8 pcalibre7"><span class="pln"> </span><span class="str">"update_time"</span><span class="pun">:</span><span class="pln"> </span><span class="lit">1513080573</span></code></li>
<li class="l"><code class="pcalibre9 pcalibre8 pcalibre7"><span class="pln"> </span><span class="pun">},</span></code></li>
<li class="l1"><code class="pcalibre9 pcalibre8 pcalibre7"><span class="pln"> </span><span class="str">"cost_ms"</span><span class="pun">:</span><span class="lit">36500</span></code></li>
<li class="l"><code class="pcalibre9 pcalibre8 pcalibre7"><span class="pun">}</span></code></li>
</ol>
```
最終序列化為string后,value為"{"wxgame":{"score":16,"update\_time": 1513080573},"cost\_ms":36500}"。
> 原文: <https://developers.weixin.qq.com/minigame/dev/api/open-api/data/KVData.html>
- 致謝
- 渲染
- 畫布
- wx.createCanvas
- Canvas
- .toTempFilePath
- .toTempFilePathSync
- .getContext
- .toDataURL
- RenderingContext
- 圖片
- wx.createImage
- Image
- 字體
- wx.getTextLineHeight
- wx.loadFont
- 幀率
- wx.setPreferredFramesPerSecond
- cancelAnimationFrame
- requestAnimationFrame
- 分包加載
- wx.loadSubpackage
- LoadSubpackageTask
- .onProgressUpdate
- 設備
- 設備方向
- wx.startDeviceMotionListening
- wx.stopDeviceMotionListening
- wx.onDeviceMotionChange
- 加速計
- wx.startAccelerometer
- wx.stopAccelerometer
- wx.onAccelerometerChange
- 剪貼板
- wx.getClipboardData
- wx.setClipboardData
- 羅盤
- wx.startCompass
- wx.stopCompass
- wx.onCompassChange
- 陀螺儀
- wx.startGyroscope
- wx.stopGyroscope
- wx.onGyroscopeChange
- 電量
- wx.getBatteryInfo
- wx.getBatteryInfoSync
- 網絡
- wx.getNetworkType
- wx.onNetworkStatusChange
- 振動
- wx.vibrateShort
- wx.vibrateLong
- 性能
- wx.onMemoryWarning
- 屏幕
- wx.getScreenBrightness
- wx.setKeepScreenOn
- wx.setScreenBrightness
- 轉屏
- wx.onDeviceOrientationChange
- wx.offDeviceOrientationChange
- 系統
- 系統信息
- wx.getSystemInfo
- wx.getSystemInfoSync
- 系統事件
- wx.onAudioInterruptionBegin
- wx.offAudioInterruptionBegin
- wx.onAudioInterruptionEnd
- wx.offAudioInterruptionEnd
- wx.onError
- wx.offError
- 生命周期
- wx.exitMiniProgram
- wx.getLaunchOptionsSync
- wx.onHide
- wx.offHide
- wx.onShow
- wx.offShow
- 觸摸事件
- wx.onTouchStart
- wx.offTouchStart
- wx.onTouchMove
- wx.offTouchMove
- wx.onTouchEnd
- wx.offTouchEnd
- wx.onTouchCancel
- wx.offTouchCancel
- Touch
- 位置
- wx.getLocation
- 媒體
- 圖片
- wx.chooseImage
- wx.previewImage
- wx.saveImageToPhotosAlbum
- 音頻
- wx.createInnerAudioContext
- wx.getAvailableAudioSources
- wx.setInnerAudioOption
- InnerAudioContext
- .pause
- .stop
- .seek
- .destroy
- .offCanplay
- .onPlay
- .offPlay
- .onPause
- .offPause
- .onStop
- .offStop
- .onEnded
- .offEnded
- .onTimeUpdate
- .offTimeUpdate
- .onError
- .onCanplay
- .offError
- .onWaiting
- .offWaiting
- .onSeeking
- .offSeeking
- .onSeeked
- .offSeeked
- .play
- 錄音
- wx.getRecorderManager
- RecorderManager
- .start
- .pause
- .resume
- .stop
- .onStart
- .onResume
- .onPause
- .onStop
- .onFrameRecorded
- .onError
- .onInterruptionBegin
- .onInterruptionEnd
- 視頻
- wx.createVideo
- Video
- .onWaiting
- .offWaiting
- .onPlay
- .offPlay
- .onPause
- .offPause
- .onEnded
- .offEnded
- .onTimeUpdate
- .offTimeUpdate
- .onError
- .offError
- .destroy
- .play
- .pause
- .stop
- .seek
- .requestFullScreen
- .exitFullScreen
- 文件
- wx.getFileSystemManager
- FileSystemManager
- .access
- .accessSync
- .appendFile
- .appendFileSync
- .saveFile
- .saveFileSync
- .getSavedFileList
- .removeSavedFile
- .copyFile
- .copyFileSync
- .getFileInfo
- .mkdir
- .mkdirSync
- .readFile
- .readFileSync
- .readdir
- .readdirSync
- .rename
- .renameSync
- .rmdir
- .rmdirSync
- .stat
- .statSync
- .unlink
- .unlinkSync
- .unzip
- .writeFile
- .writeFileSync
- Stats
- .isDirectory
- .isFile
- 網絡
- 下載
- wx.downloadFile
- DownloadTask
- .onProgressUpdate
- .abort
- 發起請求
- wx.request
- RequestTask
- .abort
- WebSocket
- wx.connectSocket
- wx.closeSocket
- wx.sendSocketMessage
- wx.onSocketOpen
- wx.onSocketClose
- wx.onSocketMessage
- wx.onSocketError
- SocketTask
- .close
- .onOpen
- .onClose
- .onError
- .onMessage
- .send
- 上傳
- wx.uploadFile
- UploadTask
- .onProgressUpdate
- .abort
- 開放接口
- 游戲圈
- wx.createGameClubButton
- GameClubButton
- .onTap
- .offTap
- .show
- .hide
- .destroy
- 開放數據
- wx.getFriendCloudStorage
- wx.getGroupCloudStorage
- wx.getUserCloudStorage
- wx.removeUserCloudStorage
- wx.setUserCloudStorage
- wx.getSharedCanvas
- UserGameData
- KVData
- removeUserStorage
- setUserStorage
- 授權
- wx.authorize
- 防沉迷
- wx.checkIsUserAdvisedToRest
- 用戶信息
- wx.getUserInfo
- wx.createUserInfoButton
- UserInfo
- UserInfoButton
- .show
- .hide
- .destroy
- .onTap
- .offTap
- 微信運動
- wx.getWeRunData
- 設置
- wx.getSetting
- wx.openSetting
- wx.createOpenSettingButton
- OpenSettingButton
- .onTap
- .offTap
- .show
- .hide
- .destroy
- AuthSetting
- 登錄
- wx.login
- wx.checkSession
- checkSessionKey
- code2Session
- 意見反饋
- wx.createFeedbackButton
- FeedbackButton
- .onTap
- .offTap
- .show
- .hide
- .destroy
- 小程序跳轉
- wx.navigateToMiniProgram
- 客服消息
- wx.openCustomerServiceConversation
- 開放數據域
- wx.getOpenDataContext
- wx.onMessage
- OpenDataContext
- .postMessage
- 接口調用憑證
- getAccessToken
- 內容安全
- imgSecCheck
- msgSecCheck
- 二維碼
- createWXAQRCode
- getWXACode
- getWXACodeUnlimit
- 調試
- wx.setEnableDebug
- console
- .groupEnd
- .debug
- .log
- .info
- .warn
- .error
- .group
- 數據緩存
- wx.getStorage
- wx.getStorageSync
- wx.setStorage
- wx.setStorageSync
- wx.removeStorage
- wx.removeStorageSync
- wx.clearStorage
- wx.clearStorageSync
- wx.getStorageInfo
- wx.getStorageInfoSync
- 更新
- wx.getUpdateManager
- UpdateManager
- .onCheckForUpdate
- .onUpdateReady
- .onUpdateFailed
- .applyUpdate
- Worker
- wx.createWorker
- Worker
- .postMessage
- .onMessage
- .terminate
- 轉發
- wx.getShareInfo
- wx.hideShareMenu
- wx.showShareMenu
- wx.updateShareMenu
- wx.shareAppMessage
- wx.onShareAppMessage
- wx.offShareAppMessage
- 廣告
- wx.createRewardedVideoAd
- wx.createBannerAd
- RewardedVideoAd
- .load
- .show
- .onLoad
- .offLoad
- .onError
- .offError
- .onClose
- .offClose
- BannerAd
- .show
- .hide
- .destroy
- .onResize
- .offResize
- .onLoad
- .offLoad
- .onError
- .offError
- 界面
- 交互
- wx.showToast
- wx.showModal
- wx.hideToast
- wx.showLoading
- wx.hideLoading
- wx.showActionSheet
- 菜單
- wx.getMenuButtonBoundingClientRect
- wx.setMenuStyle
- 鍵盤
- wx.showKeyboard
- wx.hideKeyboard
- wx.updateKeyboard
- wx.onKeyboardInput
- wx.offKeyboardInput
- wx.onKeyboardConfirm
- wx.offKeyboardConfirm
- wx.onKeyboardComplete
- wx.offKeyboardComplete
- 狀態欄
- wx.setStatusBarStyle
- 窗口
- wx.onWindowResize
- wx.offWindowResize
- 性能
- wx.getPerformance
- wx.triggerGC
- Performance
- .now
- 虛擬支付
- wx.requestMidasPayment
- midasCancelPay
- midasGetBalance
- midasPay
- midasPresent
- 定時器
- setTimeout
- clearTimeout
- setInterval
- clearInterval