{"id":401,"date":"2023-03-23T07:30:50","date_gmt":"2023-03-23T07:30:50","guid":{"rendered":"https:\/\/yunshangtulv.com.cn\/?p=401"},"modified":"2023-03-23T07:30:50","modified_gmt":"2023-03-23T07:30:50","slug":"json%e6%96%87%e4%bb%b6%e8%af%a6%e8%a7%a3%e5%8f%8a%e5%ba%94%e7%94%a8%e5%ae%9e%e4%be%8b","status":"publish","type":"post","link":"https:\/\/yunshangtulv.com.cn\/?p=401","title":{"rendered":"json\u6587\u4ef6\u8be6\u89e3\u53ca\u5e94\u7528\u5b9e\u4f8b"},"content":{"rendered":"<p>R\u8bed\u8a00\u4e2d\u53ef\u4ee5\u4f7f\u7528<code>jsonlite<\/code>\u5305\u6765\u5904\u7406JSON\u6587\u4ef6\u3002<code>jsonlite<\/code>\u5305\u63d0\u4f9b\u4e86\u4e00\u7cfb\u5217\u51fd\u6570\uff0c\u7528\u4e8e\u8bfb\u53d6\u3001\u89e3\u6790\u3001\u64cd\u4f5c\u548c\u5199\u5165JSON\u6587\u4ef6\u3002\u9996\u5148\uff0c\u786e\u4fdd\u5df2\u7ecf\u5b89\u88c5\u4e86<code>jsonlite<\/code>\u5305\u3002\u5982\u679c\u6ca1\u6709\u5b89\u88c5\uff0c\u8bf7\u4f7f\u7528\u4ee5\u4e0b\u547d\u4ee4\u8fdb\u884c\u5b89\u88c5\uff1a<\/p>\n<div class=\"bg-black rounded-md mb-4\">\n<div class=\"flex items-center relative text-gray-200 bg-gray-800 px-4 py-2 text-xs font-sans justify-between rounded-t-md\"><\/div>\n<div class=\"p-4 overflow-y-auto\"><code class=\"!whitespace-pre hljs language-R\">install.packages<span class=\"hljs-punctuation\">(<\/span><span class=\"hljs-string\">\"jsonlite\"<\/span><span class=\"hljs-punctuation\">)<\/span><br \/>\n<\/code><\/div>\n<\/div>\n<p>\u63a5\u4e0b\u6765\uff0c\u6211\u4eec\u5c06\u901a\u8fc7\u4e00\u4e9b\u5e94\u7528\u5b9e\u4f8b\u6765\u8be6\u7ec6\u89e3\u91caR\u8bed\u8a00\u7684JSON\u6587\u4ef6\u64cd\u4f5c\u3002<\/p>\n<ol>\n<li>\u8bfb\u53d6\u548c\u89e3\u6790JSON\u6587\u4ef6<\/li>\n<\/ol>\n<p>\u5047\u8bbe\u6211\u4eec\u6709\u4ee5\u4e0bJSON\u6587\u4ef6\uff08data.json\uff09\uff1a<\/p>\n<div class=\"bg-black rounded-md mb-4\">\n<div class=\"flex items-center relative text-gray-200 bg-gray-800 px-4 py-2 text-xs font-sans justify-between rounded-t-md\"><\/div>\n<div class=\"p-4 overflow-y-auto\"><code class=\"!whitespace-pre hljs language-json\"><span class=\"hljs-punctuation\">{<\/span><br \/>\n  <span class=\"hljs-attr\">\"employees\"<\/span><span class=\"hljs-punctuation\">:<\/span> <span class=\"hljs-punctuation\">[<\/span><br \/>\n    <span class=\"hljs-punctuation\">{<\/span><br \/>\n      <span class=\"hljs-attr\">\"name\"<\/span><span class=\"hljs-punctuation\">:<\/span> <span class=\"hljs-string\">\"John Doe\"<\/span><span class=\"hljs-punctuation\">,<\/span><br \/>\n      <span class=\"hljs-attr\">\"age\"<\/span><span class=\"hljs-punctuation\">:<\/span> <span class=\"hljs-number\">30<\/span><span class=\"hljs-punctuation\">,<\/span><br \/>\n      <span class=\"hljs-attr\">\"position\"<\/span><span class=\"hljs-punctuation\">:<\/span> <span class=\"hljs-string\">\"Software Engineer\"<\/span><br \/>\n    <span class=\"hljs-punctuation\">}<\/span><span class=\"hljs-punctuation\">,<\/span><br \/>\n    <span class=\"hljs-punctuation\">{<\/span><br \/>\n      <span class=\"hljs-attr\">\"name\"<\/span><span class=\"hljs-punctuation\">:<\/span> <span class=\"hljs-string\">\"Jane Smith\"<\/span><span class=\"hljs-punctuation\">,<\/span><br \/>\n      <span class=\"hljs-attr\">\"age\"<\/span><span class=\"hljs-punctuation\">:<\/span> <span class=\"hljs-number\">28<\/span><span class=\"hljs-punctuation\">,<\/span><br \/>\n      <span class=\"hljs-attr\">\"position\"<\/span><span class=\"hljs-punctuation\">:<\/span> <span class=\"hljs-string\">\"Data Analyst\"<\/span><br \/>\n    <span class=\"hljs-punctuation\">}<\/span><span class=\"hljs-punctuation\">,<\/span><br \/>\n    <span class=\"hljs-punctuation\">{<\/span><br \/>\n      <span class=\"hljs-attr\">\"name\"<\/span><span class=\"hljs-punctuation\">:<\/span> <span class=\"hljs-string\">\"Mike Brown\"<\/span><span class=\"hljs-punctuation\">,<\/span><br \/>\n      <span class=\"hljs-attr\">\"age\"<\/span><span class=\"hljs-punctuation\">:<\/span> <span class=\"hljs-number\">35<\/span><span class=\"hljs-punctuation\">,<\/span><br \/>\n      <span class=\"hljs-attr\">\"position\"<\/span><span class=\"hljs-punctuation\">:<\/span> <span class=\"hljs-string\">\"Project Manager\"<\/span><br \/>\n    <span class=\"hljs-punctuation\">}<\/span><br \/>\n  <span class=\"hljs-punctuation\">]<\/span><br \/>\n<span class=\"hljs-punctuation\">}<\/span><br \/>\n<\/code><\/div>\n<\/div>\n<p>\u9996\u5148\uff0c\u4f7f\u7528<code>fromJSON<\/code>\u51fd\u6570\u8bfb\u53d6\u5e76\u89e3\u6790JSON\u6587\u4ef6\uff1a<\/p>\n<div class=\"bg-black rounded-md mb-4\">\n<div class=\"flex items-center relative text-gray-200 bg-gray-800 px-4 py-2 text-xs font-sans justify-between rounded-t-md\"><\/div>\n<div class=\"p-4 overflow-y-auto\"><code class=\"!whitespace-pre hljs language-R\">library<span class=\"hljs-punctuation\">(<\/span>jsonlite<span class=\"hljs-punctuation\">)<\/span><\/p>\n<p><span class=\"hljs-comment\"># \u8bfb\u53d6\u5e76\u89e3\u6790JSON\u6587\u4ef6<\/span><br \/>\njson_data <span class=\"hljs-operator\">&lt;-<\/span> fromJSON<span class=\"hljs-punctuation\">(<\/span><span class=\"hljs-string\">\"data.json\"<\/span><span class=\"hljs-punctuation\">)<\/span><br \/>\n<\/code><\/div>\n<\/div>\n<ol start=\"2\">\n<li>\u63d0\u53d6JSON\u6587\u4ef6\u4e2d\u7684\u6570\u636e<\/li>\n<\/ol>\n<p>\u5728\u89e3\u6790JSON\u6587\u4ef6\u540e\uff0c\u53ef\u4ee5\u76f4\u63a5\u8bbf\u95ee\u6570\u636e\u6846\u4e2d\u7684\u6570\u636e\u3002\u4f8b\u5982\uff0c\u63d0\u53d6\u6240\u6709\u5458\u5de5\u7684\u59d3\u540d\uff1a<\/p>\n<div class=\"bg-black rounded-md mb-4\">\n<div class=\"flex items-center relative text-gray-200 bg-gray-800 px-4 py-2 text-xs font-sans justify-between rounded-t-md\"><\/div>\n<div class=\"p-4 overflow-y-auto\"><code class=\"!whitespace-pre hljs language-R\"><span class=\"hljs-comment\"># \u63d0\u53d6\u6240\u6709\u5458\u5de5\u7684\u59d3\u540d<\/span><br \/>\n<span class=\"hljs-built_in\">names<\/span> <span class=\"hljs-operator\">&lt;-<\/span> json_data<span class=\"hljs-operator\">$<\/span>employees<span class=\"hljs-operator\">$<\/span>name<\/p>\n<p>print<span class=\"hljs-punctuation\">(<\/span><span class=\"hljs-built_in\">names<\/span><span class=\"hljs-punctuation\">)<\/span><br \/>\n<\/code><\/div>\n<\/div>\n<ol start=\"3\">\n<li>\u64cd\u4f5cJSON\u6587\u4ef6\u4e2d\u7684\u6570\u636e<\/li>\n<\/ol>\n<p>\u4f7f\u7528<code>jsonlite<\/code>\u5305\u4e2d\u7684\u51fd\u6570\uff0c\u53ef\u4ee5\u5bf9JSON\u6570\u636e\u8fdb\u884c\u64cd\u4f5c\uff0c\u5982\u6dfb\u52a0\u3001\u4fee\u6539\u6216\u5220\u9664\u6570\u636e\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u793a\u4f8b\uff0c\u5411JSON\u6587\u4ef6\u4e2d\u6dfb\u52a0\u4e00\u4e2a\u65b0\u7684\u5458\u5de5\uff1a<\/p>\n<div class=\"bg-black rounded-md mb-4\">\n<div class=\"flex items-center relative text-gray-200 bg-gray-800 px-4 py-2 text-xs font-sans justify-between rounded-t-md\"><\/div>\n<div class=\"p-4 overflow-y-auto\"><code class=\"!whitespace-pre hljs language-R\"><span class=\"hljs-comment\"># \u521b\u5efa\u65b0\u7684\u5458\u5de5\u6570\u636e<\/span><br \/>\nnew_employee <span class=\"hljs-operator\">&lt;-<\/span> data.frame<span class=\"hljs-punctuation\">(<\/span>name <span class=\"hljs-operator\">=<\/span> <span class=\"hljs-string\">\"Alice Johnson\"<\/span><span class=\"hljs-punctuation\">,<\/span> age <span class=\"hljs-operator\">=<\/span> <span class=\"hljs-number\">32<\/span><span class=\"hljs-punctuation\">,<\/span> position <span class=\"hljs-operator\">=<\/span> <span class=\"hljs-string\">\"HR Specialist\"<\/span><span class=\"hljs-punctuation\">)<\/span><\/p>\n<p><span class=\"hljs-comment\"># \u5c06\u65b0\u6570\u636e\u6dfb\u52a0\u5230JSON\u6587\u4ef6<\/span><br \/>\njson_data<span class=\"hljs-operator\">$<\/span>employees <span class=\"hljs-operator\">&lt;-<\/span> rbind<span class=\"hljs-punctuation\">(<\/span>json_data<span class=\"hljs-operator\">$<\/span>employees<span class=\"hljs-punctuation\">,<\/span> new_employee<span class=\"hljs-punctuation\">)<\/span><\/p>\n<p><span class=\"hljs-comment\"># \u67e5\u770b\u66f4\u65b0\u540e\u7684JSON\u6570\u636e<\/span><br \/>\nprint<span class=\"hljs-punctuation\">(<\/span>json_data<span class=\"hljs-punctuation\">)<\/span><br \/>\n<\/code><\/div>\n<\/div>\n<ol start=\"4\">\n<li>\u5c06\u4fee\u6539\u540e\u7684JSON\u6570\u636e\u5199\u5165\u6587\u4ef6<\/li>\n<\/ol>\n<p>\u6700\u540e\uff0c\u5c06\u4fee\u6539\u540e\u7684JSON\u6570\u636e\u5199\u5165\u6587\u4ef6\u3002\u4f7f\u7528<code>toJSON<\/code>\u51fd\u6570\u8f6c\u6362\u6570\u636e\u6846\u4e3aJSON\u5b57\u7b26\u4e32\uff0c\u7136\u540e\u4f7f\u7528<code>writeLines<\/code>\u51fd\u6570\u5c06JSON\u5b57\u7b26\u4e32\u5199\u5165\u6587\u4ef6\uff1a<\/p>\n<div class=\"bg-black rounded-md mb-4\">\n<div class=\"flex items-center relative text-gray-200 bg-gray-800 px-4 py-2 text-xs font-sans justify-between rounded-t-md\"><\/div>\n<div class=\"p-4 overflow-y-auto\"><code class=\"!whitespace-pre hljs language-R\"><span class=\"hljs-comment\"># \u5c06\u6570\u636e\u6846\u8f6c\u6362\u4e3aJSON\u5b57\u7b26\u4e32<\/span><br \/>\nupdated_json <span class=\"hljs-operator\">&lt;-<\/span> toJSON<span class=\"hljs-punctuation\">(<\/span>json_data<span class=\"hljs-punctuation\">,<\/span> pretty <span class=\"hljs-operator\">=<\/span> <span class=\"hljs-literal\">TRUE<\/span><span class=\"hljs-punctuation\">)<\/span><\/p>\n<p><span class=\"hljs-comment\"># \u5c06JSON\u5b57\u7b26\u4e32\u5199\u5165\u65b0\u6587\u4ef6<\/span><br \/>\nwriteLines<span class=\"hljs-punctuation\">(<\/span>updated_json<span class=\"hljs-punctuation\">,<\/span> <span class=\"hljs-string\">\"updated_data.json\"<\/span><span class=\"hljs-punctuation\">)<\/span><br \/>\n<\/code><\/div>\n<\/div>\n<p>\u901a\u8fc7\u4ee5\u4e0a\u793a\u4f8b\uff0c\u6211\u4eec\u4e86\u89e3\u4e86\u5982\u4f55\u5728R\u8bed\u8a00\u4e2d\u5904\u7406JSON\u6587\u4ef6\u3002\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\uff0cJSON\u6587\u4ef6\u7684\u7ed3\u6784\u548c\u5185\u5bb9\u53ef\u80fd\u66f4\u4e3a\u590d\u6742\uff0c\u4f46\u57fa\u672c\u7684\u8bfb\u53d6\u3001\u89e3\u6790\u3001\u64cd\u4f5c\u548c\u5199\u5165\u65b9\u6cd5\u662f\u76f8\u4f3c\u7684\u3002\u66f4\u591a\u5173\u4e8e<code>jsonlite<\/code>\u5305\u7684\u4fe1\u606f\uff0c\u53ef\u4ee5\u67e5\u9605\u76f8\u5173\u6587\u6863\u6216\u8bbf\u95ee\u4ee5\u4e0b\u94fe\u63a5\uff1a<\/p>\n<ul>\n<li><a href=\"https:\/\/www.rdocumentation.org\/packages\/jsonlite\/versions\/1.7.2\" target=\"_new\" rel=\"noopener\">jsonlite Package Documentation<\/a><\/li>\n<li><a href=\"https:\/\/cran.r-project.org\/web\/packages\/jsonlite\/index.html\" target=\"_new\" rel=\"noopener\">CRAN: jsonlite Package<\/a><\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"<p>R\u8bed\u8a00\u4e2d\u53ef\u4ee5\u4f7f\u7528jsonlite\u5305\u6765\u5904\u7406JSON\u6587\u4ef6\u3002jsonlite\u5305\u63d0\u4f9b\u4e86\u4e00\u7cfb\u5217\u51fd\u6570\uff0c\u7528\u4e8e\u8bfb\u53d6\u3001\u89e3\u6790\u3001\u64cd\u4f5c [&hellip;]<\/p>\n","protected":false},"author":111,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[9],"tags":[],"class_list":["post-401","post","type-post","status-publish","format-standard","hentry","category-r"],"_links":{"self":[{"href":"https:\/\/yunshangtulv.com.cn\/index.php?rest_route=\/wp\/v2\/posts\/401","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/yunshangtulv.com.cn\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/yunshangtulv.com.cn\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/yunshangtulv.com.cn\/index.php?rest_route=\/wp\/v2\/users\/111"}],"replies":[{"embeddable":true,"href":"https:\/\/yunshangtulv.com.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=401"}],"version-history":[{"count":0,"href":"https:\/\/yunshangtulv.com.cn\/index.php?rest_route=\/wp\/v2\/posts\/401\/revisions"}],"wp:attachment":[{"href":"https:\/\/yunshangtulv.com.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=401"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/yunshangtulv.com.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=401"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/yunshangtulv.com.cn\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=401"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}