{"id":180462,"date":"2024-05-08T20:35:49","date_gmt":"2024-05-08T12:35:49","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/180462.html"},"modified":"2024-05-08T20:35:58","modified_gmt":"2024-05-08T12:35:58","slug":"r%e8%af%ad%e8%a8%80%e5%8f%91%e9%80%81json%e8%a7%a3%e6%9e%90json%e6%9c%89%e4%bb%80%e4%b9%88%e5%8c%85%e5%8f%af%e4%bb%a5%e4%bd%bf%e7%94%a8%e5%90%97","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/180462.html","title":{"rendered":"R\u8bed\u8a00\u53d1\u9001json\u89e3\u6790json\u6709\u4ec0\u4e48\u5305\u53ef\u4ee5\u4f7f\u7528\u5417"},"content":{"rendered":"<p style=\"text-align:center\"><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/27065700\/b531ad60-0a47-47a3-b367-dd152d4efa61.webp\" alt=\"R\u8bed\u8a00\u53d1\u9001json\u89e3\u6790json\u6709\u4ec0\u4e48\u5305\u53ef\u4ee5\u4f7f\u7528\u5417\" \/><\/p>\n<p><p>\u5728\u5904\u7406JSON\u6570\u636e\u65f6\uff0cR\u8bed\u8a00\u63d0\u4f9b\u4e86\u51e0\u4e2a\u9ad8\u6548\u7684\u5305\u6765\u53d1\u9001\u548c\u89e3\u6790JSON\u6570\u636e\u3002<strong>\u8fd9\u4e9b\u4e3b\u529b\u5de5\u5177\u5305\u62ec<code>httr<\/code>\u3001<code>jsonlite<\/code>\u3001<code>rjson<\/code><\/strong>\u3002\u5176\u4e2d\uff0c<strong><code>jsonlite<\/code>\u5305<\/strong>\u7279\u522b\u503c\u5f97\u63a8\u8350\uff0c\u5b83\u4e0d\u4ec5\u652f\u6301\u89e3\u6790JSON\u6570\u636e\uff0c\u8fd8\u53ef\u4ee5\u8f7b\u677e\u5730\u5c06R\u5bf9\u8c61\u8f6c\u6362\u6210JSON\u683c\u5f0f\uff0c\u5e76\u4e14\u4e0e\u5176\u4ed6\u6a21\u5757\u6216API\u4ea4\u4e92\u65f6\u5341\u5206\u6709\u6548\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001JSONLITE\u5305\u7684\u4f7f\u7528<\/h3>\n<\/p>\n<p><p><code>jsonlite<\/code>\u662f\u6700\u53d7\u6b22\u8fce\u4e14\u529f\u80fd\u6700\u4e3a\u5168\u9762\u7684R\u5305\u4e4b\u4e00\uff0c\u7528\u4e8e\u5904\u7406JSON\u6570\u636e\u3002\u5b83\u63d0\u4f9b\u4e86\u4e00\u79cd\u7b80\u5355\u4e14\u4e00\u81f4\u7684\u65b9\u6cd5\u6765\u5c06JSON\u6570\u636e\u8f6c\u6362\u6210R\u5bf9\u8c61\uff0c\u53cd\u4e4b\u4ea6\u7136\u3002<\/p>\n<\/p>\n<ul>\n<li>\n<p><strong>\u89e3\u6790JSON\u6570\u636e<\/strong>\uff1a\u4f7f\u7528<code>fromJSON<\/code>\u51fd\u6570\uff0c\u4f60\u53ef\u4ee5\u8f7b\u677e\u5c06JSON\u5b57\u7b26\u4e32\u6216\u6587\u4ef6\u89e3\u6790\u4e3aR\u7684\u6570\u636e\u5e27\u6216\u5217\u8868\u3002\u5b83\u975e\u5e38\u9002\u7528\u4e8e\u5904\u7406\u4ece\u7f51\u7edcAPI\u63a5\u6536\u5230\u7684\u6570\u636e\uff0c\u56e0\u4e3a\u8fd9\u4e9b\u6570\u636e\u901a\u5e38\u4ee5JSON\u683c\u5f0f\u63d0\u4f9b\u3002<code>fromJSON<\/code>\u51fd\u6570\u4f1a\u667a\u80fd\u5730\u5904\u7406\u4e0d\u540c\u7684\u6570\u636e\u7c7b\u578b\uff0c\u5305\u62ec\u6570\u5b57\u3001\u5b57\u7b26\u4e32\u3001\u5e03\u5c14\u503c\u7b49\uff0c\u786e\u4fdd\u8f6c\u6362\u540e\u7684R\u5bf9\u8c61\u4fdd\u6301\u4e86JSON\u6570\u636e\u7684\u7ed3\u6784\u548c\u7c7b\u578b\u7279\u5f81\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u751f\u6210JSON\u6570\u636e<\/strong>\uff1a<code>toJSON<\/code>\u51fd\u6570\u5141\u8bb8\u4f60\u5c06R\u5bf9\u8c61\u8f6c\u6362\u6210JSON\u683c\u5f0f\u7684\u5b57\u7b26\u4e32\u3002\u975e\u5e38\u9002\u7528\u4e8e\u5f53\u4f60\u9700\u8981\u5c06R\u6570\u636e\u53d1\u9001\u5230\u4e00\u4e2a\u7f51\u7edcAPI\u6216\u5b58\u50a8\u4e3aJSON\u6587\u4ef6\u65f6\u3002\u8fd9\u4e2a\u51fd\u6570\u652f\u6301\u5404\u79cdR\u5bf9\u8c61\u7c7b\u578b\uff0c\u5305\u62ec\u6570\u636e\u5e27\u3001\u5217\u8868\u3001\u77e9\u9635\u7b49\uff0c\u5e76\u63d0\u4f9b\u591a\u79cd\u9009\u9879\u6765\u81ea\u5b9a\u4e49\u6700\u7ec8\u751f\u6210\u7684JSON\u5b57\u7b26\u4e32\u7684\u683c\u5f0f\uff0c\u5982\u7f8e\u5316\u8f93\u51fa\u3001\u6570\u5b57\u7cbe\u5ea6\u7b49\u3002<\/p>\n<\/p>\n<\/li>\n<\/ul>\n<p><h3>\u4e8c\u3001HTTR\u5305\u7684\u8fd0\u7528<\/h3>\n<\/p>\n<p><p><code>httr<\/code>\u5305\u662f\u53e6\u4e00\u4e2a\u7528\u4e8eWeb API\u4ea4\u4e91\u4ee5\u53caHTTP\u8bf7\u6c42\u5904\u7406\u7684\u91cd\u8981R\u5305\u3002\u7279\u522b\u662f\uff0c\u5b83\u5728\u53d1\u9001JSON\u6570\u636e\u548c\u63a5\u6536JSON\u683c\u5f0f\u7684\u54cd\u5e94\u65f6\u663e\u5f97\u5c24\u4e3a\u91cd\u8981\u3002<\/p>\n<\/p>\n<ul>\n<li>\n<p><strong>\u53d1\u9001HTTP\u8bf7\u6c42<\/strong>\uff1a\u4f7f\u7528<code>httr<\/code>\u7684<code>GET<\/code>\u3001<code>POST<\/code>\u3001<code>PUT<\/code>\u7b49\u51fd\u6570\uff0c\u53ef\u4ee5\u5411\u6307\u5b9a\u7684URL\u53d1\u9001HTTP\u8bf7\u6c42\u3002\u5f53\u9700\u8981\u4e0e\u8fd4\u56deJSON\u6570\u636e\u7684Web API\u4ea4\u4e92\u65f6\uff0c\u4f60\u53ef\u4ee5\u901a\u8fc7<code>POST<\/code>\u51fd\u6570\uff0c\u914d\u5408<code>body<\/code>\u53c2\u6570\u4e2d\u4f7f\u7528<code>toJSON<\/code>\u51fd\u6570\u5904\u7406\u540e\u7684\u5185\u5bb9\uff0c\u53d1\u9001JSON\u683c\u5f0f\u7684\u6570\u636e\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u5904\u7406HTTP\u54cd\u5e94<\/strong>\uff1a\u6536\u5230HTTP\u54cd\u5e94\u540e\uff0c\u4f7f\u7528<code>httr<\/code>\u5305\u4e2d\u7684<code>content<\/code>\u51fd\u6570\u53ef\u4ee5\u8f7b\u677e\u63d0\u53d6\u54cd\u5e94\u4f53\u3002\u5bf9\u4e8eJSON\u683c\u5f0f\u7684\u54cd\u5e94\uff0c<code>content<\/code>\u51fd\u6570\u53ef\u4ee5\u76f4\u63a5\u5c06\u54cd\u5e94\u4f53\u89e3\u6790\u4e3aR\u5bf9\u8c61\uff0c\u7b80\u5316\u4e86\u6570\u636e\u5904\u7406\u6d41\u7a0b\u3002<\/p>\n<\/p>\n<\/li>\n<\/ul>\n<p><h3>\u4e09\u3001RJSON\u5305\u7684\u7279\u70b9<\/h3>\n<\/p>\n<p><p><code>rjson<\/code>\u5305\u63d0\u4f9b\u4e86\u53e6\u4e00\u79cd\u9009\u62e9\u7528\u4e8e\u89e3\u6790JSON\u6570\u636e\u548c\u751f\u6210JSON\u6570\u636e\u3002\u76f8\u6bd4<code>jsonlite<\/code>\uff0c<code>rjson<\/code>\u7684\u529f\u80fd\u53ef\u80fd\u4e0d\u90a3\u4e48\u4e30\u5bcc\uff0c\u4f46\u5b83\u63d0\u4f9b\u4e86\u8db3\u591f\u7684\u529f\u80fd\u8fdb\u884c\u57fa\u672c\u7684JSON\u6570\u636e\u5904\u7406\u3002<\/p>\n<\/p>\n<ul>\n<li>\n<p><strong>\u57fa\u672c\u89e3\u6790\u548c\u751f\u6210<\/strong>\uff1a\u901a\u8fc7<code>fromJSON<\/code>\u548c<code>toJSON<\/code>\u51fd\u6570\uff0c<code>rjson<\/code>\u5305\u5141\u8bb8\u4ece\u7b80\u5355\u5230\u590d\u6742\u7684JSON\u6570\u636e\u8fdb\u884c\u89e3\u6790\u548c\u751f\u6210\u3002\u867d\u7136\u5b83\u53ef\u80fd\u4e0d\u652f\u6301<code>jsonlite<\/code>\u5305\u90a3\u6837\u7684\u9ad8\u7ea7\u7279\u6027\uff0c\u4f46\u5bf9\u4e8e\u57fa\u672c\u9700\u6c42\u6765\u8bf4\u662f\u5b8c\u5168\u8db3\u591f\u7684\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u6027\u80fd\u8003\u91cf<\/strong>\uff1a\u5728\u5904\u7406\u975e\u5e38\u5927\u7684\u6570\u636e\u96c6\u65f6\uff0c<code>rjson<\/code>\u4e0e<code>jsonlite<\/code>\u7684\u6027\u80fd\u53ef\u80fd\u4f1a\u6709\u6240\u4e0d\u540c\u3002\u56e0\u6b64\uff0c\u5728\u9009\u62e9\u9002\u5408\u7684\u5305\u65f6\uff0c\u8003\u8651\u6570\u636e\u7684\u5927\u5c0f\u548c\u5904\u7406\u9700\u6c42\u662f\u5f88\u91cd\u8981\u7684\u3002<\/p>\n<\/p>\n<\/li>\n<\/ul>\n<p><h3>\u56db\u3001\u9009\u62e9\u5408\u9002\u7684\u5305<\/h3>\n<\/p>\n<p><p>\u9009\u62e9<code>jsonlite<\/code>\u3001<code>httr<\/code>\u8fd8\u662f<code>rjson<\/code>\uff0c\u53d6\u51b3\u4e8e\u4f60\u7684\u5177\u4f53\u9700\u6c42\u3002<strong><code>jsonlite<\/code>\u7531\u4e8e\u5176\u529f\u80fd\u5168\u9762\u548c\u4f7f\u7528\u4fbf\u6377\uff0c\u901a\u5e38\u662f\u9996\u9009<\/strong>\u3002\u5bf9\u4e8e\u9700\u8981\u8fdb\u884c\u590d\u6742HTTP\u8bf7\u6c42\u7684\u9879\u76ee\uff0c<strong>\u7ed3\u5408<code>httr<\/code>\u548c<code>jsonlite<\/code>\u4f7f\u7528\uff0c\u5c06\u63d0\u4f9b\u66f4\u9ad8\u7684\u7075\u6d3b\u6027\u548c\u6548\u7387<\/strong>\u3002\u5982\u679c\u9879\u76ee\u8981\u6c42\u8f83\u4e3a\u7b80\u5355\uff0c\u4e14\u5bf9\u5305\u7684\u4f53\u79ef\u6709\u6240\u8003\u8651\uff0c<code>rjson<\/code>\u53ef\u80fd\u662f\u4e00\u4e2a\u8f7b\u91cf\u7ea7\u7684\u9009\u62e9\u3002<\/p>\n<\/p>\n<p><p>\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\uff0c\u4e86\u89e3\u6bcf\u4e2a\u5305\u7684\u4f18\u52bf\u548c\u5c40\u9650\u6027\uff0c\u6839\u636e\u9879\u76ee\u9700\u6c42\u505a\u51fa\u660e\u667a\u7684\u9009\u62e9\uff0c\u662f\u53d1\u6325R\u8bed\u8a00\u5904\u7406JSON\u6570\u636e\u5f3a\u5927\u529f\u80fd\u7684\u5173\u952e\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p><strong>\u5982\u4f55\u5728R\u8bed\u8a00\u4e2d\u53d1\u9001json\u6570\u636e\uff1f<\/strong><\/p>\n<p>R\u8bed\u8a00\u4e2d\u53ef\u4ee5\u4f7f\u7528httr\u5305\u6765\u53d1\u9001json\u6570\u636e\u3002\u9996\u5148\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528jsonlite\u5305\u4e2d\u7684toJSON\u51fd\u6570\u5c06R\u5bf9\u8c61\u8f6c\u6362\u4e3ajson\u683c\u5f0f\u7684\u5b57\u7b26\u4e32\u3002\u7136\u540e\uff0c\u4f7f\u7528httr\u5305\u4e2d\u7684POST\u51fd\u6570\u53d1\u9001json\u6570\u636e\u3002<\/p>\n<pre><code class=\"language-R\">library(httr)\nlibrary(jsonlite)\n\n# \u521b\u5efa\u4e00\u4e2aR\u5bf9\u8c61\uff0c\u4f8b\u5982\u4e00\u4e2a\u5305\u542b\u6570\u636e\u7684\u5217\u8868\ndata &lt;- list(name=&quot;John&quot;, age=30, city=&quot;New York&quot;)\n\n# \u5c06R\u5bf9\u8c61\u8f6c\u6362\u4e3ajson\u5b57\u7b26\u4e32\njson &lt;- toJSON(data)\n\n# \u53d1\u9001json\u6570\u636e\nresponse &lt;- POST(url, body = json, encode = &quot;json&quot;, add_headers(&quot;Content-Type&quot; = &quot;application\/json&quot;))\n\n# \u67e5\u770b\u670d\u52a1\u5668\u8fd4\u56de\u7684\u7ed3\u679c\ncontent(response)\n<\/code><\/pre>\n<p><strong>\u7528\u54ea\u4e9bR\u8bed\u8a00\u5305\u6765\u89e3\u6790json\u6570\u636e\uff1f<\/strong><\/p>\n<p>\u5728R\u8bed\u8a00\u4e2d\uff0c\u6709\u51e0\u4e2a\u5e38\u7528\u7684\u5305\u53ef\u7528\u4e8e\u89e3\u6790json\u6570\u636e\u3002\u5176\u4e2d\u6700\u5e38\u7528\u7684\u662fjsonlite\u5305\uff0c\u5b83\u63d0\u4f9b\u4e86\u8bb8\u591a\u51fd\u6570\u7528\u4e8e\u89e3\u6790\u548c\u5904\u7406json\u6570\u636e\u3002\u6b64\u5916\uff0crjson\u548cRJSONIO\u4e5f\u662f\u89e3\u6790json\u6570\u636e\u7684\u53e6\u4e24\u4e2a\u6d41\u884c\u7684\u5305\u3002<\/p>\n<pre><code class=\"language-R\">library(jsonlite)\nlibrary(rjson)\nlibrary(RJSONIO)\n\n# \u4ecejson\u6587\u4ef6\u4e2d\u8bfb\u53d6json\u6570\u636e\ndata &lt;- fromJSON(file = &quot;data.json&quot;)\n\n# \u89e3\u6790json\u5b57\u7b26\u4e32\ndata &lt;- fromJSON(json_string)\n\n# \u83b7\u53d6json\u5bf9\u8c61\u7684\u7279\u5b9a\u503c\nvalue &lt;- data$name\n\n# \u4f7f\u7528rjson\u5305\u6765\u89e3\u6790json\ndata &lt;- rjson::fromJSON(json_string)\n\n# \u4f7f\u7528RJSONIO\u5305\u6765\u89e3\u6790json\ndata &lt;- RJSONIO::fromJSON(json_string)\n<\/code><\/pre>\n<p><strong>\u5982\u4f55\u5c06json\u6570\u636e\u8f6c\u6362\u4e3aR\u6570\u636e\u6846\uff1f<\/strong><\/p>\n<p>\u5c06json\u6570\u636e\u8f6c\u6362\u4e3aR\u6570\u636e\u6846\u53ef\u4ee5\u4f7f\u7528jsonlite\u5305\u4e2d\u7684\u51fd\u6570\u3002\u53ef\u4ee5\u4f7f\u7528fromJSON\u51fd\u6570\u5c06json\u6570\u636e\u89e3\u6790\u4e3aR\u5bf9\u8c61\uff0c\u7136\u540e\u4f7f\u7528as.data.frame\u51fd\u6570\u5c06\u5176\u8f6c\u6362\u4e3a\u6570\u636e\u6846\u3002<\/p>\n<pre><code class=\"language-R\">library(jsonlite)\n\n# \u89e3\u6790json\u6570\u636e\u5e76\u5c06\u5176\u8f6c\u6362\u4e3aR\u5bf9\u8c61\ndata &lt;- fromJSON(json_string)\n\n# \u5c06R\u5bf9\u8c61\u8f6c\u6362\u4e3a\u6570\u636e\u6846\ndataframe &lt;- as.data.frame(data)\n<\/code><\/pre>\n<p>\u6ce8\u610f\uff1a\u5982\u679cjson\u6570\u636e\u662f\u5d4c\u5957\u7684\uff0c\u5e76\u4e14\u5305\u542b\u591a\u4e2a\u5c42\u6b21\u7684\u7ed3\u6784\uff0c\u53ef\u4ee5\u4f7f\u7528flatten\u51fd\u6570\u6765\u5c06\u5176\u6241\u5e73\u5316\uff0c\u7136\u540e\u518d\u4f7f\u7528as.data.frame\u51fd\u6570\u8f6c\u6362\u4e3a\u6570\u636e\u6846\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5728\u5904\u7406JSON\u6570\u636e\u65f6\uff0cR\u8bed\u8a00\u63d0\u4f9b\u4e86\u51e0\u4e2a\u9ad8\u6548\u7684\u5305\u6765\u53d1\u9001\u548c\u89e3\u6790JSON\u6570\u636e\u3002\u8fd9\u4e9b\u4e3b\u529b\u5de5\u5177\u5305\u62echttr\u3001jsonli [&hellip;]","protected":false},"author":3,"featured_media":180473,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[37],"tags":[],"acf":[],"_links":{"self":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/180462"}],"collection":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/comments?post=180462"}],"version-history":[{"count":0,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/180462\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/180473"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=180462"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=180462"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=180462"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}