{"id":360,"date":"2013-04-30T02:11:10","date_gmt":"2013-04-29T17:11:10","guid":{"rendered":"http:\/\/www.typea.info\/blog\/index.php\/2013\/04\/30\/gaemodeljavascriptjson\/"},"modified":"2013-04-30T02:11:10","modified_gmt":"2013-04-29T17:11:10","slug":"gaemodeljavascriptjson","status":"publish","type":"post","link":"https:\/\/www.typea.info\/blog\/index.php\/2013\/04\/30\/gaemodeljavascriptjson\/","title":{"rendered":"GAE\u306eModel\u3068JavaScript\u306e\u30e2\u30c7\u30eb\u3092JSON\u7d4c\u7531\u3067\u76f8\u4e92\u5909\u63db"},"content":{"rendered":"<p><a href=\"https:\/\/www.typea.info\/blog\/index.php\/2013\/04\/28\/jquerygaejson\">GAE \u3068 jQuery \u3067 JSON \u3092\u3084\u308a\u3068\u308a\u3059\u308b\u6e96\u5099\u304c\u3068\u3068\u306e\u3063\u305f<\/a>\u306e\u3067\u3001GAE(Python) \u306e db.Model \u3068\u3001JavaScript \u3067\u6271\u3046\u30e2\u30c7\u30eb\u3092\u76f8\u4e92\u306b\u5909\u63db\u3055\u305b\u305f\u3044\u3002<\/p>\n<p>\u57fa\u672c\u7dda\u3068\u3057\u3066\u307e\u305a\u306f\u3001<\/p>\n<ul>\n<li>\u306a\u308b\u3079\u304f\u5909\u63db\u30b3\u30fc\u30c9\u306f\u66f8\u304d\u305f\u304f\u306a\u3044 <\/li>\n<li>\u3042\u307e\u308a\u6c4e\u7528\u6027\u3092\u6c42\u3081\u306a\u3044(\u8907\u96d1\u306a\u69cb\u9020\u306f\u5909\u63db\u3067\u304d\u306a\u304f\u3066\u3082\u3044\u3044\u3084) <\/li>\n<\/ul>\n<p>\u3068\u3044\u3046\u3053\u3068\u3067\u691c\u8a3c\u3092\u9032\u3081\u3066\u3044\u304d\u305f\u3044\u3068\u601d\u3044\u307e\u3059\u3002<\/p>\n<p>\u3068\u3044\u3046\u306e\u306f\u3001\u753b\u9762\u5074\u3068\u30b5\u30fc\u30d0\u30fc\u5074\u3067\u3044\u3061\u3044\u3061\u5909\u63db\u3059\u308b\u30b3\u30fc\u30c9\u3092\u66f8\u3044\u3066\u3044\u308b\u6642\u9593\u306f\u306a\u3044\u306e\u3067\u3001\u67d4\u8edf\u6027\u3092\u72a0\u7272\u306b\u3057\u3066\u3082\u7c21\u5358\u306b\u30c7\u30fc\u30bf\u306e\u3084\u308a\u3068\u308a\u3092\u884c\u3044\u305f\u3044\u306a\u3041\u3068\u3044\u3046\u3053\u3068\u3067\u3059\u3002\u4ed5\u4e8b\u3067\u3084\u3063\u3066\u3044\u308b\u8a33\u3067\u306f\u306a\u3044\u306e\u3068\u3001\u7d30\u5207\u308c\u306e\u6642\u9593\u3092\u898b\u3064\u3051\u306a\u304c\u3089\u306e\u4f5c\u696d\u3067\u3082\u3042\u308b\u306e\u3067\u3002<\/p>\n<h2>JavaScript\u306e\u30e2\u30c7\u30eb<\/h2>\n<p>\u307e\u305a\u3001JavaScript \u306e\u30e2\u30c7\u30eb\u3092\u7528\u610f\u3002\u30b0\u30ed\u30fc\u30d0\u30eb\u30aa\u30d6\u30b8\u30a7\u30af\u30c8\u3001pmt \u3092\u7528\u610f\u3057\u3066\u3001\u305d\u306e\u4e2d\u306b model \u30aa\u30d6\u30b8\u30a7\u30af\u30c8\u3092\u3064\u304f\u3063\u3066\u3001\u3055\u3089\u306b\u305d\u3053\u306b\u3001Project \u30e2\u30c7\u30eb \u3092\u4f5c\u6210\u3059\u308b\u3002\u4eca\u5f8c\u3001\u30e2\u30c7\u30eb\u306e\u8ffd\u52a0\u306f\u3001model \u30aa\u30d6\u30b8\u30a7\u30af\u30c8\u306e\u5c5e\u6027\u3068\u3057\u3066\u8ffd\u52a0\u3057\u3066\u3044\u304f\u3002<\/p>\n<pre class=\"prettyprint linenums\">var pmt;\r\n(function($){\r\n    pmt.model = {\r\n            Project: function(){\r\n                this.object_key = &quot;&quot;;\r\n                this.name = &quot;&quot;;\r\n            }\r\n    };\r\n})(jQuery);<\/pre>\n<h2>JavaScript\u306e\u30e2\u30c7\u30eb\u7d4c\u7531\u3067GAE\u306e\u547c\u3073\u51fa\u3057<\/h2>\n<p>\u30dc\u30bf\u30f3\u304c\u62bc\u3055\u308c\u305f\u6642\u306b\u3001\u4e0a\u8a18\u3067\u4f5c\u6210\u3057\u305fProject \u30e2\u30c7\u30eb\u306e\u30a4\u30f3\u30b9\u30bf\u30f3\u30b9\u3092\u4f5c\u6210\u3057\u3001name \u30d7\u30ed\u30d1\u30c6\u30a3\u306b\u5024\u3092\u30bb\u30c3\u30c8\u3057\u305f\u72b6\u614b\u3067\u3001GAE\u306b\u9001\u4fe1\u3059\u308b\u3002\u3053\u306e\u3068\u304d\u3001\u30e2\u30c7\u30eb\u3092 JSON\u3068\u3057\u3066\u9001\u4fe1\u3059\u308b\u3002<\/p>\n<p>\u3067\u3001\u7d50\u679c\u304c\u8fd4\u3063\u3066\u304d\u305f\u3089(success)\u3001\u30b0\u30ed\u30fc\u30d0\u30eb\u30aa\u30d6\u30b8\u30a7\u30af\u30c8 test.json \u306b\u30bb\u30c3\u30c8\u3057\u3001\u7d50\u679c\u30e2\u30c7\u30eb\u3068\u3057\u3066\u3001\u518d\u5ea6Project\u30e2\u30c7\u30eb\u306e\u30a4\u30f3\u30b9\u30bf\u30f3\u30b9\u3092\u4f5c\u6210\u3002<\/p>\n<h3>jQuery.extend \u3067\u7d50\u679c\u3068\u3057\u3066\u8fd4\u3063\u3066\u304d\u305fJSON\u306e\u5185\u5bb9\u3092Project\u30e2\u30c7\u30eb\u306b\u30de\u30fc\u30b8<\/h3>\n<p>\u3057\u3066\u307f\u308b\u3002<a href=\"http:\/\/blog.livedoor.jp\/dankogai\/archives\/51859373.html\">JavaScript\u30aa\u30d6\u30b8\u30a7\u30af\u30c8\u306e\u8907\u88fd\u306f\u306a\u304b\u306a\u304b\u5927\u5909\u305d\u3046<\/a> \u306a\u306e\u3060\u304c\u3001JSON\u306f\u3001\u307e\u305a\u3001\u57fa\u672c\u6587\u5b57\u5217\u3068\u3044\u3046\u3053\u3068\u3068\u3067\u3001<a href=\"http:\/\/api.jquery.com\/jQuery.extend\/\">jQuery.extend<\/a>() \u3092\u4f7f\u3063\u3066\u307f\u308b\u3002\u3053\u308c\u306f\u30012\u3064\u4ee5\u4e0a\u306e\u30aa\u30d6\u30b8\u30a7\u30af\u30c8\u306e\u5185\u5bb9\u3092\u3001\u6700\u521d\u306e\u5f15\u6570\u3068\u3057\u3066\u6e21\u3055\u308c\u305f\u30aa\u30d6\u30b8\u30a7\u30af\u30c8\u306b\u30de\u30fc\u30b8\u3059\u308b\u95a2\u6570\u3002<\/p>\n<p>\u4ee5\u4e0b\u30bd\u30fc\u30b9\u3067\u3084\u308a\u305f\u3044\u3053\u3068\u306f\u3001Project\u30e2\u30c7\u30eb\u306b\u3001\u7d50\u679cJSON \u306e\u5024\u3092\u89e3\u6790\u306a\u3057\u306b\u8ee2\u8a18\u3059\u308b\u3053\u3068\u3002<\/p>\n<pre class=\"prettyprint linenums\">$(&quot;#create_project&quot;).click(function(){\r\n    var proj = new pmt.model.Project();\r\n    proj.name = &quot;new_project&quot;;\r\n    \r\n    $.ajax({\r\n        url: &quot;\/project&quot;,\r\n        type: &quot;POST&quot;,\r\n        dataType: 'json',\r\n        contentType: &quot;application\/json; charset=utf-8&quot;,\r\n        data: JSON.stringify(proj),\r\n        success: function(data){\r\n            test.json = data;\r\n            var retProj = new pmt.model.Project();        \r\n            $.extend(retProj,data);\r\n            alert(retProj.name);\r\n        }\r\n    });\r\n    \r\n});<\/pre>\n<h2>GAE\u306e\u53d7\u3051\u5074\u306e\u30e2\u30c7\u30eb\u3092\u4f5c\u6210\u3059\u308b<\/h2>\n<p>GAE\u306e\u30e2\u30c7\u30eb\u3092JSON\u5316\u3059\u308b\u306e\u306b\u3001<a href=\"https:\/\/www.typea.info\/blog\/index.php\/2013\/04\/28\/jquerygaejson\">json.dump \u3092\u4f7f\u3048\u308b<\/a>\u306e\u3060\u304c\u3001GAE\u306e\u30e2\u30c7\u30eb\u3092\u76f4\u63a5\u5909\u63db\u3059\u308b\u3053\u3068\u306f\u3067\u304d\u306a\u3044\u306e\u3067\u3001\u4e00\u65e6\u3001\u8f9e\u66f8\u30aa\u30d6\u30b8\u30a7\u30af\u30c8\u306bGAE\u306e\u30e2\u30c7\u30eb\u3092\u5909\u63db\u3059\u308b\u5fc5\u8981\u304c\u3042\u308b<a href=\"http:\/\/ja.softuses.com\/194765\">\u3002\u300cGoogle \u306e\u30a2\u30d7\u30ea\u30b1\u30fc\u30b7\u30e7\u30f3 \u30a8\u30f3\u30b8\u30f3 \u30e2\u30c7\u30eb\u306e JSON \u306e\u30b7\u30ea\u30a2\u30eb\u5316\u300d<\/a> \u3092\u53c2\u8003\u306b\u3001\u5148\u305a\u306f\u4e00\u756a\u7c21\u5358\u306a\u65b9\u6cd5\u3092\u5b9f\u88c5\u3059\u308b\u3002<\/p>\n<p>db.Model \u3068\u5b9f\u969b\u306e\u30af\u30e9\u30b9\u306e\u9593\u3067\u5171\u901a\u306b\u5229\u7528\u3059\u308b\u305f\u3081\u3001\u4e0a\u8a18\u30b5\u30a4\u30c8\u306eDictModel\u65b9\u5f0f\u3092\u3068\u308b\u3002<\/p>\n<p>\u305f\u3060\u3057\u3001Model.key() \u3092 \u30af\u30e9\u30a4\u30a2\u30f3\u30c8\u306b\u6e21\u3057\u305f\u3044\u306e\u3067\u3001\u8f9e\u66f8\u306b\u3001&quot;object_key&quot; \u3092\u8ffd\u52a0\u3057\u3066\u307f\u308b\u3002<\/p>\n<p>key() \u30e1\u30bd\u30c3\u30c9\u306f\u3001\u672a put() \u3060\u3068\u4f8b\u5916\u3092\u8fd4\u3059\u3089\u3057\u3044\u306e\u3067\u3001\u30c7\u30d5\u30a9\u30eb\u30c8\u306f\u3068\u308a\u3042\u3048\u305a\u30d6\u30e9\u30f3\u30af\u306b\u3057\u3066\u304a\u304d\u307e\u3059\u3002<\/p>\n<pre class=\"prettyprint linenums\"># -*- coding:utf-8 -*-\r\nfrom google.appengine.ext import db\r\n\r\nclass DictModel(db.Model):\r\n    ''' \r\n    http:\/\/ja.softuses.com\/194765\r\n    '''\r\n    def to_dict(self):\r\n        prps = dict([(p, unicode(getattr(self, p))) for p in self.properties()])\r\n        object_key = ''\r\n        try:\r\n            object_key = unicode(self.key())\r\n        except:\r\n            pass\r\n        prps['object_key'] = object_key\r\n        return prps<\/pre>\n<p>\u3067\u3001\u3053\u308c\u3092\u7d99\u627f\u3057\u3066\u3001\u5bfe\u5fdc\u3059\u308bProject\u30af\u30e9\u30b9\u3092\u4f5c\u6210\u3057\u3066\u307f\u308b\u3002\u3068\u3002<\/p>\n<pre class=\"prettyprint linenums\"># -*- coding:utf-8 -*-\r\nfrom google.appengine.ext import db\r\nfrom dictmodel import DictModel\r\n\r\nclass Project(DictModel):\r\n    name = db.StringProperty(required=True)<\/pre>\n<h3>\u30af\u30e9\u30a4\u30a2\u30f3\u30c8\u304b\u3089\u6e21\u3055\u308c\u305fJSON\u6587\u5b57\u5217\u3092\u30c7\u30b3\u30fc\u30c9\u3057\u3066\u3001Project\u30a4\u30f3\u30b9\u30bf\u30f3\u30b9\u3092\u4f5c\u6210\u3059\u308b<\/h3>\n<p>\u306e\u306b\u3001object_hook \u306b\u30c7\u30b3\u30fc\u30c9\u3059\u308b\u51e6\u7406\u3092\u6307\u5b9a\u3057\u307e\u3059\u3002\u4ee5\u4e0b\u306e\u4f8b\u3067\u306f\u3001\u30e9\u30e0\u30c0\u5f0f\u3092\u4f7f\u3063\u3066\u3001Project\u306e\u30b3\u30f3\u30b9\u30c8\u30e9\u30af\u30bf\u306bjson\u30e9\u30a4\u30d6\u30e9\u30ea\u306b\u3088\u308a\u3001\u89e3\u6790\u6e08\u307fJSON\u30c7\u30fc\u30bf o \u3092\u6e21\u3057\u3066\u3044\u307e\u3059\u3002<\/p>\n<pre class=\"prettyprint linenums\">class ProjectHandler(webapp2.RequestHandler):\r\n    def post(self):\r\n        json_data = self.request.body\r\n\r\n        proj = json.loads(json_data, object_hook=lambda o: Project(**o))\r\n        proj.put()\r\n        \r\n        logger.info(&quot;Project key %s&quot; % unicode(proj.key))\r\n        \r\n        self.response.content_type = 'application\/json; charset=utf-8'\r\n        self.response.out.write(json.dumps(proj.to_dict()))<\/pre>\n<p>\u4ee5\u4e0b\u306e\u69d8\u306b\u3001\u30c7\u30b3\u30fc\u30c9\uff5e\u7d50\u679c\u30a4\u30f3\u30b9\u30bf\u30f3\u30b9\u4f5c\u6210\u3092\u5916\u306b\u51fa\u3057\u3066\u3001\u304d\u3081\u7d30\u304b\u304f\u51e6\u7406\u3059\u308b\u3053\u3068\u3082\u3067\u304d\u307e\u3059\u3002<\/p>\n<p>\u547c\u3073\u51fa\u3055\u308c\u308b\u3068\u304d\u306b\u3001\u89e3\u6790\u3055\u308c\u305fJSON\u6587\u5b57\u5217\u304c\u3001\u8f9e\u66f8\u30aa\u30d6\u30b8\u30a7\u30af\u30c8\u3068\u3057\u3066\u6e21\u3055\u308c\u308b\u306e\u3067\u3001\u305d\u3053\u304b\u3089\u5024\u3092\u53d6\u308a\u51fa\u3057\u3066\u3001GAE\u306eModel\u306e\u30b3\u30f3\u30b9\u30c8\u30e9\u30af\u30bf\u306e\u540d\u524d\u4ed8\u304d\u5f15\u6570\u306b\u500b\u5225\u306b\u6e21\u3059\u3053\u3068\u304c\u3067\u304d\u307e\u3059\u3002<\/p>\n<pre class=\"prettyprint linenums\">def json_decoder(o):\r\n    return Project(name=o[&quot;name&quot;])<\/pre>\n<p>\u5225\u9014\u5b9a\u7fa9\u3057\u305f\u30c7\u30b3\u30fc\u30c9\u51e6\u7406\u3092\u3001object_hook \u306b\u6e21\u3057\u307e\u3059\u3002<\/p>\n<pre class=\"prettyprint linenums\">proj = json.loads(json_data, object_hook=json_decoder)<\/pre>\n<p>\u4e0a\u8a18\u4f8b\u3067\u306f\u3001name=o[&quot;name&quot;] \u3068\u3057\u3066\u3001\u540d\u524d\u4ed8\u304d\u5f15\u6570\u306e\u5024\u306b\u8f9e\u66f8\u304b\u3089\u5024\u3092\u53d6\u5f97\u3057\u3066\u6e21\u3057\u3066\u3044\u307e\u3059\u304c\u3001\u5143\u3005\u306e\u30e9\u30e0\u30c0\u306e\u4f8b\u306e\u3088\u3046\u306b\u3001\u8f9e\u66f8\u30aa\u30d6\u30b8\u30a7\u30af\u30c8 o \u3092 **o \u3068\u3057\u3066\u5f15\u6570\u306b\u308f\u305f\u3059\u3053\u3068\u3067\u3001<a href=\"python \u30ad\u30fc\u30ef\u30fc\u30c9\u5f15\u6570\">\u8f9e\u66f8\u306b\u542b\u307e\u308c\u3066\u3044\u308b\u5185\u5bb9\u3092\u3001\u3059\u3079\u3066\u5c55\u958b\u3057\u305f\u72b6\u614b\u3068\u3057\u3066\u5f15\u6570\u306b\u6e21\u3059\u3053\u3068\u304c\u3067\u304d\u307e\u3059<\/a>\u3002\u3064\u307e\u308a\u3001o \u306b \u201cname\u201d:&quot;value&quot; \u3068\u3044\u3046 \u8981\u7d20\u304c\u3042\u308c\u3070\u3001name=o[&quot;name&quot;] \u3068 \u5c55\u958b\u3055\u308c\u305f\u72b6\u614b\u3067\u3001\u5f15\u6570\u3068\u3057\u3066\u6e21\u3055\u308c\u308b\u3068\u3044\u3046\u3053\u3068\u3067\u3059\u3002<\/p>\n<h2>\u5b9f\u884c\u3057\u3066\u307f\u308b<\/h2>\n<p>\u3053\u3053\u307e\u3067\u3067\u3001\u307e\u305a\u306f\u3001\u5358\u7d14\u306a\u69cb\u9020\u306e\u30aa\u30d6\u30b8\u30a7\u30af\u30c8\u306a\u3089\u3001\u753b\u9762\u3068GAE\u5074\u3067SON\u3092\u7d4c\u7531\u3057\u3066\u6975\u529b\u5909\u63db\u30b3\u30fc\u30c9\u3092\u66f8\u304b\u305a\u306b\u3001\u30e2\u30c7\u30eb\u306e\u3084\u308a\u3068\u308a\u304c\u3067\u304d\u308b\u69d8\u306b\u306a\u308a\u307e\u3057\u305f\u3002\u8907\u96d1\u306a\u69cb\u9020\u3084\u3001GAE\u306eModel\u306e\u30d5\u30a3\u30fc\u30eb\u30c9\u30c7\u30fc\u30bf\u578b\u306b\u3064\u3044\u3066\u306f\u3001\u4eca\u5f8c\u306e\u8ab2\u984c\u3068\u3057\u3066\u884c\u304d\u305f\u3044\u3068\u601d\u3044\u307e\u3059\u3002<\/p>\n<p>\u4e0a\u8a18\u3067\u3001jQuery.ajax \u306e\u7d50\u679c\u304b\u3089\u751f\u6210\u3057\u305f\u30aa\u30d6\u30b8\u30a7\u30af\u30c8\u3092\u3001\u30b0\u30ed\u30fc\u30d0\u30eb\u5909\u6570 test.json \u306b\u5165\u308c\u307e\u3057\u305f\u304c\u3001Chrome \u306e\u30c7\u30d0\u30c3\u30ac\u3067\u5185\u5bb9\u3092\u898b\u3066\u307f\u308b\u3068\u3001\u304d\u3061\u3093\u3068\u5024\u304c\u3084\u308a\u3068\u308a\u3055\u308c\u3066\u3044\u308b\u3053\u3068\u304c\u308f\u304b\u308a\u307e\u3059\u3002<\/p>\n<p><a href=\"https:\/\/www.typea.info\/blog\/wp-content\/uploads\/image\/GAEModelJavaScriptJSON_1231\/gae_json01.jpg\"><img loading=\"lazy\" decoding=\"async\" title=\"gae_json01\" style=\"border-left-width: 0px; border-right-width: 0px; border-bottom-width: 0px; display: inline; border-top-width: 0px\" border=\"0\" alt=\"gae_json01\" src=\"https:\/\/www.typea.info\/blog\/wp-content\/uploads\/image\/GAEModelJavaScriptJSON_1231\/gae_json01_thumb.jpg\" width=\"515\" height=\"117\" \/><\/a> <\/p>\n<p>\u3088\u3057\u3088\u3057\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>GAE \u3068 jQuery \u3067 JSON \u3092\u3084\u308a\u3068\u308a\u3059\u308b\u6e96\u5099\u304c\u3068\u3068\u306e\u3063\u305f\u306e\u3067\u3001GAE(Python) \u306e db.Model \u3068\u3001JavaScript \u3067\u6271\u3046\u30e2\u30c7\u30eb\u3092\u76f8\u4e92\u306b\u5909\u63db\u3055\u305b\u305f\u3044\u3002 \u57fa\u672c\u7dda\u3068\u3057\u3066\u307e\u305a\u306f\u3001 \u306a\u308b\u3079\u304f\u5909\u63db\u30b3 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"sns_share_botton_hide":"","vkExUnit_sns_title":"","_vk_print_noindex":"","sitemap_hide":"","_veu_custom_css":"","veu_display_promotion_alert":"","vkexunit_cta_each_option":"","footnotes":""},"categories":[19,26,20],"tags":[],"class_list":["post-360","post","type-post","status-publish","format-standard","hentry","category-googleappengine","category-jquery","category-python"],"veu_head_title_object":{"title":"","add_site_title":""},"_links":{"self":[{"href":"https:\/\/www.typea.info\/blog\/index.php\/wp-json\/wp\/v2\/posts\/360","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.typea.info\/blog\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.typea.info\/blog\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.typea.info\/blog\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.typea.info\/blog\/index.php\/wp-json\/wp\/v2\/comments?post=360"}],"version-history":[{"count":0,"href":"https:\/\/www.typea.info\/blog\/index.php\/wp-json\/wp\/v2\/posts\/360\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.typea.info\/blog\/index.php\/wp-json\/wp\/v2\/media?parent=360"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.typea.info\/blog\/index.php\/wp-json\/wp\/v2\/categories?post=360"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.typea.info\/blog\/index.php\/wp-json\/wp\/v2\/tags?post=360"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}