{"id":180456,"date":"2024-11-09T10:25:17","date_gmt":"2024-11-09T02:25:17","guid":{"rendered":"https:\/\/server.hk\/cnblog\/180456\/"},"modified":"2024-11-09T10:25:18","modified_gmt":"2024-11-09T02:25:18","slug":"%e6%b7%b1%e5%85%a5%e5%89%96%e6%9e%90-android-%e6%95%b8%e6%93%9a%e5%ba%ab%e4%b9%8b-on-%e4%bd%bf%e7%94%a8%e6%96%b9%e6%b3%95-android-%e6%95%b8%e6%93%9a%e5%ba%ab-json","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/180456\/","title":{"rendered":"\u6df1\u5165\u5256\u6790 Android \u6578\u64da\u5eab\u4e4b ON \u4f7f\u7528\u65b9\u6cd5 (android \u6578\u64da\u5eab json)"},"content":{"rendered":"<h1 id=\"%e6%b7%b1%e5%85%a5%e5%89%96%e6%9e%90-android-%e6%95%b8%e6%93%9a%e5%ba%ab%e4%b9%8b-on-%e4%bd%bf%e7%94%a8%e6%96%b9%e6%b3%95-android-%e6%95%b8%e6%93%9a%e5%ba%ab-json-bKdNxTqtQd\">\u6df1\u5165\u5256\u6790 Android \u6578\u64da\u5eab\u4e4b ON \u4f7f\u7528\u65b9\u6cd5 (android \u6578\u64da\u5eab json)<\/h1>\n<p>\u5728\u73fe\u4eca\u7684\u79fb\u52d5\u61c9\u7528\u958b\u767c\u4e2d\uff0c\u6578\u64da\u5eab\u7684\u4f7f\u7528\u662f\u4e0d\u53ef\u6216\u7f3a\u7684\u4e00\u90e8\u5206\u3002Android \u5e73\u53f0\u63d0\u4f9b\u4e86\u591a\u7a2e\u6578\u64da\u5b58\u5132\u89e3\u6c7a\u65b9\u6848\uff0c\u5176\u4e2d\u4f7f\u7528 JSON \u683c\u5f0f\u7684\u6578\u64da\u5eab\u64cd\u4f5c\u8d8a\u4f86\u8d8a\u53d7\u5230\u958b\u767c\u8005\u7684\u9752\u775e\u3002\u672c\u6587\u5c07\u6df1\u5165\u63a2\u8a0e Android \u6578\u64da\u5eab\u7684\u4f7f\u7528\u65b9\u6cd5\uff0c\u7279\u5225\u662f\u5982\u4f55\u5229\u7528 JSON \u4f86\u9032\u884c\u6578\u64da\u7684\u5b58\u5132\u548c\u7ba1\u7406\u3002<\/p>\n<h2 id=\"%e4%bb%80%e9%ba%bc%e6%98%af-json%ef%bc%9f-bKdNxTqtQd\">\u4ec0\u9ebc\u662f JSON\uff1f<\/h2>\n<p>JSON\uff08JavaScript Object Notation\uff09\u662f\u4e00\u7a2e\u8f15\u91cf\u7d1a\u7684\u6578\u64da\u4ea4\u63db\u683c\u5f0f\uff0c\u6613\u65bc\u4eba\u985e\u95b1\u8b80\u548c\u7de8\u5beb\uff0c\u540c\u6642\u4e5f\u6613\u65bc\u6a5f\u5668\u89e3\u6790\u548c\u751f\u6210\u3002\u5b83\u7684\u7d50\u69cb\u985e\u4f3c\u65bc JavaScript \u7684\u5c0d\u8c61\u8868\u793a\u6cd5\uff0c\u901a\u5e38\u7528\u65bc\u5728\u5ba2\u6236\u7aef\u548c\u670d\u52d9\u5668\u4e4b\u9593\u50b3\u905e\u6578\u64da\u3002<\/p>\n<h2 id=\"android-%e4%b8%ad%e7%9a%84%e6%95%b8%e6%93%9a%e5%ba%ab%e9%81%b8%e6%93%87-bKdNxTqtQd\">Android \u4e2d\u7684\u6578\u64da\u5eab\u9078\u64c7<\/h2>\n<p>\u5728 Android \u958b\u767c\u4e2d\uff0c\u958b\u767c\u8005\u53ef\u4ee5\u9078\u64c7\u591a\u7a2e\u6578\u64da\u5eab\u89e3\u6c7a\u65b9\u6848\uff0c\u5305\u62ec SQLite\u3001Room \u548c NoSQL \u6578\u64da\u5eab\u7b49\u3002\u9019\u4e9b\u6578\u64da\u5eab\u5404\u6709\u512a\u7f3a\u9ede\uff0c\u958b\u767c\u8005\u53ef\u4ee5\u6839\u64da\u61c9\u7528\u7684\u9700\u6c42\u9078\u64c7\u5408\u9069\u7684\u6578\u64da\u5eab\u3002<\/p>\n<ul>\n<li><strong>SQLite\uff1a<\/strong>Android \u5167\u5efa\u7684\u8f15\u91cf\u7d1a\u95dc\u4fc2\u578b\u6578\u64da\u5eab\uff0c\u9069\u5408\u5c0f\u578b\u61c9\u7528\u3002<\/li>\n<li><strong>Room\uff1a<\/strong>Google \u63d0\u4f9b\u7684\u6578\u64da\u5eab\u5eab\uff0c\u57fa\u65bc SQLite\uff0c\u63d0\u4f9b\u4e86\u66f4\u9ad8\u5c64\u6b21\u7684\u62bd\u8c61\uff0c\u7c21\u5316\u4e86\u6578\u64da\u5eab\u64cd\u4f5c\u3002<\/li>\n<li><strong>NoSQL \u6578\u64da\u5eab\uff1a<\/strong>\u5982 Firebase Realtime Database\uff0c\u9069\u5408\u9700\u8981\u5be6\u6642\u6578\u64da\u66f4\u65b0\u7684\u61c9\u7528\u3002<\/li>\n<\/ul>\n<h2 id=\"%e4%bd%bf%e7%94%a8-json-%e9%80%b2%e8%a1%8c%e6%95%b8%e6%93%9a%e5%ad%98%e5%84%b2-bKdNxTqtQd\">\u4f7f\u7528 JSON \u9032\u884c\u6578\u64da\u5b58\u5132<\/h2>\n<p>\u5728 Android \u61c9\u7528\u4e2d\uff0c\u4f7f\u7528 JSON \u4f86\u5b58\u5132\u548c\u50b3\u905e\u6578\u64da\u662f\u4e00\u7a2e\u5e38\u898b\u7684\u505a\u6cd5\u3002\u4ee5\u4e0b\u662f\u4f7f\u7528 JSON \u9032\u884c\u6578\u64da\u5b58\u5132\u7684\u57fa\u672c\u6b65\u9a5f\uff1a<\/p>\n<h3 id=\"1-%e6%95%b8%e6%93%9a%e6%a8%a1%e5%9e%8b%e7%9a%84%e5%ae%9a%e7%be%a9-bKdNxTqtQd\">1. \u6578\u64da\u6a21\u578b\u7684\u5b9a\u7fa9<\/h3>\n<p>\u9996\u5148\uff0c\u9700\u8981\u5b9a\u7fa9\u6578\u64da\u6a21\u578b\u3002\u5047\u8a2d\u6211\u5011\u8981\u5b58\u5132\u7528\u6236\u4fe1\u606f\uff0c\u53ef\u4ee5\u5275\u5efa\u4e00\u500b User \u985e\uff1a<\/p>\n<pre><code>public class User {\n    private String name;\n    private int age;\n\n    public User(String name, int age) {\n        this.name = name;\n        this.age = age;\n    }\n\n    \/\/ Getter \u548c Setter \u65b9\u6cd5\n}<\/code><\/pre>\n<h3 id=\"2-%e5%b0%87%e5%b0%8d%e8%b1%a1%e8%bd%89%e6%8f%9b%e7%82%ba-json-bKdNxTqtQd\">2. \u5c07\u5c0d\u8c61\u8f49\u63db\u70ba JSON<\/h3>\n<p>\u63a5\u4e0b\u4f86\uff0c\u53ef\u4ee5\u4f7f\u7528\u7b2c\u4e09\u65b9\u5eab\u5982 Gson \u5c07 User \u5c0d\u8c61\u8f49\u63db\u70ba JSON \u5b57\u7b26\u4e32\uff1a<\/p>\n<pre><code>Gson gson = new Gson();\nUser user = new User(\"Alice\", 30);\nString jsonString = gson.toJson(user); \/\/ \u5c07\u5c0d\u8c61\u8f49\u63db\u70ba JSON \u5b57\u7b26\u4e32<\/code><\/pre>\n<h3 id=\"3-%e5%b0%87-json-%e5%ad%98%e5%84%b2%e5%88%b0%e6%95%b8%e6%93%9a%e5%ba%ab-bKdNxTqtQd\">3. \u5c07 JSON \u5b58\u5132\u5230\u6578\u64da\u5eab<\/h3>\n<p>\u7136\u5f8c\uff0c\u53ef\u4ee5\u5c07 JSON \u5b57\u7b26\u4e32\u5b58\u5132\u5230 SQLite \u6216 Room \u6578\u64da\u5eab\u4e2d\u3002\u4ee5\u4e0b\u662f\u4f7f\u7528 SQLite \u7684\u793a\u4f8b\uff1a<\/p>\n<pre><code>SQLiteDatabase db = this.getWritableDatabase();\nContentValues values = new ContentValues();\nvalues.put(\"user_data\", jsonString);\ndb.insert(\"users\", null, values); \/\/ \u5c07 JSON \u5b58\u5132\u5230\u6578\u64da\u5eab<\/code><\/pre>\n<h3 id=\"4-%e5%be%9e%e6%95%b8%e6%93%9a%e5%ba%ab%e4%b8%ad%e8%ae%80%e5%8f%96-json-bKdNxTqtQd\">4. \u5f9e\u6578\u64da\u5eab\u4e2d\u8b80\u53d6 JSON<\/h3>\n<p>\u8981\u5f9e\u6578\u64da\u5eab\u4e2d\u8b80\u53d6 JSON\uff0c\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u4ee3\u78bc\uff1a<\/p>\n<pre><code>Cursor cursor = db.query(\"users\", new String[]{\"user_data\"}, null, null, null, null, null);\nif (cursor.moveToFirst()) {\n    String jsonData = cursor.getString(0);\n    User user = gson.fromJson(jsonData, User.class); \/\/ \u5c07 JSON \u5b57\u7b26\u4e32\u8f49\u63db\u56de\u5c0d\u8c61\n}<\/code><\/pre>\n<h2 id=\"%e7%b5%90%e8%ab%96-bKdNxTqtQd\">\u7d50\u8ad6<\/h2>\n<p>\u4f7f\u7528 JSON \u9032\u884c\u6578\u64da\u5b58\u5132\u548c\u7ba1\u7406\u5728 Android \u958b\u767c\u4e2d\u63d0\u4f9b\u4e86\u9748\u6d3b\u6027\u548c\u4fbf\u5229\u6027\u3002\u958b\u767c\u8005\u53ef\u4ee5\u8f15\u9b06\u5730\u5c07\u5c0d\u8c61\u8f49\u63db\u70ba JSON \u683c\u5f0f\uff0c\u4e26\u5c07\u5176\u5b58\u5132\u5728\u6578\u64da\u5eab\u4e2d\uff0c\u96a8\u6642\u8b80\u53d6\u548c\u4f7f\u7528\u3002\u9019\u7a2e\u65b9\u6cd5\u4e0d\u50c5\u63d0\u9ad8\u4e86\u6578\u64da\u8655\u7406\u7684\u6548\u7387\uff0c\u9084\u4f7f\u5f97\u6578\u64da\u7684\u50b3\u905e\u66f4\u52a0\u7c21\u55ae\u3002<\/p>\n<p>\u5982\u679c\u60a8\u6b63\u5728\u5c0b\u627e\u9ad8\u6548\u7684\u6578\u64da\u5b58\u5132\u89e3\u6c7a\u65b9\u6848\uff0c\u8003\u616e\u4f7f\u7528 <a href=\"https:\/\/server.hk\">\u9999\u6e2f VPS<\/a> \u4f86\u642d\u5efa\u60a8\u7684\u61c9\u7528\u5f8c\u7aef\uff0c\u9019\u5c07\u70ba\u60a8\u7684\u61c9\u7528\u63d0\u4f9b\u7a69\u5b9a\u7684\u652f\u6301\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u6df1\u5165\u5256\u6790 Android \u6578\u64da\u5eab\u7684 ON \u4f7f\u7528\u65b9\u6cd5\uff0c\u63a2\u7d22\u5982\u4f55\u6709\u6548\u8655\u7406 JSON \u6578\u64da\uff0c\u63d0\u5347\u61c9\u7528\u6027\u80fd\u8207\u6578\u64da\u7ba1\u7406\u6548\u7387\u3002<\/p>\n","protected":false},"author":0,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[101],"tags":[],"class_list":["post-180456","post","type-post","status-publish","format-standard","hentry","category-database"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/180456","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/types\/post"}],"replies":[{"embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/comments?post=180456"}],"version-history":[{"count":1,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/180456\/revisions"}],"predecessor-version":[{"id":180457,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/180456\/revisions\/180457"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=180456"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=180456"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=180456"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}