{"id":37,"date":"2025-10-14T14:04:03","date_gmt":"2025-10-14T06:04:03","guid":{"rendered":"http:\/\/47.99.178.170\/?p=37"},"modified":"2025-10-23T21:47:32","modified_gmt":"2025-10-23T13:47:32","slug":"%e6%ba%90%e7%a0%81%e8%a7%a3%e6%9e%90-cjson%e6%ba%90%e7%a0%81%e8%a7%a3%e6%9e%90","status":"publish","type":"post","link":"https:\/\/www.garfield-blog.top\/?p=37","title":{"rendered":"\u3010\u6e90\u7801\u89e3\u6790\u3011 cJSON\u6e90\u7801\u89e3\u6790"},"content":{"rendered":"\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\">\n<p>\u6e90\u7801\u4e0b\u8f7d\u94fe\u63a5https:\/\/sourceforge.net\/projects\/cjson\/<\/p>\n<\/blockquote>\n\n\n\n<h2 class=\"wp-block-heading\">1. \u6838\u5fc3\u6570\u636e\u7ed3\u6784<\/h2>\n\n\n\n<p>\u76f8\u540c\u7b49\u7ea7\u7684\u5143\u7d20\u4f7f\u7528\u53cc\u5411\u94fe\u8868\u94fe\u63a5\uff0c\u4e0d\u540c\u7b49\u7ea7\u7684\u5143\u7d20\u4f7f\u7528<code>child<\/code>\u6307\u9488\u8fde\u63a5\u3002\u4f8b\u5982\uff1a\u591a\u4e2a<code>object<\/code>\u5e76\u5217\u65f6\u6216\u7740<code>object<\/code>\u5185\u90e8\u7684\u6210\u5458\u4f7f\u7528\u53cc\u5411\u94fe\u8868\u8fde\u63a5\uff0c<code>object<\/code>\u540d\u79f0\u4e0e\u5176\u5185\u90e8\u7684\u6210\u5458\u901a\u8fc7<code>child<\/code>\u8fde\u63a5<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>typedef struct cJSON {\n    struct cJSON *next,*prev;   \/\/ \u53cc\u5411\u94fe\u8868\u5934\u8282\u70b9\u548c\u5c3e\u8282\u70b9\n    struct cJSON *child;        \/\/ \u5b58\u50a8\u5b50\u9879\n    int type;                   \/\/ \u8282\u70b9\u7c7b\u578b\n    char *valuestring;          \/\/ \u5b58\u50a8\u5bf9\u8c61\u6570\u7ec4\u6216\u952e\u503c\u5bf9\u7684\u5185\u5bb9\n    int valueint;               \/\/ number\u503c\u7684\u6574\u5f62\u5b58\u50a8\n    double valuedouble;         \/\/ number\u503c\u7684\u6d6e\u70b9\u578b\u5b58\u50a8\n    char *string;               \/\/ \u5b58\u50a8\u5bf9\u8c61\u6570\u7ec4\u6216\u952e\u503c\u5bf9\u7684\u540d\u79f0\n} cJSON;\n\n\/* cJSON Types: *\/\n#define cJSON_False 0\n#define cJSON_True 1\n#define cJSON_NULL 2\n#define cJSON_Number 3\n#define cJSON_String 4\n#define cJSON_Array 5\n#define cJSON_Object 6<\/code><\/pre>\n\n\n\n<p>\u6bcf\u4e00\u4e2a<code>json<\/code>\u8282\u70b9\u5bf9\u5e94\u7740\u4e00\u4e2a<code>cJSON<\/code>\u7ed3\u6784\uff0c\u4ece\u6700\u5916\u5c42\u5230\u6700\u5185\u5c42\u53ef\u4ee5\u770b\u505a\u662f\u4e00\u7ea7\u4e00\u7ea7\uff0c\u8f6c\u5316\u6210\u5bf9\u5e94\u7684\u6570\u636e\u7ed3\u6784\u5b58\u50a8\u5219\u5982\u4e0b\u56fe<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><div class='fancybox-wrapper lazyload-container-unload' data-fancybox='post-images' href='https:\/\/www.garfield-blog.top\/wp-content\/uploads\/2025\/10\/struct-cJSON-scaled.jpg'><img class=\"lazyload lazyload-style-1\" src=\"data:image\/svg+xml;base64,PCEtLUFyZ29uTG9hZGluZy0tPgo8c3ZnIHdpZHRoPSIxIiBoZWlnaHQ9IjEiIHhtbG5zPSJodHRwOi8vd3d3LnczLm9yZy8yMDAwL3N2ZyIgc3Ryb2tlPSIjZmZmZmZmMDAiPjxnPjwvZz4KPC9zdmc+\"  loading=\"lazy\" decoding=\"async\" width=\"2560\" height=\"1978\" data-original=\"https:\/\/www.garfield-blog.top\/wp-content\/uploads\/2025\/10\/struct-cJSON-scaled.jpg\" src=\"data:image\/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAAAXNSR0IArs4c6QAAAARnQU1BAACxjwv8YQUAAAAJcEhZcwAADsQAAA7EAZUrDhsAAAANSURBVBhXYzh8+PB\/AAffA0nNPuCLAAAAAElFTkSuQmCC\" alt=\"\" class=\"wp-image-250\"  sizes=\"auto, (max-width: 2560px) 100vw, 2560px\" \/><\/div><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\">2. \u5168\u5c40\u53d8\u91cf<\/h2>\n\n\n\n<p><code>char *ep;<\/code><\/p>\n\n\n\n<p>\u5728\u8bb0\u5f55\u51fa\u9519\u65f6\u662f\u5728\u54ea\u4e2a\u5b57\u7b26\uff0c\u4ee5\u9519\u8bef\u7801\u7684\u65b9\u5f0f\u8fd4\u56de\uff1b\u901a\u8fc7<code>cJSON_GetErrorPtr()<\/code>\u83b7\u53d6\u3002\u672c\u6587\u7684\u793a\u4f8b\u4ee3\u7801\u4e2d\u4e0d\u6d89\u53ca\u6b64\u63a5\u53e3\uff0c\u540e\u6587\u4e2d\u4e5f\u4e0d\u6df1\u5165\u8ba8\u8bba\u6b64\u673a\u5236\uff0c\u5728\u6b64\u63d0\u51fa\u4ec5\u7528\u4e8e\u5411\u8bfb\u8005\u8865\u5145<code>cJSON<\/code>\u6574\u4f53\u67b6\u6784\u8bbe\u8ba1<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">3. \u4ece\u5b57\u7b26\u4e32\u89e3\u6790json\u6d41\u7a0b<\/h2>\n\n\n\n<p><strong><code>cJSON<\/code>\u63d0\u4f9b\u7684\u4e00\u4e2a\u5173\u952e\u7684\u529f\u80fd\u4fbf\u662f\uff0c\u5bf9<code>json<\/code>\u6587\u672c\u7684\u89e3\u6790\u3002\u5bf9\u4e8e\u6811\u72b6\u7ed3\u6784\u7684<code>json<\/code>\u683c\u5f0f<code>cJSON<\/code>\u521b\u5efa\u4e86<code>struct cJSON<\/code>\u6570\u636e\u7ed3\u6784\uff0c\u5176\u4e2d\u4f7f\u7528\u4e86\u4f7f\u7528\u53cc\u5411\u94fe\u8868\u5b58\u50a8\u540c\u7ea7\u522b\u8282\u70b9\uff0c\u521b\u5efa<code>struct cJSON *child<\/code>\u5143\u7d20\uff0c\u5b58\u50a8\u4f4e\u7ea7\u522b\u8282\u70b9\u3002\u89e3\u6790\u6d41\u7a0b\u4e2d\u4f7f\u7528\u53cc\u5c42\u9012\u5f52\u7684\u8bbe\u8ba1\u4ece\u6839\u8282\u70b9\u5f00\u59cb\u7531\u5916\u81f3\u5185\u4e00\u5c42\u5c42\u89e3\u6790\uff0c\u5bf9\u5b57\u7b26\u4e32\u7684\u5904\u7406\u548c\u6574\u4f53\u67b6\u6784\u8bbe\u8ba1\u6709\u8bb8\u591a\u7cbe\u5999\u4f18\u96c5\u7684\u8bbe\u8ba1\uff0c\u5728\u9605\u8bfb\u6e90\u7801\u65f6\u53ef\u4ee5\u5b66\u4e60\u501f\u9274\u3002\u672c\u7ae0\u8282\u4ece\u5b57\u7b26\u4e32\u89e3\u6790\u6d41\u7a0b\u5207\u5165\uff0c\u5bf9<code>cJSON<\/code>\u4e2d\u7684\u540d\u79f0\u4ee5<code>Parse_<\/code>\u5f00\u5934\u7684\u4e00\u7ec4\u63a5\u53e3\u8fdb\u884c\u5256\u6790\uff0c\u5728\u6e90\u7801\u5b66\u4e60\u4e2d\u611f\u53d7<code>cJSON<\/code>\u4e2d\u8bbe\u8ba1\u7684\u5de7\u601d\u3002<\/strong><\/p>\n\n\n\n<p>\u53c2\u8003<code>cjson<\/code>\u6e90\u4ee3\u7801\u4e2d\u7ed9\u51fa<code>demo<\/code>\u7684\u6d41\u7a0b<\/p>\n\n\n\n<p>\u6838\u5fc3\u51fd\u6570\u4e3a<code>cJSON_Parse<\/code>\u3001<code>cJSON_Print<\/code>\u548c<code>cJSON_Delete<\/code><\/p>\n\n\n\n<p><strong>\u6838\u5fc3\u903b\u8f91\u5982\u4e0b<\/strong><\/p>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\">\n<p>\u7b14\u8005\u6ce8\uff1a\u4e0b\u6587\u4ee3\u7801\u5df2\u683c\u5f0f\u5316\u5904\u7406\uff0c\u5e76\u9002\u5f53\u7b80\u5316\u53ea\u4fdd\u7559\u6838\u5fc3\u903b\u8f91<\/p>\n<\/blockquote>\n\n\n\n<pre class=\"wp-block-code\"><code>void doit(char *text)\n{\n    char *out;\n    cJSON *json;\n\n    json = cJSON_Parse(text);\n    out = cJSON_Print(json);\n    cJSON_Delete(json);\n    printf(\"%s\\n\", out);\n    free(out);\n}\n\nint main (int argc, const char * argv&#91;])\n{\n    char text1&#91;] = \"{\\n\\\"name\\\": \\\"Jack (\\\\\\\"Bee\\\\\\\") Nimble\\\", \\n\\\"format\\\": {\\\"type\\\":       \\\"rect\\\", \\n\\\"width\\\":      1920, \\n\\\"height\\\":     1080, \\n\\\"interlace\\\":  false,\\\"frame rate\\\": 24\\n}\\n}\"; \n\n    doit(text1);\n    create_objects();\n\n    return 0;\n}<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">3.1 \u5173\u952e\u51fd\u6570\u63a5\u53e3\u53ca\u5b9e\u73b0\u903b\u8f91<\/h3>\n\n\n\n<h4 class=\"wp-block-heading\"><code>cJSON_New_Item()<\/code>\u51fd\u6570<\/h4>\n\n\n\n<p>\u5185\u90e8\u6784\u9020\u51fd\u6570\uff0c\u521b\u5efa\u4e00\u4e2a<code>cjson<\/code>\u8282\u70b9\uff0c\u5e76\u8fd4\u56de\u8282\u70b9\u5730\u5740<\/p>\n\n\n\n<p><strong>\u6838\u5fc3\u903b\u8f91\u5982\u4e0b<\/strong><\/p>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\">\n<p>\u7b14\u8005\u6ce8\uff1a\u4e0b\u6587\u4ee3\u7801\u5df2\u683c\u5f0f\u5316\u5904\u7406\uff0c\u5e76\u9002\u5f53\u7b80\u5316\u53ea\u4fdd\u7559\u6838\u5fc3\u903b\u8f91<\/p>\n<\/blockquote>\n\n\n\n<pre class=\"wp-block-code\"><code>cJSON *cJSON_New_Item(void)\n{\n    cJSON* node = (cJSON*)malloc(sizeof(cJSON));\n    return node;\n}<\/code><\/pre>\n\n\n\n<p><strong>\u51fd\u6570\u6838\u5fc3\u601d\u60f3<\/strong><\/p>\n\n\n\n<p>\u7533\u8bf7\u5185\u5b58\u8fd4\u56de\u5185\u5b58\u6307\u9488\uff0c\u521b\u5efa\u94fe\u8868\u6216\u5176\u4ed6\u6570\u636e\u7ed3\u6784\u8282\u70b9\u7684\u5e38\u89c4\u64cd\u4f5c\uff0c\u65e0\u9700\u591a\u8a00<\/p>\n\n\n\n<h4 class=\"wp-block-heading\"><code>cJSON_Parse<\/code>()\u51fd\u6570<\/h4>\n\n\n\n<p>\u521b\u5efa\u65b0\u7684<code>cJSON<\/code>\u8282\u70b9\uff0c\u8c03\u7528<code>parse_value()<\/code>\u586b\u5145\u8282\u70b9<\/p>\n\n\n\n<p><strong>\u6838\u5fc3\u903b\u8f91\u5982\u4e0b<\/strong><\/p>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\">\n<p>\u7b14\u8005\u6ce8\uff1a\u4e0b\u6587\u4ee3\u7801\u5df2\u683c\u5f0f\u5316\u5904\u7406\uff0c\u5e76\u9002\u5f53\u7b80\u5316\u53ea\u4fdd\u7559\u6838\u5fc3\u903b\u8f91<\/p>\n<\/blockquote>\n\n\n\n<pre class=\"wp-block-code\"><code>\/* Parse an object - create a new root, and populate. *\/\ncJSON *cJSON_ParseWithOpts(const char *value)\n{\n    cJSON *c = cJSON_New_Item();\n    parse_value(c, skip(value));\n\n    return c;\n}\n\n\/* Default options for cJSON_Parse *\/\ncJSON *cJSON_Parse(const char *value)\n{\n    return cJSON_ParseWithOpts(value);\n}<\/code><\/pre>\n\n\n\n<p><strong>\u51fd\u6570\u6838\u5fc3\u601d\u60f3<\/strong><\/p>\n\n\n\n<p>\u672c\u8d28\u662f\u5bf9<code>parse_value()<\/code>\u7684\u4e00\u5c42\u5c01\u88c5<\/p>\n\n\n\n<h4 class=\"wp-block-heading\"><code>parse_value()<\/code>\u51fd\u6570<\/h4>\n\n\n\n<p>\u6587\u672c\u89e3\u6790\u5904\u7406\u51fd\u6570\uff0c\u5bf9\u6587\u672c\u8fdb\u884c\u521d\u6b65\u5206\u7c7b\u3002\u5bf9\u4e0d\u540c\u7684\u60c5\u51b5\u4f7f\u7528\u4e0d\u540c\u7684\u5904\u7406\u6d41\u7a0b<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>json<\/code>\u683c\u5f0f\u4e2d\u7684<strong>\u5bf9\u8c61<\/strong>\u5bf9\u5e94<code>{<\/code>\u5f00\u5934<\/li>\n\n\n\n<li><code>json<\/code>\u683c\u5f0f\u4e2d\u7684<strong>\u6570\u7ec4<\/strong>\u5bf9\u5e94<code>[<\/code>\u5f00\u5934<\/li>\n\n\n\n<li><code>json<\/code>\u683c\u5f0f\u4e2d\u5b57\u7b26\u4e32\u4e0e\u6570\u503c\u88ab\u7edf\u79f0\u4e3a<strong>\u503c<\/strong>\u4f7f\u7528<code>\"\"<\/code>\u5305\u88f9<\/li>\n<\/ul>\n\n\n\n<p><strong>\u6838\u5fc3\u903b\u8f91\u5982\u4e0b<\/strong><\/p>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\">\n<p>\u7b14\u8005\u6ce8\uff1a\u4e0b\u6587\u4ee3\u7801\u5df2\u683c\u5f0f\u5316\u5904\u7406\uff0c\u5e76\u9002\u5f53\u7b80\u5316\u53ea\u4fdd\u7559\u6838\u5fc3\u903b\u8f91<\/p>\n<\/blockquote>\n\n\n\n<pre class=\"wp-block-code\"><code>static const char *parse_value(cJSON *item, const char *value)\n{\n    if (*value == '\\\"') {\n        return parse_string(item, value);\n    }\n\n    if (*value == '-' || (*value &gt;= '0' &amp;&amp; *value &lt;= '9')) {\n        return parse_number(item, value);\n    }\n\n    if (*value == '&#91;') {\n        return parse_array(item, value);\n    }\n\n    if (*value == '{') {\n        return parse_object(item, value);\n    }\n\n    return NULL;    \/* failure. *\/\n}<\/code><\/pre>\n\n\n\n<p><strong>\u51fd\u6570\u6838\u5fc3\u601d\u60f3<\/strong><\/p>\n\n\n\n<p>\u6839\u636e\u5f85\u89e3\u6790\u5b57\u7b26\u4e32\u7684\u524d\u51e0\u4f4d\u5b57\u7b26\u6765\u5224\u65ad\uff0c\u6b64\u6bb5\u6570\u636e\u5bf9\u4e8e<code>json<\/code>\u4e2d\u7684\u54ea\u79cd\u7c7b\u578b\u3002<code>cJSON<\/code>\u8fd8\u5bf9\u5b57\u7b26\u4e32\u4e3a<code>null<\/code>\u3001<code>true<\/code>\u3001<code>false<\/code>\u8fd9\u4e09\u79cd\u7279\u6b8a\u60c5\u51b5\u6709\u5904\u7406\uff0c\u672c\u6587\u4e2d\u5e76\u672a\u4f53\u73b0\u8fd9\u4e00\u70b9<\/p>\n\n\n\n<h4 class=\"wp-block-heading\"><code>parse_object()<\/code>\u51fd\u6570<\/h4>\n\n\n\n<p><code>object<\/code>\u89e3\u6790\u51fd\u6570\uff0c\u5c06\u4f20\u5165\u7684<code>value<\/code>\u5f53\u4f5c\u4e00\u4e2a\u975e\u5e38\u957f\u7684\u5b57\u7b26\u4e32\uff0c\u4e0d\u65ad\u7684\u5206\u5272\u5b57\u7b26\u4e32\u3002\u5c06\u89e3\u6790\u51fa\u6765\u7684\u6570\u636e\u7528\u6765\u586b\u5145<code>item<\/code>\uff0c\u540c\u7ea7\u522b\u5143\u7d20\u586b\u5145\u5728\u53cc\u5411\u94fe\u8868\u4e2d\uff0c\u4f4e\u7ea7\u522b\u5143\u7d20\u586b\u5145\u5728<code>child<\/code>\u8282\u70b9\u4e2d\u3002<\/p>\n\n\n\n<p><strong>\u6838\u5fc3\u903b\u8f91\u5982\u4e0b<\/strong><\/p>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\">\n<p>\u7b14\u8005\u6ce8\uff1a\u4e0b\u6587\u4ee3\u7801\u5df2\u683c\u5f0f\u5316\u5904\u7406\uff0c\u5e76\u9002\u5f53\u7b80\u5316\u53ea\u4fdd\u7559\u6838\u5fc3\u903b\u8f91<\/p>\n<\/blockquote>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u5c06<code>item<\/code>\u4f5c\u4e3a\u6839\u8282\u70b9\uff0c\u521b\u5efa\u4e00\u4e2a\u6876\u7ed3\u6784\u5b58\u50a8\u89e3\u6790\u51fa\u7684<code>json<\/code>\u5185\u5bb9<\/li>\n\n\n\n<li>\u51fd\u6570\u4f7f\u7528\u53cc\u5c42\u9012\u5f52\u8bbe\u8ba1<\/li>\n<\/ul>\n\n\n\n<pre class=\"wp-block-code\"><code>\/* Build an object from the text. *\/\nstatic const char *parse_object(cJSON *item, const char *value)\n{\n    \/* child\u5728\u51fd\u6570\u524d\u534a\u6bb5\u4e3a\u65b0\u589e\u8282\u70b9\uff0c\u51fd\u6570\u540e\u534a\u6bb5\u4e3a\u94fe\u8868\u54e8\u5175\u4f4d\u6307\u9488 *\/\n    cJSON *child;\n    child = cJSON_New_Item();\n    item-&gt;child = child;\n    item-&gt;type = cJSON_Object;\n\n    \/* \u53d6\u51fa\u524d\u534a\u90e8\u5206\u5b58\u5165string,\u53ef\u80fd\u662f\u5bf9\u8c61\u6216\u6570\u7ec4\u7684\u540d\u79f0\uff0c\u6216\u662f\u952e\u503c\u5bf9\u7684\u524d\u534a\u90e8\u5206 *\/\n    value = skip(parse_string(child, skip(value + 1)));\n    child-&gt;string = child-&gt;valuestring;\n    child-&gt;valuestring = NULL;\n    if (*value != ':') {\n        return NULL;\n    }\n\n    \/*\n     * \u6b64\u5904\u53ef\u80fd\u9020\u6210\u9012\u5f52\u8c03\u7528\n     * \u9012\u5f52\u7684\u51fa\u53e3: value\u5f00\u5934\u5230\u7b2c\u4e00\u4e2a\u201c,\u201d\u7684\u90e8\u5206\u662f\u666e\u901a\u952e\u503c\u5bf9,\u4e0d\u5305\u542bobject\u6216array\u6df1\u5c42\u5d4c\u5957\n     *\/\n    value = skip(parse_value(child, skip(value + 1)));\n\n    while (*value == ',') {\n        cJSON *new_item;\n        new_item = cJSON_New_Item();\n\n        \/*\n         * \u521b\u5efa\u53cc\u5411\u4e0d\u5faa\u73af\u94fe\u8868\uff0c\u4fdd\u5b58\u4e4b\u524d\u7684\u89e3\u6790\u7ed3\u679c\u3002\u94fe\u8868\u4e2d\u4ec5\u4fdd\u5b58\u540c\u7ea7\u5143\u7d20\n         * \u6b64\u5904child\u89c6\u4e3a\u94fe\u8868\u7684\u54e8\u5175\u4f4d\u6307\u9488\u800c\u4e0d\u662f\u65b0\u589e\u8282\u70b9\u6307\u9488\n         *\/\n        child-&gt;next = new_item;\n        new_item-&gt;prev = child;\n        child = new_item;\n\n        value = skip(parse_string(child, skip(value + 1)));\n        child-&gt;string = child-&gt;valuestring;\n        child-&gt;valuestring = NULL;\n        if (*value != ':') {\n            return NULL;\n        }\n\n        value = skip(parse_value(child, skip(value + 1)));\n    }\n\n    if (*value == '}') {\n        return value + 1;\n    }\n\n    return NULL;\n}<\/code><\/pre>\n\n\n\n<p><strong>\u51fd\u6570\u6838\u5fc3\u601d\u60f3<\/strong><\/p>\n\n\n\n<p>\u6b64\u51fd\u6570\u6700\u4e3b\u8981\u7684\u4f5c\u7528\u4fbf\u662f\u586b\u5145<code>cJSON<\/code>\u7ed3\u6784\u4f53\uff0c<code>struct cJSON<\/code>\u662f<code>cJSON<\/code>\u4e2d\u6700\u4e3a\u6838\u5fc3\u548c\u57fa\u7840\u7684\u6570\u636e\u7ed3\u6784\uff0c\u5176\u6838\u5fc3\u7ed3\u6784\u662f\u4e24\u4e2a\u94fe\u8868\u8282\u70b9\u548c\u4e00\u4e2a<code>child<\/code>\u8282\u70b9\u3002\u5728\u89e3\u6790<code>json<\/code>\u6587\u4ef6\u65f6\u5c06\u5176\u4e2d\u6210\u5458\u89c6\u4e3a\u5177\u6709\u4e0d\u540c\u7684\u7b49\u7ea7\uff1b<strong>\u76f8\u540c\u7f29\u8fdb\u7684\u6210\u5458\u4e3a\u540c\u4e00\u7b49\u7ea7\uff0c\u5c06\u540c\u4e00\u7b49\u7ea7\u7684\u5143\u7d20\u4f7f\u7528\u53cc\u5411\u94fe\u8868\u8fde\u63a5\uff0c\u4f4e\u7b49\u7ea7\u5143\u7d20\u5219\u5b58\u653e\u5728<code>child<\/code>\u8282\u70b9\u4e2d<\/strong>\uff1b\u586b\u5145\u7684<code>struct cJSON<\/code>\u5b9e\u73b0\u7684\u65b9\u5f0f\uff0c\u4f7f\u7528\u4e86\u53cc\u5c42\u9012\u5f52\u7684\u65b9\u5f0f\u53bb\u5b9e\u73b0<\/p>\n\n\n\n<p><strong>\u53cc\u5c42\u9012\u5f52\u7684\u51fd\u6570\u8bbe\u8ba1<\/strong><\/p>\n\n\n\n<p>\u5bf9\u4e8e<code>object<\/code>\u6765\u8bf4\u5176\u81f3\u5c11\u5305\u542b\u4e24\u5c42\u7b49\u7ea7\uff0c\u5373<code>object<\/code>\u540d\u79f0\u548c\u6210\u5458\uff1b\u5bf9\u4e8e\u5c06\u540d\u79f0\u4f5c\u4e3a\u5b57\u7b26\u4e32\u5904\u7406\u3002\u9996\u5148\u63d0\u53d6<code>object<\/code>\u540d\u79f0\u90e8\u5206\uff0c\u5bf9\u4e8e\u540e\u534a\u6bb5\u6210\u5458\u7684\u5904\u7406\u5219\u662f\u8c03\u7528<code>parse_value()<\/code>\u53bb\u5904\u7406\uff0c\u6b64\u65f6\u5982\u679c\u5185\u90e8\u6210\u5458\u662f<code>object<\/code>\u5219\u4f1a\u89e6\u53d1<strong>\u7b2c\u4e00\u5c42\u9012\u5f52<\/strong>\uff0c\u6b64\u65f6<strong><code>child<\/code><\/strong>\u8282\u70b9\u4fbf\u4f5c\u4e3a\u4e00\u4e2a\u65b0\u7684\u6839\u8282\u70b9\uff0c<strong>\u76f4\u5230\u9047\u5230\u666e\u901a\u7684\u952e\u503c\u5bf9\u9012\u5f52\u7ed3\u675f<\/strong>\uff0c\u8fd9\u4fbf\u662f\u51fd\u6570\u524d\u534a\u6bb5\u7684\u5904\u7406<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><div class='fancybox-wrapper lazyload-container-unload' data-fancybox='post-images' href='https:\/\/www.garfield-blog.top\/wp-content\/uploads\/2025\/10\/parse_value\u6b65\u9aa41-scaled.jpg'><img class=\"lazyload lazyload-style-1\" src=\"data:image\/svg+xml;base64,PCEtLUFyZ29uTG9hZGluZy0tPgo8c3ZnIHdpZHRoPSIxIiBoZWlnaHQ9IjEiIHhtbG5zPSJodHRwOi8vd3d3LnczLm9yZy8yMDAwL3N2ZyIgc3Ryb2tlPSIjZmZmZmZmMDAiPjxnPjwvZz4KPC9zdmc+\"  loading=\"lazy\" decoding=\"async\" width=\"2560\" height=\"967\" data-original=\"https:\/\/www.garfield-blog.top\/wp-content\/uploads\/2025\/10\/parse_value\u6b65\u9aa41-scaled.jpg\" src=\"data:image\/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAAAXNSR0IArs4c6QAAAARnQU1BAACxjwv8YQUAAAAJcEhZcwAADsQAAA7EAZUrDhsAAAANSURBVBhXYzh8+PB\/AAffA0nNPuCLAAAAAElFTkSuQmCC\" alt=\"\" class=\"wp-image-247\"  sizes=\"auto, (max-width: 2560px) 100vw, 2560px\" \/><\/div><\/figure>\n\n\n\n<p>\u5f53\u4e00\u5c42\u9012\u5f52\u7ed3\u675f\u540e\uff0c\u51fd\u6570\u540e\u534a\u6bb5\u521b\u5efa\u4e00\u4e2a\u5faa\u73af\u904d\u5386<code>json<\/code>\u6587\u4ef6\u7684\u6bcf\u4e00\u884c\uff0c\u5faa\u73af\u4e2d\u521b\u5efa\u4e00\u4e2a\u65b0\u8282\u70b9\u6dfb\u52a0\u5728\u53cc\u5411\u94fe\u8868\u540e\uff0c\u5e76\u63d0\u53d6\u4e0b\u4e2a\u5143\u7d20\u7684\u540d\u79f0\u586b\u5145<code>child-&gt;valuestring<\/code>\u3002\u63a5\u7740\u7ee7\u7eed\u8c03\u7528<code>parse_value()<\/code>\u5982\u679c\u6b64\u65f6\u89e3\u6790\u7684\u5143\u7d20\u662f<code>object<\/code>\u4fbf\u662f<strong>\u7b2c\u4e8c\u5c42\u9012\u5f52<\/strong>\uff0c\u9012\u5f52\u51fa\u53e3\u4e0e\u7b2c\u4e00\u5c42\u76f8\u540c\u3002\u6574\u4e2a\u5faa\u73af\u4e2d\u4f1a\u5c06\u76f8\u540c\u7ea7\u522b\u6210\u5458\u6dfb\u52a0\u81f3\u53cc\u5411\u94fe\u8868\uff0c\u4f4e\u7ea7\u522b\u5143\u7d20\u6dfb\u52a0\u81f3<code>child<\/code>\u8282\u70b9<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><div class='fancybox-wrapper lazyload-container-unload' data-fancybox='post-images' href='https:\/\/www.garfield-blog.top\/wp-content\/uploads\/2025\/10\/parse_value\u6b65\u9aa42-scaled.jpg'><img class=\"lazyload lazyload-style-1\" src=\"data:image\/svg+xml;base64,PCEtLUFyZ29uTG9hZGluZy0tPgo8c3ZnIHdpZHRoPSIxIiBoZWlnaHQ9IjEiIHhtbG5zPSJodHRwOi8vd3d3LnczLm9yZy8yMDAwL3N2ZyIgc3Ryb2tlPSIjZmZmZmZmMDAiPjxnPjwvZz4KPC9zdmc+\"  loading=\"lazy\" decoding=\"async\" width=\"2560\" height=\"914\" data-original=\"https:\/\/www.garfield-blog.top\/wp-content\/uploads\/2025\/10\/parse_value\u6b65\u9aa42-scaled.jpg\" src=\"data:image\/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAAAXNSR0IArs4c6QAAAARnQU1BAACxjwv8YQUAAAAJcEhZcwAADsQAAA7EAZUrDhsAAAANSURBVBhXYzh8+PB\/AAffA0nNPuCLAAAAAElFTkSuQmCC\" alt=\"\" class=\"wp-image-248\"  sizes=\"auto, (max-width: 2560px) 100vw, 2560px\" \/><\/div><\/figure>\n\n\n\n<p>\u4e8c\u5c42\u9012\u5f52\u7ed3\u675f\u540e\uff0c\u51fd\u6570\u8c03\u7528\u6808\u8fd4\u56de\u3002\u7ee7\u7eed\u5904\u7406\u4e00\u5c42\u9012\u5f52\u672a\u5904\u7406\u7684\u90e8\u5206<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><div class='fancybox-wrapper lazyload-container-unload' data-fancybox='post-images' href='https:\/\/www.garfield-blog.top\/wp-content\/uploads\/2025\/10\/parse_value\u6b65\u9aa43-scaled.jpg'><img class=\"lazyload lazyload-style-1\" src=\"data:image\/svg+xml;base64,PCEtLUFyZ29uTG9hZGluZy0tPgo8c3ZnIHdpZHRoPSIxIiBoZWlnaHQ9IjEiIHhtbG5zPSJodHRwOi8vd3d3LnczLm9yZy8yMDAwL3N2ZyIgc3Ryb2tlPSIjZmZmZmZmMDAiPjxnPjwvZz4KPC9zdmc+\"  loading=\"lazy\" decoding=\"async\" width=\"2560\" height=\"914\" data-original=\"https:\/\/www.garfield-blog.top\/wp-content\/uploads\/2025\/10\/parse_value\u6b65\u9aa43-scaled.jpg\" src=\"data:image\/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAAAXNSR0IArs4c6QAAAARnQU1BAACxjwv8YQUAAAAJcEhZcwAADsQAAA7EAZUrDhsAAAANSURBVBhXYzh8+PB\/AAffA0nNPuCLAAAAAElFTkSuQmCC\" alt=\"\" class=\"wp-image-249\"  sizes=\"auto, (max-width: 2560px) 100vw, 2560px\" \/><\/div><\/figure>\n\n\n\n<p>\u5b9e\u9645\u4e0a\uff0c\u7edd\u5927\u90e8\u5206<code>json<\/code>\u90fd\u662f\u4ee5<code>{<\/code>\u5f00\u5934\uff0c\u56e0\u6b64\u6bcf\u6b21\u89e3\u6790\u65f6\u7b2c\u4e00\u4e2a\u8fdb\u5165\u7684\u51fd\u6570\u90fd\u662f<code>parse_object()<\/code>\u56e0\u6b64\u5bf9\u8fd9\u4e2a\u51fd\u6570\u7684\u7406\u89e3\uff0c\u5bf9\u6574\u4e2a<code>cJSON<\/code>\u89e3\u6790\u6d41\u7a0b\u7684\u7406\u89e3\u81f3\u5173\u91cd\u8981<\/p>\n\n\n\n<h4 class=\"wp-block-heading\"><code>parse_array()<\/code>\u51fd\u6570<\/h4>\n\n\n\n<p>\u5176\u6838\u5fc3\u903b\u8f91\u4e0e<code>parse_object()<\/code>\u51fd\u6570\u975e\u5e38\u76f8\u4f3c\uff0c\u6700\u5927\u7684\u533a\u522b\u5728\u4e8e\u5728\u51fd\u6570\u524d\u534a\u6bb5\u4e0d\u9700\u8981\u53d6\u51fa<code>:<\/code>\u7b26\u53f7\u7684\u524d\u534a\u90e8\u5206<\/p>\n\n\n\n<p><strong>\u6838\u5fc3\u903b\u8f91\u5982\u4e0b<\/strong><\/p>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\">\n<p>\u7b14\u8005\u6ce8\uff1a\u4e0b\u6587\u4ee3\u7801\u5df2\u683c\u5f0f\u5316\u5904\u7406\uff0c\u5e76\u9002\u5f53\u7b80\u5316\u53ea\u4fdd\u7559\u6838\u5fc3\u903b\u8f91<\/p>\n<\/blockquote>\n\n\n\n<pre class=\"wp-block-code\"><code>\/* Build an array from input text. *\/\nstatic const char *parse_array(cJSON *item, const char *value)\n{\n    \/* child\u5728\u51fd\u6570\u524d\u534a\u6bb5\u4e3a\u65b0\u589e\u8282\u70b9\uff0c\u51fd\u6570\u540e\u534a\u6bb5\u4e3a\u94fe\u8868\u54e8\u5175\u4f4d\u6307\u9488 *\/\n    cJSON *child;\n\n    child = cJSON_New_Item();\n    item-&gt;child = child;\n    item-&gt;type = cJSON_Array;\n\n    \/*\n     * \u6b64\u5904\u53ef\u80fd\u9020\u6210\u9012\u5f52\u8c03\u7528\n     * \u9012\u5f52\u7684\u51fa\u53e3: value\u5f00\u5934\u5230\u7b2c\u4e00\u4e2a\u201c,\u201d\u7684\u90e8\u5206\u662f\u666e\u901a\u952e\u503c\u5bf9,\u4e0d\u5305\u542bobject\u6216array\u6df1\u5c42\u5d4c\u5957\n     *\/\n    value = skip(parse_value(child, skip(value + 1)));\n\n    while (*value == ',') {\n        cJSON *new_item;\n        new_item = cJSON_New_Item()\n\n        \/* \u6b64\u5904child\u89c6\u4e3a\u94fe\u8868\u7684\u54e8\u5175\u4f4d\u6307\u9488\u800c\u4e0d\u662f\u65b0\u589e\u8282\u70b9\u6307\u9488 *\/\n        child-&gt;next = new_item;\n        new_item-&gt;prev = child;\n        child = new_item;\n\n        value = skip(parse_value(child, skip(value + 1)));\n    }\n\n    if (*value == ']') {\n        return value + 1;   \n    }\n\n    return NULL;\n}<\/code><\/pre>\n\n\n\n<p><strong>\u51fd\u6570\u6838\u5fc3\u601d\u60f3<\/strong><\/p>\n\n\n\n<p>\u6b64\u51fd\u6570\u6574\u4f53\u7ed3\u6784\u8bbe\u8ba1\u4e0e<code>parse_object()<\/code>\u9ad8\u5ea6\u76f8\u4f3c\uff0c\u540c\u6837\u91c7\u53d6\u53cc\u5c42\u9012\u5f52\u7684\u8bbe\u8ba1\u5728\u6b64\u4e0d\u505a\u8d58\u8ff0\u3002\u4f46\u4e0e<code>parse_object()<\/code>\u53c8\u5b58\u5728\u4e00\u5904\u975e\u5e38\u5927\u7684\u4e0d\u540c\uff0c\u5373\u4e0d\u5bf9<code>item-&gt;string<\/code>\u8fdb\u884c\u8d4b\u503c<\/p>\n\n\n\n<p><strong>\u6d88\u5931\u7684\u540d\u5b57<\/strong><\/p>\n\n\n\n<p>\u7531\u4e8e<code>json<\/code>\u4e2d\u7684<code>array<\/code>\u7ed3\u6784\u662f\u5d4c\u5957\u5728<code>object<\/code>\u4e2d\uff0c<code>parse_array()<\/code>\u51fd\u6570\u53ea\u4f1a\u88ab<code>parse_object()<\/code>\u51fd\u6570\u8c03\u7528\u3002\u6240\u4ee5\u5728\u89e3\u6790<code>array<\/code>\u65f6\u540d\u79f0\u7684\u90e8\u5206\u4f1a\u5728\u8fdb\u5165<code>parse_array()<\/code>\u51fd\u6570\u524d\u5c31\u4f1a\u5728<code>parse_object()<\/code>\u4e2d\u89e3\u6790\u5904\u7406\uff0c<code>array<\/code>\u5185\u90e8\u7684\u5143\u7d20\u662f\u4ee5<code>,<\/code>\u5206\u9694\u7684\uff0c\u6240\u6709\u7684\u952e\u503c\u5bf9\u90fd\u53ea\u4f1a\u88ab\u5d4c\u5957\u5728\u53e6\u4e00\u4e2a<code>object<\/code>\u4e2d<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">3.2 \u5b57\u7b26\u4e32\u5904\u7406\u63a5\u53e3<\/h3>\n\n\n\n<p><code>cjson<\/code>\u4e2d\u8bbe\u8ba1\u5927\u91cf\u7684\u5b57\u7b26\u4e32\u5904\u7406\u903b\u8f91\uff0c\u5176\u4e2d\u6709\u8bb8\u591a\u8bbe\u8ba1\u53ef\u4ee5\u5b66\u4e60\u501f\u9274\uff0c\u5728\u6b64\u5217\u51fa\uff1b<\/p>\n\n\n\n<p>\u672c\u6587\u5217\u51fa\u7684\u51e0\u4e2a\u51fd\u6570\u57fa\u672c\u4e0a<strong>\u53ef\u76f4\u63a5\u590d\u5236\u7c98\u8d34\u5728\u5176\u4ed6\u9879\u76ee\u4e2d\u4f7f\u7528<\/strong>\uff0c\u662f<strong>\u975e\u5e38\u4f18\u8d28\u7684\u8f6e\u5b50\uff0c\u6545\u8d34\u51fa\u51fd\u6570\u6e90\u4ee3\u7801<\/strong><\/p>\n\n\n\n<h4 class=\"wp-block-heading\"><code>skip<\/code>\u51fd\u6570<\/h4>\n\n\n\n<p>\u8df3\u8fc7\u7a7a\u683c\u548c\u65e0\u7528\u5b57\u7b26<\/p>\n\n\n\n<p><strong>\u6e90\u4ee3\u7801\u4e2d\u51fd\u6570\u5b9e\u73b0\u5982\u4e0b<\/strong><\/p>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\">\n<p>\u7b14\u8005\u6ce8\uff1a\u4ee3\u7801\u5df2\u505a\u683c\u5f0f\u5316\u5904\u7406<\/p>\n<\/blockquote>\n\n\n\n<pre class=\"wp-block-code\"><code>static const char *skip(const char *in) \n{\n    while (in &amp;&amp; *in &amp;&amp; (unsigned char)*in &lt;= 32) {\n        in++;\n    }\n\n    return in;\n}<\/code><\/pre>\n\n\n\n<p><strong>\u51fd\u6570\u6838\u5fc3\u601d\u60f3<\/strong><\/p>\n\n\n\n<p>\u51fd\u6570\u901a\u8fc7<code>while<\/code>\u5faa\u73af\u904d\u5386\u6574\u4e2a\u5b57\u7b26\u4e32\uff0c\u9996\u5148\u5224\u65ad<strong>\u6307\u9488\u4e0d\u4e3a\u7a7a<\/strong>\uff0c\u6307\u9488\u6307\u5411\u7684<strong>\u503c\u4e0d\u4e3a\u7a7a<\/strong><\/p>\n\n\n\n<p>\u4e0d\u65ad\u5411\u540e\u504f\u79fb\uff0c\u76f4\u5230\u6307\u5411\u7684\u5b57\u7b26\u7684<code>ACSII<\/code>\u7801\u5927\u4e8e<code>32<\/code>;\u51fd\u6570\u4f5c\u8005\u975e\u5e38\u5de7\u5999\u7684\u5229\u7528\u4e86<code>ASCII<\/code>\u7801\u8868\u7684\u6392\u5217\u7684\u89c4\u5219\uff0c\u5c06\u524d<code>32<\/code>\u4f4d\u5728\u5b57\u7b26\u4e32\u89e3\u6790\u65f6\u65e0\u6cd5\u7528\u5230\u7684\u5b57\u7b26\u820d\u5f03\uff0c\u53ea\u4fdd\u7559\u4e86\u6709\u6548\u5b57\u7b26<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><div class='fancybox-wrapper lazyload-container-unload' data-fancybox='post-images' href='https:\/\/www.garfield-blog.top\/wp-content\/uploads\/2025\/10\/ACSII\u7801\u8868.jpg'><img class=\"lazyload lazyload-style-1\" src=\"data:image\/svg+xml;base64,PCEtLUFyZ29uTG9hZGluZy0tPgo8c3ZnIHdpZHRoPSIxIiBoZWlnaHQ9IjEiIHhtbG5zPSJodHRwOi8vd3d3LnczLm9yZy8yMDAwL3N2ZyIgc3Ryb2tlPSIjZmZmZmZmMDAiPjxnPjwvZz4KPC9zdmc+\"  loading=\"lazy\" decoding=\"async\" width=\"1080\" height=\"652\" data-original=\"https:\/\/www.garfield-blog.top\/wp-content\/uploads\/2025\/10\/ACSII\u7801\u8868.jpg\" src=\"data:image\/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAAAXNSR0IArs4c6QAAAARnQU1BAACxjwv8YQUAAAAJcEhZcwAADsQAAA7EAZUrDhsAAAANSURBVBhXYzh8+PB\/AAffA0nNPuCLAAAAAElFTkSuQmCC\" alt=\"\" class=\"wp-image-246\"  sizes=\"auto, (max-width: 1080px) 100vw, 1080px\" \/><\/div><\/figure>\n\n\n\n<h4 class=\"wp-block-heading\"><code>parse_number<\/code>\u51fd\u6570<\/h4>\n\n\n\n<p>\u5c06\u5b57\u7b26\u4e32\u89e3\u6790\u4e3a\u4e00\u4e2a\u6570\u5b57\uff0c\u540c\u65f6\u652f\u6301\u590d\u6570\uff0c\u6d6e\u70b9\u6570\uff0c\u53ef\u89e3\u6790\u79d1\u5b66\u8bb0\u6570\u6cd5\u7684\u7279\u6027\u3002<\/p>\n\n\n\n<p><strong>\u6e90\u4ee3\u7801\u4e2d\u51fd\u6570\u5b9e\u73b0\u5982\u4e0b<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\/* Parse the input text to generate a number, and populate the result into item. *\/\nstatic const char *parse_number(cJSON *item,const char *num)\n{\n    double n=0,sign=1,scale=0;int subscale=0,signsubscale=1;\n\n    if (*num=='-') sign=-1,num++;   \/* Has sign? *\/\n    if (*num=='0') num++;           \/* is zero *\/\n    if (*num&gt;='1' &amp;&amp; *num&lt;='9') do  n=(n*10.0)+(*num++ -'0');   while (*num&gt;='0' &amp;&amp; *num&lt;='9'); \/* Number? *\/\n    if (*num=='.' &amp;&amp; num&#91;1]&gt;='0' &amp;&amp; num&#91;1]&lt;='9') {num++;        do  n=(n*10.0)+(*num++ -'0'),scale--; while (*num&gt;='0' &amp;&amp; *num&lt;='9');}  \/* Fractional part? *\/\n    if (*num=='e' || *num=='E')     \/* Exponent? *\/\n    {   num++;if (*num=='+') num++; else if (*num=='-') signsubscale=-1,num++;      \/* With sign? *\/\n        while (*num&gt;='0' &amp;&amp; *num&lt;='9') subscale=(subscale*10)+(*num++ - '0');   \/* Number? *\/\n    }\n\n    n=sign*n*pow(10.0,(scale+subscale*signsubscale));   \/* number = +\/- number.fraction * 10^+\/- exponent *\/\n\n    item-&gt;valuedouble=n;\n    item-&gt;valueint=(int)n;\n    item-&gt;type=cJSON_Number;\n    return num;\n}<\/code><\/pre>\n\n\n\n<p><strong>\u51fd\u6570\u6838\u5fc3\u903b\u8f91<\/strong><\/p>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\">\n<p>\u7b14\u8005\u6ce8\uff1a\u4e0b\u6587\u4ee3\u7801\u5df2\u683c\u5f0f\u5316\u5904\u7406\uff0c\u5e76\u9002\u5f53\u7b80\u5316\u53ea\u4fdd\u7559\u6838\u5fc3\u903b\u8f91<\/p>\n<\/blockquote>\n\n\n\n<p>\u9996\u5148\u7533\u8bf7\u6570\u4e2a\u53d8\u91cf\u5b58\u50a8\u4e0d\u540c\u7684\u7279\u6027\u503c<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>static const char *parse_number(cJSON *item, const char *num)\n{\n    \/*\n     * n\uff1a\u5b58\u50a8\u6570\u5b57\n     * sign\uff1a\u5b58\u50a8\u6b63\u8d1f\u53f7\n     * scale\uff1a\u5b58\u50a8\u5c0f\u6570\u70b9\u540e\u7684\u4f4d\u6570\n     * subscale\uff1a\u5b58\u50a8\u79d1\u5b66\u8ba1\u6570\u6cd5\u90e8\u5206\u7684\u503c\n     * signsubscale\uff1a\u5b58\u50a8\u79d1\u5b66\u8ba1\u6570\u6cd5\u7684\u6b63\u8d1f\u53f7\n     *\/\n    double n, sign, scale;\n    n = 0;\n    sign = 1;\n    scale = 0;\n\n    int subscale, signsubscale;\n    subscale = 0;\n    signsubscale = 1;\n    ...\n}<\/code><\/pre>\n\n\n\n<p>\u4f9d\u6b21\u904d\u5386\u6574\u4e2a\u5b57\u7b26\u4e32\uff0c\u5c06\u4e0d\u540c\u90e8\u5206\u7684\u503c\u5b58\u50a8\u6700\u5f00\u59cb\u4e0d\u540c\u7684\u53d8\u91cf\u4e2d<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>static const char *parse_number(cJSON *item, const char *num)\n{\n    ...\n    \/* \u53d6\u51fa\u7b26\u53f7\u4f4d\uff0c\u4f7f\u7528sign\u8bb0\u5f55 *\/\n    if (*num == '-') {\n        sign =- 1, num++;\n    }\n\n    \/* \u8df3\u8fc7\u591a\u4f59\u76840 *\/\n    if (*num == '0') {\n        num++;\n    }\n\n    \/* \u5c06\u5b57\u7b26\u4e32\u4e2d\u7684\u8fde\u7eed\u6570\u5b57\u8f6c\u6362\u4e3a\u6574\u5f62 *\/\n    if (*num &gt;= '1' &amp;&amp; *num &lt;= '9') {\n        do {\n            \/* *num++ -'0';\u7b49\u4ef7\u4e8e*num-'0';num++; *\/\n            n = (n * 10.0) + (*num++ -'0');\n        } while (*num &gt;= '0' &amp;&amp; *num &lt;= '9');\n    }\n\n    \/*\n     * \u5c06\u5c0f\u6570\u70b9\u540e\u7684\u8fde\u7eed\u6570\u5b57\u8f6c\u5316\u4e3a\u6574\u5f62\n     * \u4e0e\u4e0a\u8ff0\u6d41\u7a0b\u533a\u522b\u5728\u4e8e\u4f7f\u7528scale\u53d8\u91cf\u8bb0\u5f55\u5c0f\u6570\u70b9\u540e\u6570\u5b57\u4f4d\u6570\n     *\/\n    if (*num == '.' &amp;&amp; num&#91;1] &gt;= '0' &amp;&amp; num&#91;1] &lt;= '9') {\n        num++;\n        do {\n            \/* *num++ -'0';\u7b49\u4ef7\u4e8e*num-'0';num++;*\/\n            n = (n * 10.0) + (*num++ -'0'), scale--;\n        } while (*num &gt;= '0' &amp;&amp; *num &lt;= '9');\n    }\n\n     \/* \u9996\u5148\u8bb0\u5f55\u79d1\u5b66\u8ba1\u6570\u6cd5\u7684\u7b26\u53f7 *\/\n    if (*num == 'e' || *num == 'E') {\n        num++;\n        if (*num == '+') {\n            num++;\n        } else if (*num == '-') {\n            signsubscale = -1, num++;\n        }\n\n        \/*\n         * \u5c06\u5b57\u7b26\u4e32\u4e2d\u7684\u8fde\u7eed\u6570\u5b57\u8f6c\u5316\u4e3a\u6574\u5f62\n         * \u6b64\u5904\u4e0d\u4f7f\u7528do while \u53ef\u80fd\u662f\u8003\u8651\u8f93\u5165\u975e\u6cd5\u5b57\u7b26\u7684\u60c5\u51b5\n         *\/\n        while (*num &gt;= '0' &amp;&amp; *num &lt;= '9') {\n            \/* *num++ -'0';\u7b49\u4ef7\u4e8e*num-'0';num++;*\/\n            subscale = (subscale * 10) + (*num++ - '0');\n        }\n    }\n    ...\n}<\/code><\/pre>\n\n\n\n<p>\u5c06\u6b64\u524d\u8bb0\u5f55\u7684\u4e0d\u540c\u4f4d\u7f6e\u7684\u503c\u901a\u8fc7\u8fd0\u7b97\u8f6c\u5316\u6210\u6d6e\u70b9\u6570<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>static const char *parse_number(cJSON *item, const char *num)\n{\n    ...\n    \/*\n     * \u5e26\u7b26\u53f7\u7684n\uff1asign * n\n     * \u79d1\u5b66\u8ba1\u6570\u6cd5\u7684\u7cfb\u6570\uff1apow(10.0, (scale + subscale * signsubscale));\n     * \u6b64\u5904\u8ba1\u7b97\u7684\u662f\u79d1\u5b66\u8ba1\u6570\u6cd5\u6240\u4ee5\u5e95\u6570\u662f10\n     *\/\n    n = sign * n * pow(10.0, (scale + subscale * signsubscale));\n\n    item-&gt;valuedouble = n;\n    item-&gt;valueint = (int)n;\n    item-&gt;type = cJSON_Number;\n\n    return num;\n}<\/code><\/pre>\n\n\n\n<p><strong>\u51fd\u6570\u6838\u5fc3\u601d\u60f3<\/strong><\/p>\n\n\n\n<p>\u53ef\u4f7f\u7528\u5b57\u7b26\u4e32\u8868\u793a\u4e00\u4e2a\u6570\u5b57\u4f8b\u5982<code>\"-1.23e10\"<\/code>\u8868\u793a\u8d1f\u4e00\u767e\u4e8c\u5341\u4e09<\/p>\n\n\n\n<p>\u5176\u4e2d\u4ece\u5de6\u5230\u53f3\u4f9d\u6b21\u4e3a<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>\"-\"<\/code>\u7b26\u53f7\u4f4d<\/li>\n\n\n\n<li><code>\"1\"<\/code>\u6570\u5b57\u6574\u6570\u4f4d<\/li>\n\n\n\n<li><code>\".23\"<\/code>\u6574\u6570\u540e\u7684\u5c0f\u6570<\/li>\n\n\n\n<li><code>\"e10\"<\/code>\u79d1\u5b66\u8ba1\u6570\u6cd5<\/li>\n<\/ul>\n\n\n\n<p><code>parse_number()<\/code>\u7684\u6838\u5fc3\u601d\u60f3\u5728\u4e8e\u5c06\u5b57\u7b26\u4e32\u7684\u6bcf\u4e00\u90e8\u5206\u53d6\u51fa\uff0c\u6700\u540e\u901a\u8fc7\u8ba1\u7b97\u83b7\u5f97\u5b57\u7b26\u4e32\u4ee3\u8868\u7684\u6570\u5b57<\/p>\n\n\n\n<h4 class=\"wp-block-heading\"><code>parse_string<\/code>\u51fd\u6570<\/h4>\n\n\n\n<p>\u6b64\u51fd\u6570\u7528\u4e8e\u5904\u7406\u5f62\u5f0f\u4e3a<code>\"XXXXXX\"<\/code>\u7684\u5b57\u7b26\u4e32\u3002\u51fd\u6570\u4f1a\u5904\u7406\u5b57\u7b26\u4e32\u4e2d\u7684\u8f6c\u4e49\u5b57\u7b26\uff0c\u5e76\u5c06<code>Unicode<\/code>\u8f6c\u4e49\u5e8f\u5217\u8f6c\u6362\u4e3a\u5bf9\u5e94\u7684<code>UTF-8<\/code>\u5b57\u7b26<\/p>\n\n\n\n<p>\u8be5\u51fd\u6570\u7684\u4e3b\u8981\u529f\u80fd\u5305\u62ec\uff1a<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u89e3\u6790\u5b57\u7b26\u4e32\u4e2d\u7684\u8f6c\u4e49\u5b57\u7b26\uff0c\u5982<code>\\n<\/code>\u3001<code>\\t<\/code>\u7b49\u3002<\/li>\n\n\n\n<li>\u5c06<code>Unicode<\/code>\u8f6c\u4e49\u5e8f\u5217<code>\\uXXXX<\/code>\u8f6c\u6362\u4e3a<code>UTF-8<\/code>\u5b57\u7b26\u3002<\/li>\n\n\n\n<li>\u5904\u7406<code>UTF-16<\/code>\u4ee3\u7406\u5bf9<code>surrogate pairs<\/code><\/li>\n<\/ul>\n\n\n\n<p><strong>\u6e90\u4ee3\u7801\u4e2d\u51fd\u6570\u5b9e\u73b0\u5982\u4e0b<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\/* parse_hex4\u7528\u4e8e\u89e3\u6790\u4e00\u4e2a4\u4f4d\u5341\u516d\u8fdb\u5236\u6570\uff0c\u5e76\u5c06\u5176\u8f6c\u6362\u4e3a\u5bf9\u5e94\u7684\u65e0\u7b26\u53f7\u6574\u6570 *\/\nstatic unsigned parse_hex4(const char *str)\n{\n    unsigned h=0;\n    if (*str&gt;='0' &amp;&amp; *str&lt;='9') h+=(*str)-'0'; else if (*str&gt;='A' &amp;&amp; *str&lt;='F') h+=10+(*str)-'A'; else if (*str&gt;='a' &amp;&amp; *str&lt;='f') h+=10+(*str)-'a'; else return 0;\n    h=h&lt;&lt;4;str++;\n    if (*str&gt;='0' &amp;&amp; *str&lt;='9') h+=(*str)-'0'; else if (*str&gt;='A' &amp;&amp; *str&lt;='F') h+=10+(*str)-'A'; else if (*str&gt;='a' &amp;&amp; *str&lt;='f') h+=10+(*str)-'a'; else return 0;\n    h=h&lt;&lt;4;str++;\n    if (*str&gt;='0' &amp;&amp; *str&lt;='9') h+=(*str)-'0'; else if (*str&gt;='A' &amp;&amp; *str&lt;='F') h+=10+(*str)-'A'; else if (*str&gt;='a' &amp;&amp; *str&lt;='f') h+=10+(*str)-'a'; else return 0;\n    h=h&lt;&lt;4;str++;\n    if (*str&gt;='0' &amp;&amp; *str&lt;='9') h+=(*str)-'0'; else if (*str&gt;='A' &amp;&amp; *str&lt;='F') h+=10+(*str)-'A'; else if (*str&gt;='a' &amp;&amp; *str&lt;='f') h+=10+(*str)-'a'; else return 0;\n    return h;\n}\n\n\/* Parse the input text into an unescaped cstring, and populate item. *\/\nstatic const unsigned char firstByteMark&#91;7] = { 0x00, 0x00, 0xC0, 0xE0, 0xF0, 0xF8, 0xFC };\nstatic const char *parse_string(cJSON *item,const char *str)\n{\n    const char *ptr=str+1;char *ptr2;char *out;int len=0;unsigned uc,uc2;\n    if (*str!='\\\"') {ep=str;return 0;}  \/* not a string! *\/\n\n    while (*ptr!='\\\"' &amp;&amp; *ptr &amp;&amp; ++len) if (*ptr++ == '\\\\') ptr++;  \/* Skip escaped quotes. *\/\n\n    out=(char*)cJSON_malloc(len+1); \/* This is how long we need for the string, roughly. *\/\n    if (!out) return 0;\n\n    ptr=str+1;ptr2=out;\n    while (*ptr!='\\\"' &amp;&amp; *ptr)\n    {\n        if (*ptr!='\\\\') *ptr2++=*ptr++;\n        else\n        {\n            ptr++;\n            switch (*ptr)\n            {\n                case 'b': *ptr2++='\\b'; break;\n                case 'f': *ptr2++='\\f'; break;\n                case 'n': *ptr2++='\\n'; break;\n                case 'r': *ptr2++='\\r'; break;\n                case 't': *ptr2++='\\t'; break;\n                case 'u':    \/* transcode utf16 to utf8. *\/\n                    uc=parse_hex4(ptr+1);ptr+=4;    \/* get the unicode char. *\/\n\n                    if ((uc&gt;=0xDC00 &amp;&amp; uc&lt;=0xDFFF) || uc==0)    break;  \/* check for invalid.   *\/\n\n                    if (uc&gt;=0xD800 &amp;&amp; uc&lt;=0xDBFF)   \/* UTF16 surrogate pairs.   *\/\n                    {\n                        if (ptr&#91;1]!='\\\\' || ptr&#91;2]!='u')    break;  \/* missing second-half of surrogate.    *\/\n                        uc2=parse_hex4(ptr+3);ptr+=6;\n                        if (uc2&lt;0xDC00 || uc2&gt;0xDFFF)       break;  \/* invalid second-half of surrogate.    *\/\n                        uc=0x10000 + (((uc&amp;0x3FF)&lt;&lt;10) | (uc2&amp;0x3FF));\n                    }\n\n                    len=4;if (uc&lt;0x80) len=1;else if (uc&lt;0x800) len=2;else if (uc&lt;0x10000) len=3; ptr2+=len;\n\n                    switch (len) {\n                        case 4: *--ptr2 =((uc | 0x80) &amp; 0xBF); uc &gt;&gt;= 6;\n                        case 3: *--ptr2 =((uc | 0x80) &amp; 0xBF); uc &gt;&gt;= 6;\n                        case 2: *--ptr2 =((uc | 0x80) &amp; 0xBF); uc &gt;&gt;= 6;\n                        case 1: *--ptr2 =(uc | firstByteMark&#91;len]);\n                    }\n                    ptr2+=len;\n                    break;\n                default:  *ptr2++=*ptr; break;\n            }\n            ptr++;\n        }\n    }\n    *ptr2=0;\n    if (*ptr=='\\\"') ptr++;\n    item-&gt;valuestring=out;\n    item-&gt;type=cJSON_String;\n    return ptr;\n}<\/code><\/pre>\n\n\n\n<p><strong>\u51fd\u6570\u6838\u5fc3\u903b\u8f91<\/strong><\/p>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\">\n<p>\u7b14\u8005\u6ce8\uff1a\u4e0b\u6587\u4ee3\u7801\u5df2\u683c\u5f0f\u5316\u5904\u7406\uff0c\u5e76\u9002\u5f53\u7b80\u5316\u53ea\u4fdd\u7559\u6838\u5fc3\u903b\u8f91<\/p>\n<\/blockquote>\n\n\n\n<p>\u5c06\u4f20\u5165\u7684\u5b57\u7b26\u4e32\u8fdb\u884c\u904d\u5386\uff0c\u5bf9\u4e8e\u5176\u4e2d\u7684\u8f6c\u4e49\u5b57\u7b26\u548c<code>Unicode<\/code>\u5b57\u7b26\u505a\u4e13\u95e8\u8f6c\u5316\u5904\u7406\uff0c\u5c06\u8f6c\u5316\u540e\u7684\u7ed3\u679c\u5b58\u5165\u4e00\u5757\u7533\u8bf7\u597d\u7684\u5185\u5b58\u4e2d\u3002\u4f7f\u7528<code>item-&gt;valuestring<\/code>\u6307\u5411\u8fd9\u5757\u5185\u5b58\u7528\u4f5c\u51fa\u53c2\uff0c\u51fd\u6570\u8fd4\u56de\u904d\u5386\u5b57\u7b26\u4e32\u7684\u7ed3\u5c3e<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>static const char *parse_string(cJSON *item, const char *str)\n{\n    const char *ptr = str+1;\n    char *ptr2;\n    char *out;\n    int len = 0;\n    unsigned uc, uc2;\n\n    \/*\n     * \u6b64\u5904\u5bf9'\\\\'\u7279\u6b8a\u5904\u7406\u662f\u8003\u8651\u5230\u540e\u7eed\u5b57\u7b26\u4e32\u5904\u7406\u6d41\u7a0b\u4e2d'\\'\u53ef\u80fd\u4f1a\u88ab\u8bef\u8ba4\u4e3a\u8f6c\u4e49\u5b57\u7b26\n     * \u6240\u4ee5\u9047\u5230'\\\\'\u65f6\u9700\u8981\u989d\u5916\u504f\u79fb\u4e00\u4f4d\n     *\/\n    while (*ptr != '\\\"' &amp;&amp; *ptr &amp;&amp; ++len) {\n        if (*ptr++ == '\\\\') {\n            ptr++;\n        }\n    }\n\n    out = (char*)malloc(len + 1);\n\n    ptr = str + 1;\n    ptr2 = out;\n    \/*\n     * \u5c06ptr\u62f7\u8d1d\u5230ptr2\u4e2d\uff0c\u5bf9\u4e8e\u8f6c\u4e49\u5b57\u7b26\u548c\n     *\/\n    while (*ptr!='\\\"' &amp;&amp; *ptr) {\n        if (*ptr!='\\\\') {\n            *ptr2++ = *ptr++;\n        } else {\n            ptr++;\n            switch (*ptr) {\n                case 'b':\n                    *ptr2++ = '\\b';\n                    break;\n                case 'f':\n                    *ptr2++ = '\\f';\n                    break;\n                case 'n':\n                    *ptr2++ = '\\n';\n                    break;\n                case 'r':\n                    *ptr2++ = '\\r';\n                    break;\n                case 't':\n                    *ptr2++ = '\\t';\n                    break;\n                case 'u':    \/* transcode utf16 to utf8. *\/\n                        ...\n                        \/*\n                         * \\u \u540e\u9762\u8ddf\u7740\u56db\u4e2a\u5341\u516d\u8fdb\u5236\u6570\u5b57\uff080-9\u3001a-f \u6216 A-F\uff09\uff0c\u8868\u793a\u4e00\u4e2a Unicode \u5b57\u7b26\u7684\u4ee3\u7801\u70b9\n                         * \u6b64\u5904\u7bc7\u5e45\u9650\u5236\uff0c\u5bf9 utf16\u8f6cutf8\u7684\u5904\u7406\u540e\u6587\u4e2d\u8ba8\u8bba\n                         *\/\n                        ...\n                default:\n                    *ptr2++ = *ptr;\n                    break;\n            }\n            ptr++;\n        }\n    }\n    *ptr2 = 0;\n\n    if (*ptr == '\\\"') {\n        ptr++;\n    }\n    \/*\n     * \u6b64\u5904\u4e0d\u80fdfree(out)\n     * \u7ed3\u6784\u4f53\u5b9a\u4e49\u4e2dvaluestring\u662f\u4e00\u4e2achar*\u7c7b\u578b\uff0c\u8be5\u6307\u9488\u6307\u5411\u7684\u5185\u5b58\u7684\u751f\u547d\u5468\u671f\u5e94\u5f53\u4e0evaluestring\u6307\u9488\u4fdd\u6301\u4e00\u81f4\n     * \u6b64\u5904out\u6301\u6709\u7684\u5185\u5b58\u5e94\u5f53\u5728valuestring\u6307\u9488\u7f6eNULL\u65f6\u518dfree\uff0c\u5426\u5219\u53ef\u80fd\u51fa\u73b0\u5185\u5b58\u8e29\u7a7a\u6216\u91ce\u6307\u9488\u7684\u95ee\u9898\n     *\/\n    item-&gt;valuestring = out;\n    item-&gt;type = cJSON_String;\n\n    return ptr;\n}<\/code><\/pre>\n\n\n\n<p><strong>\u5bf9\u4e8e <code>\\u<\/code>\u8f6c\u4e49\u5b57\u7b26\u7684\u5904\u7406<\/strong><\/p>\n\n\n\n<p><strong>\u6838\u5fc3\u903b\u8f91\u5982\u4e0b<\/strong><\/p>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\">\n<p>\u7b14\u8005\u6ce8\uff1a\u4e0b\u6587\u4ee3\u7801\u5df2\u683c\u5f0f\u5316\u5904\u7406\uff0c\u5e76\u9002\u5f53\u7b80\u5316\u53ea\u4fdd\u7559\u6838\u5fc3\u903b\u8f91<\/p>\n<\/blockquote>\n\n\n\n<p>\u6b64\u5904\u5bf9<code>Unicode<\/code>\u5b57\u7b26\u5904\u7406\u6d41\u7a0b\u4e2d\u4f5c\u8005\u5229\u7528<code>Unicode<\/code>\u5b57\u7b26\u672c\u8eab\u7684\u7279\u70b9\u8fdb\u884c\u9488\u5bf9\u6027\u5904\u7406\uff0c\u6d41\u7a0b\u4e2d\u4f7f\u7528\u4e86\u5927\u91cf<code>Macgic Number<\/code>\uff0c\u5728\u6b64\u5217\u51fa\u5e76\u6ce8\u89e3\u3002<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>firstByteMark[7]<\/code><\/li>\n\n\n\n<li><code>{ 0x00, 0x00, 0xC0, 0xE0, 0xF0, 0xF8, 0xFC }<\/code>\u6570\u7ec4\u5143\u7d20\u5bf9\u5e94\u4e0d\u540c\u957f\u5ea6\u7684<code>UTF-8<\/code>\u5b57\u7b26\u7684\u8d77\u59cb\u5b57\u8282\u6807\u8bb0<\/li>\n\n\n\n<li><code>0x00<\/code>\uff1a\u4fdd\u7559<\/li>\n\n\n\n<li><code>0xC0<\/code>\uff1a\u7528\u4e8e 2 \u5b57\u8282 UTF-8 \u7f16\u7801\u7684\u8d77\u59cb\u5b57\u8282<\/li>\n\n\n\n<li><code>0xE0<\/code>\uff1a\u7528\u4e8e 3 \u5b57\u8282 UTF-8 \u7f16\u7801\u7684\u8d77\u59cb\u5b57\u8282<\/li>\n\n\n\n<li><code>0xF0<\/code>\uff1a\u7528\u4e8e 4 \u5b57\u8282 UTF-8 \u7f16\u7801\u7684\u8d77\u59cb\u5b57\u8282<\/li>\n\n\n\n<li><code>0xF8<\/code>\uff1a\u7528\u4e8e 5 \u5b57\u8282 UTF-8 \u7f16\u7801\u7684\u8d77\u59cb\u5b57\u8282<\/li>\n\n\n\n<li><code>0xFC<\/code>\uff1a\u7528\u4e8e 6 \u5b57\u8282 UTF-8 \u7f16\u7801\u7684\u8d77\u59cb\u5b57\u8282<\/li>\n\n\n\n<li><code>Unicode<\/code>\u7f16\u7801\u5212\u5206\u4e86\u8bb8\u591a\u4e0d\u540c\u7684\u533a\u95f4\uff1b\u5176\u4e2d\u6709\u51e0\u4e2a\u7279\u6b8a\u7684\u533a\u95f4\uff0c\u4f4e\u4f4d\u4e13\u7528\u533a\u95f4\u3001\u9ad8\u4f4d\u66ff\u4ee3\u533a\u95f4\u3001\u9ad8\u4f4d\u4e13\u7528\u66ff\u4ee3\u533a\u95f4\u3002\u8fd9\u4e9b\u533a\u95f4\u7684\u5b57\u7b26\u4e0d\u5177\u6709\u5b9e\u9645\u542b\u4e49<\/li>\n<\/ul>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\">\n<p><a href=\"https:\/\/www.fuhaoku.net\/blocks\">Unicode\u7f16\u7801\u533a\u95f4\u8868\u67e5\u8be2<\/a><\/p>\n<\/blockquote>\n\n\n\n<pre class=\"wp-block-code\"><code>static const unsigned char firstByteMark&#91;7] = { 0x00, 0x00, 0xC0, 0xE0, 0xF0, 0xF8, 0xFC };\n\ncase 'u':     \/* transcode utf16 to utf8. *\/\n    uc = parse_hex4(ptr + 1);\n    ptr += 4;\n\n    \/* Unicoded \u4f4e\u4f4d\u4e13\u7528\u533a\u95f4 *\/\n    if ((uc &gt;= 0xDC00 &amp;&amp; uc &lt;= 0xDFFF) || uc == 0) {\n        break;\n    }\n\n    \/* Unicoded \u9ad8\u4f4d\u66ff\u4ee3\u533a\u95f4\u3001\u9ad8\u4f4d\u66ff\u4ee3\u4e13\u7528\u533a\u95f4 *\/\n    if (uc &gt;= 0xD800 &amp;&amp; uc &lt;= 0xDBFF) {\n        uc2 = parse_hex4(ptr + 3);\n        ptr += 6;\n        \/* \u6839\u636e UTF-16 \u7f16\u7801\u89c4\u5219\uff0c\u5c06\u4e24\u4e2a\u4ee3\u7406\u9879\u7ec4\u5408\u6210\u4e00\u4e2a Unicode \u7801\u70b9 *\/\n        uc = 0x10000 + (((uc&amp;0x3FF)&lt;&lt;10) | (uc2&amp;0x3FF));\n    }\n\n    \/* \u6839\u636e Unicode \u7801\u70b9\u7684\u8303\u56f4\u786e\u5b9a\u5b57\u7b26\u6240\u5360\u7684\u5b57\u8282\u6570 *\/\n    len = 4;\n    if (uc &lt; 0x80) {\n        len = 1;\n    } else if (uc &lt; 0x800) {\n        len = 2;\n    } else if (uc &lt; 0x10000) {\n        len = 3;\n        ptr2 += len;\n    }\n\n    \/* \u5c06 Unicode \u7801\u70b9\u6309\u7167 UTF-8 \u7f16\u7801\u89c4\u5219\u8f6c\u6362\u4e3a\u5bf9\u5e94\u7684\u5b57\u8282\u5e8f\u5217 *\/\n    switch (len) {\n        case 4:\n            *--ptr2 =((uc | 0x80) &amp; 0xBF);\n            uc &gt;&gt;= 6;\n        case 3:\n            *--ptr2 =((uc | 0x80) &amp; 0xBF);\n            uc &gt;&gt;= 6;\n        case 2:\n            *--ptr2 =((uc | 0x80) &amp; 0xBF);\n            uc &gt;&gt;= 6;\n        case 1:\n            *--ptr2 =(uc | firstByteMark&#91;len]);\n    }\n    ptr2 += len;\n    break;<\/code><\/pre>\n\n\n\n<p><strong>\u51fd\u6570\u6838\u5fc3\u601d\u60f3<\/strong><\/p>\n\n\n\n<p>\u5bf9\u8f6c\u4e49\u5b57\u7b26\u548c<code>Unicode<\/code>\u7f16\u7801\u5b57\u7b26\u7684\u8f6c\u6362<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">3.3 \u89e3\u6790json\u683c\u5f0f\u6587\u4ef6<\/h3>\n\n\n\n<p><code>cJSON<\/code>\u6e90\u7801\u4e2d\u7ed9\u51fa\u4e86\u4e00\u4e2a\u89e3\u6790<code>json<\/code>\u6587\u4ef6\u7684\u793a\u4f8b\uff0c\u4ece\u6587\u4ef6\u4e2d\u8bfb\u53d6\u5185\u5bb9\u7136\u540e\u5b58\u653e\u5728\u5185\u5b58\u4e2d\uff0c\u672c\u8d28\u4e0a\u4e0e\u4ece\u5b57\u7b26\u4e32\u4e2d\u89e3\u6790<code>json<\/code>\u6ca1\u6709\u533a\u522b<\/p>\n\n\n\n<p><strong>\u6838\u5fc3\u903b\u8f91\u5982\u4e0b<\/strong><\/p>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\">\n<p>\u7b14\u8005\u6ce8\uff1a\u4e0b\u6587\u4ee3\u7801\u5df2\u683c\u5f0f\u5316\u5904\u7406\uff0c\u5e76\u9002\u5f53\u7b80\u5316\u53ea\u4fdd\u7559\u6838\u5fc3\u903b\u8f91<\/p>\n<\/blockquote>\n\n\n\n<pre class=\"wp-block-code\"><code>void doit(char *text)\n{\n    char *out;\n    cJSON *json;\n\n    json = cJSON_Parse(text);\n    out = cJSON_Print(json);\n    cJSON_Delete(json);\n    printf(\"%s\\n\", out);\n    free(out);\n}\n\n\/* Read a file, parse, render back, etc. *\/\nvoid dofile(char *filename)\n{\n    FILE *f;\n    long len;\n    char *data;\n\n    f = fopen(filename, \"rb\");\n    fseek(f, 0, SEEK_END);\n\n    len = ftell(f);\n    fseek(f, 0, SEEK_SET);\n\n    data = (char*)malloc(len + 1);\n    fread(data, 1, len, f);\n\n    fclose(f);\n\n    doit(data);\n    free(data);\n}<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">4. \u6784\u9020json<\/h2>\n\n\n\n<p><strong>\u6982\u62ec\u7684\u8bf4<code>cJSON<\/code>\u6784\u9020<code>json<\/code>\u7684\u8fc7\u7a0b\u662f\uff0c\u521b\u5efa\u4e00\u4e2a<code>struct cJSON<\/code>\u7c7b\u578b\u7684\u8282\u70b9\uff0c\u9996\u5148\u5c06\u9700\u8981\u7684\u6570\u636e\u586b\u5145\u8fd9\u4e2a\u65b0\u5efa\u7684\u8282\u70b9\u3002\u7136\u540e\u5c06\u8fd9\u4e2a\u65b0\u8282\u70b9\u6dfb\u52a0\u5230\u5df2\u6709\u7684\u65e7\u8282\u70b9\u7684\u5408\u9002\u7684\u4f4d\u7f6e\u4e2d\uff0c\u800c\u65e7\u8282\u70b9\u7684\u7c7b\u578b\u4e5f\u662f\u4e00\u4e2a<code>struct cJSON<\/code>\u7c7b\u578b\u7684\u8282\u70b9\u3002\u6240\u4ee5<code>cJSON<\/code>\u6784\u9020<code>json<\/code>\u7684\u8fc7\u7a0b\u4fbf\u662f\u4e00\u5c42\u5c42\u7684\u5d4c\u5957<code>struct cJSON<\/code>\u8282\u70b9\u7684\u8fc7\u7a0b\uff0c\u5bf9\u4e8e\u5b69\u5b50\u8282\u70b9\u548c\u5144\u5f1f\u8282\u70b9\u7684\u5904\u7406\u548c\u65b0\u8282\u70b9\u7684\u521b\u5efa\u548c\u5d4c\u5957\u4fbf\u662f\u672c\u7ae0\u8ba8\u8bba\u7684\u91cd\u70b9\u3002<\/strong><\/p>\n\n\n\n<p>\u4ece<code>cJSON<\/code>\u6e90\u4ee3\u7801\u4e2d\u7ed9\u51fa\u7684\u6d4b\u8bd5<code>demo<\/code>\u4e2d\uff0c\u53ef\u4ee5\u627e\u5230\u5173\u4e8e\u7684\u5173\u4e8e\u6784\u9020<code>json<\/code>\u5b57\u7b26\u4e32\u7684\u793a\u4f8b\u51fd\u6570<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">\u5173\u952e\u63a5\u53e3<\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>cJSON_CreateObject()<\/code>\u521b\u5efa<code>object<\/code>\u5bf9\u8c61\u8282\u70b9<\/li>\n\n\n\n<li><code>cJSON_AddItemToObject()<\/code>\u5411<code>object<\/code>\u4e2d\u6dfb\u52a0<code>cJSON<\/code>\u5bf9\u8c61<\/li>\n\n\n\n<li><code>cJSON_AddNumberToObject()<\/code>\u5411<code>object<\/code>\u4e2d\u6dfb\u52a0<code>number<\/code>\u5bf9\u8c61<\/li>\n\n\n\n<li><code>cJSON_AddStringToObject()<\/code>\u5411<code>object<\/code>\u4e2d\u6dfb\u52a0<code>string<\/code>\u5bf9\u8c61<\/li>\n\n\n\n<li><code>cJSON_CreateIntArray()<\/code>\u521b\u5efa<code>array<\/code>\u5bf9\u8c61\u8282\u70b9<\/li>\n<\/ul>\n\n\n\n<p><strong>\u6838\u5fc3\u903b\u8f91\u5982\u4e0b<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>void create_objects()\n{\n    cJSON *root, *img, *thm, *IDs;\n    char *name_str, *out;;\n    int ids&#91;4] = {116, 943, 234, 38793};\n\n    root = cJSON_CreateObject();\n    img = cJSON_CreateObject();\n    cJSON_AddItemToObject(root, \"Image\", img);\n    cJSON_AddNumberToObject(img, \"Width\", 800);\n    cJSON_AddNumberToObject(img, \"Height\", 600);\n    cJSON_AddStringToObject(img, \"Title\", \"View from 15th Floor\");\n    thm = cJSON_CreateObject();\n    cJSON_AddItemToObject(img, \"Thumbnail\", thm);\n    cJSON_AddStringToObject(thm, \"Url\", \"http:\/*www.example.com\/image\/481989943\");\n    cJSON_AddNumberToObject(thm, \"Height\", 125);\n    cJSON_AddStringToObject(thm, \"Width\", \"100\");\n    IDs = cJSON_CreateIntArray(ids, 4);\n    cJSON_AddItemToObject(img, \"IDs\", IDs);\n\n    out = cJSON_Print(root);\n    cJSON_Delete(root);\n    printf(\"%s\\n\", out);\n    free(out);\n}\n\n\u8f93\u51fa\u7ed3\u679c\u4e3a\n{\n        \"Image\":        {\n                \"Width\":        800,\n                \"Height\":       600,\n                \"Title\":        \"View from 15th Floor\",\n                \"Thumbnail\":    {\n                        \"Url\":  \"http:\/*www.example.com\/image\/481989943\",\n                        \"Height\":       125,\n                        \"Width\":        \"100\"\n                },\n                \"IDs\":  &#91;116, 943, 234, 38793]\n        }\n}<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\"><code>cJSON_CreateObject()<\/code>\u51fd\u6570<\/h3>\n\n\n\n<p>\u521b\u5efa\u65b0\u7684<code>cJSON<\/code>\u8282\u70b9\uff0c\u5e76\u5c06<code>type<\/code>\u8bbe\u7f6e\u4e3a<code>cJSON_Object<\/code><\/p>\n\n\n\n<p><strong>\u6838\u5fc3\u903b\u8f91\u5982\u4e0b<\/strong><\/p>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\">\n<p>\u7b14\u8005\u6ce8\uff1a\u4e0b\u6587\u4ee3\u7801\u5df2\u683c\u5f0f\u5316\u5904\u7406\uff0c\u5e76\u9002\u5f53\u7b80\u5316\u53ea\u4fdd\u7559\u6838\u5fc3\u903b\u8f91<\/p>\n<\/blockquote>\n\n\n\n<pre class=\"wp-block-code\"><code>cJSON *cJSON_CreateObject(void)\n{\n    cJSON *item = cJSON_New_Item();\n\n    if(item) {\n        item-&gt;type = cJSON_Object;\n    }\n\n    return item;\n}<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\"><code>cJSON_AddItemToObject()<\/code>\u51fd\u6570<\/h3>\n\n\n\n<p>\u586b\u5145\u65b0\u8282\u70b9\u7684<code>string<\/code>\u5143\u7d20\uff0c\u7136\u540e\u8c03\u7528<code>cJSON_AddItemToArray()<\/code>\u51fd\u6570\u3002<\/p>\n\n\n\n<p><strong>\u6838\u5fc3\u903b\u8f91\u5982\u4e0b<\/strong><\/p>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\">\n<p>\u7b14\u8005\u6ce8\uff1a\u4e0b\u6587\u4ee3\u7801\u5df2\u683c\u5f0f\u5316\u5904\u7406\uff0c\u5e76\u9002\u5f53\u7b80\u5316\u53ea\u4fdd\u7559\u6838\u5fc3\u903b\u8f91<\/p>\n<\/blockquote>\n\n\n\n<pre class=\"wp-block-code\"><code>void cJSON_AddItemToObject(cJSON *object, onst char *string, JSON *item)\n{\n    size_t len;\n    char* copy;\n\n    len = strlen(string) + 1;\n    copy = (char*)malloc(len)\n\n    memcpy(copy, string, len);\n    item-&gt;string = copy;\n\n    cJSON_AddItemToArray(object, item);\n}<\/code><\/pre>\n\n\n\n<p><strong>\u51fd\u6570\u6838\u5fc3\u601d\u60f3<\/strong><\/p>\n\n\n\n<p>\u672c\u8d28\u662f\u5bf9<code>cJSON_AddItemToArray()<\/code>\u51fd\u6570\u7684\u4e00\u5c42\u5c01\u88c5<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><code>cJSON_AddItemToArray()<\/code>\u51fd\u6570<\/h3>\n\n\n\n<p>\u5411<code>array<\/code>\u4e2d\u6dfb\u52a0\u65b0\u8282\u70b9<\/p>\n\n\n\n<p><strong>\u6838\u5fc3\u903b\u8f91\u5982\u4e0b<\/strong><\/p>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\">\n<p>\u7b14\u8005\u6ce8\uff1a\u4e0b\u6587\u4ee3\u7801\u5df2\u683c\u5f0f\u5316\u5904\u7406\uff0c\u5e76\u9002\u5f53\u7b80\u5316\u53ea\u4fdd\u7559\u6838\u5fc3\u903b\u8f91<\/p>\n<\/blockquote>\n\n\n\n<pre class=\"wp-block-code\"><code>\/* Add item to array\/object. *\/\nvoid cJSON_AddItemToArray(cJSON *array, cJSON *item)\n{\n    cJSON *c = array-&gt;child;\n\n    if (!c) {\n        array-&gt;child = item;\n    } else {\n        while (c &amp;&amp; c-&gt;next) {\n            c = c-&gt;next;\n        }\n        c-&gt;next = item;\n        item-&gt;prev = c;\n    }\n}<\/code><\/pre>\n\n\n\n<p><strong>\u51fd\u6570\u6838\u5fc3\u601d\u60f3<\/strong><\/p>\n\n\n\n<p>\u82e5<code>array<\/code>\u6ca1\u6709\u5b69\u5b50\u8282\u70b9\uff0c\u5219\u5c06\u65b0\u8282\u70b9\u4f5c\u4e3a<code>array<\/code>\u7684\u5b69\u5b50\u8282\u70b9<\/p>\n\n\n\n<p>\u82e5<code>array<\/code>\u6709\u5b69\u5b50\u8282\u70b9\uff0c\u5219\u5c06\u627e\u5230\u8be5\u8282\u70b9\u7684\u53cc\u5411\u94fe\u8868\u5c3e\u90e8\u63d2\u5165\uff0c\u5c06\u65b0\u8282\u70b9\u4f5c\u4e3a<code>array<\/code>\u7684\u5b69\u5b50\u8282\u70b9<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><code>cJSON_AddItemToArray()<\/code>\u51fd\u6570\u548c<code>cJSON_AddStringToObject()<\/code>\u51fd\u6570<\/h3>\n\n\n\n<p>\u672c\u8d28\u662f\u5bf9<code>cJSON_AddItemToObject()<\/code>\u51fd\u6570\u7684\u4e00\u5c42\u5c01\u88c5<\/p>\n\n\n\n<p><strong>\u6838\u5fc3\u903b\u8f91\u5982\u4e0b<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>#define cJSON_AddNumberToObject(object,name,n)    cJSON_AddItemToObject(object, name, cJSON_CreateNumber(n))\n#define cJSON_AddStringToObject(object,name,s)    cJSON_AddItemToObject(object, name, cJSON_CreateString(s))<\/code><\/pre>\n\n\n\n<p><strong>\u51fd\u6570\u6838\u5fc3\u601d\u60f3<\/strong><\/p>\n\n\n\n<p>\u5bf9\u4e8e<code>cJSON<\/code>\u6765\u8bf4\u4e0d\u7ba1\u8282\u70b9\u7c7b\u578b\u662f<code>object<\/code>\u6216\u662f<code>array<\/code>\u5176\u5b58\u50a8\u7c7b\u578b\u5747\u4e3a<code>struct cJSON<\/code>\u6240\u4ee5\u6dfb\u52a0\u65b0\u7684\u6210\u5458\u65f6\uff0c\u5e95\u5c42\u63a5\u53e3\u5747\u53ef\u4f7f\u7528<code>cJSON_AddItemToObject()<\/code><\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><code>cJSON_CreateIntArray()<\/code>\u51fd\u6570<\/h3>\n\n\n\n<p>\u65b0\u5efa<code>array<\/code>\u5143\u7d20\uff0c\u5e76\u586b\u5145\u6574\u5f62\u6570\u7ec4<code>numbers<\/code>\u7684\u5143\u7d20<\/p>\n\n\n\n<p><strong>\u6838\u5fc3\u903b\u8f91\u5982\u4e0b<\/strong><\/p>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\">\n<p>\u7b14\u8005\u6ce8\uff1a\u4e0b\u6587\u4ee3\u7801\u5df2\u683c\u5f0f\u5316\u5904\u7406\uff0c\u5e76\u9002\u5f53\u7b80\u5316\u53ea\u4fdd\u7559\u6838\u5fc3\u903b\u8f91<\/p>\n<\/blockquote>\n\n\n\n<pre class=\"wp-block-code\"><code>\/* \u521b\u5efa\u65b0\u7684array\u8282\u70b9 *\/\ncJSON *cJSON_CreateArray(void)\n{\n    cJSON *item = cJSON_New_Item();\n    item-&gt;type = cJSON_Array;\n\n    return item;\n}\n\n\/* \u521b\u5efa\u65b0\u7684number\u8282\u70b9 *\/\ncJSON *cJSON_CreateNumber(double num)\n{\n    cJSON *item = cJSON_New_Item();\n\n    item-&gt;type = cJSON_Number;\n    item-&gt;valuedouble = num;\n    item-&gt;valueint = (int)num;\n\n    return item;\n}\n\n\/* Create Arrays: *\/\ncJSON *cJSON_CreateIntArray(const int *numbers, int count)\n{\n    int i;\n    cJSON *n, *p, *a;\n\n    *n = 0;\n    *p = 0;\n    *a = cJSON_CreateArray();\n\n    for(i = 0; a &amp;&amp; i &lt; count; i++) {\n        n = cJSON_CreateNumber(numbers&#91;i]);\n        \/* \u7b2c\u4e00\u4e2a\u5143\u7d20\u653e\u5728child\u8282\u70b9\u4e2d *\/\n        if(!i) {\n            a-&gt;child = n;\n        } else {\n            p-&gt;next = n;\n            n-&gt;prev = p;\n        }\n        \/* p\u59cb\u7ec8\u6307\u5411\u94fe\u8868\u7684\u5c3e\u90e8 *\/\n        p = n;\n    }\n\n    return a;\n}<\/code><\/pre>\n\n\n\n<p><strong>\u51fd\u6570\u6838\u5fc3\u601d\u60f3<\/strong><\/p>\n\n\n\n<p>\u521b\u5efa<code>array<\/code>\u5143\u7d20\uff0c\u5229\u7528\u5faa\u73af\u5c06\u5f85\u76ee\u6807\u6570\u7ec4\u7684\u5185\u5bb9\u586b\u5165\uff1b\u9996\u5148\u5c06\u7b2c\u4e00\u4e2a\u5143\u7d20\u9632\u5728<code>child<\/code>\u8282\u70b9\u4e2d\uff0c\u518d\u5c06\u540e\u7eed\u5143\u7d20\u4f9d\u6b21\u653e\u5165\u65b0\u521b\u5efa\u7684<code>child<\/code>\u7684\u5144\u5f1f\u8282\u70b9\u4e2d<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">5. cJSON\u8f93\u51fa<\/h2>\n\n\n\n<p><strong><code>cJSON<\/code>\u8f93\u51fa\u65f6\u76f8\u5173\u51fd\u6570\u8fd4\u56de\u503c\u7c7b\u578b\u4e3a<code>char*<\/code>\u7136\u540e\u7531\u8c03\u7528\u8005\u63a5\u6536\u3002\u8fd9\u4fbf\u5e26\u6765\u4e86\u4e00\u4e2a\u95ee\u9898\uff0c\u5982\u4f55\u4fdd\u8bc1\u8c03\u7528\u8005\u63a5\u6536\u5230\u7684\u6307\u9488\u6307\u5411\u7684\u5185\u5b58\u5730\u5740\u7a7a\u6216\u5783\u573e\u5185\u5b58\u3002\u5728\u5bf9\u4e8e<code>Struct cJSON<\/code>\u7684\u89e3\u6790\u8fc7\u7a0b\u7ed3\u679c\u7684\u6301\u4e45\u5316\u4e0e\u5185\u5b58\u7ba1\u7406\uff0c\u4fbf\u662f<code>cJSON<\/code>\u4e0d\u53ef\u907f\u514d\u7684\u95ee\u9898\u3002\u5bf9\u4e8e<code>cJSON<\/code>\u5982\u4f55\u5904\u7406\u8fd9\u4e2a\u95ee\u9898\u4e0a\uff0c\u5728\u4e0b\u6587\u4e2d\u5bf9<code>cJSON<\/code>\u6e90\u7801\u7684\u4e00\u6b65\u6b65\u5256\u6790\u4e2d\u53ef\u7aa5\u89c1\u4e00\u4e8c\u3002<\/strong><\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><code>print_value()<\/code>\u51fd\u6570<\/h3>\n\n\n\n<p>\u6839\u636e\u8282\u70b9\u7c7b\u578b\u9488\u5bf9\u4e0d\u540c\u7c7b\u578b\u9488\u5bf9\u5904\u7406\uff0c<strong>\u5176\u4e2d\u51fd\u6570\u5165\u53c2<code>depth,fmt,p<\/code>\u4f1a\u5f71\u54cd\u540e\u7eed\u5904\u7406\u903b\u8f91<\/strong><\/p>\n\n\n\n<p><strong>\u6838\u5fc3\u903b\u8f91\u5982\u4e0b<\/strong><\/p>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\">\n<p>\u7b14\u8005\u6ce8\uff1a\u4e0b\u6587\u4ee3\u7801\u5df2\u683c\u5f0f\u5316\u5904\u7406\uff0c\u5e76\u9002\u5f53\u7b80\u5316\u53ea\u4fdd\u7559\u6838\u5fc3\u903b\u8f91<\/p>\n<\/blockquote>\n\n\n\n<pre class=\"wp-block-code\"><code>\/* Render a value to text. *\/\nstatic char *print_value(cJSON *item, int depth, int fmt, printbuffer *p)\n{\n    char *out = NULL;\n    printbuffer *str = NULL;\n\n    if (p) {\n        str = p;\n    } else {\n        str = NULL;\n    }\n\n    switch ((item-&gt;type) &amp; 255) {\n        case cJSON_Number:\n            out = print_number(item, str);\n            break;\n        case cJSON_String:\n            out = print_string(item, str);\n            break;\n        case cJSON_Array:\n            out = print_array(item, depth, fmt, str);\n            break;\n        case cJSON_Object:\n            out = print_object(item, depth, fmt, str);\n            break;\n    }\n\n    return out;\n}<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\"><code>cJSON_Print()<\/code>\u51fd\u6570<\/h3>\n\n\n\n<p><strong>\u6838\u5fc3\u903b\u8f91\u5982\u4e0b<\/strong><\/p>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\">\n<p>\u7b14\u8005\u6ce8\uff1a\u4e0b\u6587\u4ee3\u7801\u5df2\u683c\u5f0f\u5316\u5904\u7406\uff0c\u5e76\u9002\u5f53\u7b80\u5316\u53ea\u4fdd\u7559\u6838\u5fc3\u903b\u8f91\uff1b<\/p>\n<\/blockquote>\n\n\n\n<pre class=\"wp-block-code\"><code>\/* Render a cJSON item\/entity\/structure to text. *\/\nchar *cJSON_Print(cJSON *item)\n{\n    return print_value(item, 0, 1, 0);\n}<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\"><code>print_object(item, 0, 1, 0)<\/code>\u51fd\u6570<\/h3>\n\n\n\n<p><strong>\u6838\u5fc3\u903b\u8f91\u5982\u4e0b<\/strong><\/p>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\">\n<p>\u7b14\u8005\u6ce8\uff1a\u4e0b\u6587\u4ee3\u7801\u5df2\u683c\u5f0f\u5316\u5904\u7406\uff0c\u5e76\u9002\u5f53\u7b80\u5316\u53ea\u4fdd\u7559\u6838\u5fc3\u903b\u8f91\uff1b\u6b64\u5904\u4ec5\u5217\u51fa<code>cJSON_Print<\/code>\u63a5\u53e3\u7684\u903b\u8f91\uff0c\u5373<code>print_number<\/code>\u7b2c\u4e8c\u4e2a\u53c2\u6570<code>printbuffer *p<\/code>\u4e3a<code>NULL<\/code>\u7684\u903b\u8f91\uff0c\u6545\u5220\u9664\u90e8\u5206\u5197\u4f59\u4ee3\u7801<\/p>\n<\/blockquote>\n\n\n\n<pre class=\"wp-block-code\"><code>\/* Render an object to text. *\/\nstatic char *print_object(cJSON *item, int depth, int fmt, printbuffer *p)\n{\n    char *out=  NULL, *ptr, *ret, *str;\n    int len = 7, i = 0, j;\n    size_t tmplen = 0;\n    cJSON *child;\n    child = item-&gt;child;\n\n    \/* \u8ba1\u7b97\u672c\u5c42\u5143\u7d20\u7684\u4e2a\u6570\uff0c\u4f7f\u7528numentries\u8bb0\u5f55 *\/\n    int numentries = 0;\n    while (child) {\n        numentries++, child = child-&gt;next;\n    }\n\n    \/*\n     * \u6b64\u5904entries\u548cnames\u53ef\u89c6\u4e3achar entries&#91;]&#91;]\u548cchar names&#91;]&#91;]\n     * names\u5b58\u50a8\":\"\u524d\u534a\u6bb5\u7684\u503c\uff0c\u5373object\u3001array\u6216\u952e\u503c\u5bf9\u7684\u540d\u79f0\n     * entries\u5b58\u50a8\u201c:\u201d\u540e\u534a\u6bb5\u7684\u503c\uff0c\u5373object\u3001array\u6216\u952e\u503c\u5bf9\u7684\u5185\u5bb9\n     *\/\n    char **entries = NULL, **names = NULL;\n\n    entries = (char**)malloc(numentries * sizeof(char*));\n    names = (char**)malloc(numentries * sizeof(char*));\n    memset(entries, 0, sizeof(char*) * numentries);\n    memset(names, 0, sizeof(char*) * numentries);\n\n    \/* depth\u8bb0\u5f55\u5f53\u524d\u5c42\u6570\u6df1\u5ea6 *\/\n    child = item-&gt;child;\n    depth ++;\n    len += depth;\n\n    \/* \u5faa\u73af\u4e2d\u4ec5\u641c\u7d22\u672c\u5c42\u8282\u70b9\uff0c\u5bf9\u4e8e\u6df1\u5c42\u7684child\u8282\u70b9\uff0c\u5728print_value()\u4e2d\u9012\u5f52\u5904\u7406 *\/\n    while (child) {\n        str = print_string_ptr(child-&gt;string, 0);\n        ret = print_value(child, depth, fmt, 0);\n\n        names&#91;i] = str;\n        entries&#91;i] = ret;\n        i++;\n\n        len += strlen(ret) + strlen(str) + 2 + (fmt ? 2 + depth : 0);\n        child = child-&gt;next;\n    }\n\n    out = (char*)malloc(len);\n\n    \/* \u6784\u9020object\u7684\u5de6\u5927\u62ec\u53f7 *\/\n    *out = '{';\n    ptr = out + 1;\n    *ptr++ = '\\n';\n    *ptr = 0;\n\n    \/* \u904d\u5386\u641c\u7d22\u6574\u4e2ajson *\/\n    for (i = 0; i &lt; numentries; i++) {\n        \/* \u586b\u5145\u6bcf\u4e00\u884c\u5de6\u4fa7\u7684tab *\/\n        for (j = 0; j &lt; depth; j++) {\n            *ptr++ = '\\t';\n        }\n\n        tmplen = strlen(names&#91;i]);\n        memcpy(ptr, names&#91;i], tmplen);\n        ptr += tmplen;\n        *ptr++ = ':';\n\n        strcpy(ptr, entries&#91;i]);\n        ptr += strlen(entries&#91;i]);\n\n        if (i != numentries - 1) {\n            *ptr++ = ',';\n        }\n\n        *ptr++ = '\\n';\n        *ptr = 0;\n\n        free(names&#91;i]);\n        free(entries&#91;i]);\n    }\n\n    free(names);\n    free(entries);\n\n    \/* \u586b\u5145\u6bcf\u4e00\u884c\u5de6\u4fa7\u7684tab *\/\n    for (i = 0; i &lt; depth - 1; i++) {\n        *ptr++ = '\\t';\n    }\n\n    *ptr++ = '}';\n    *ptr++ = 0;\n\n    return out; \n}<\/code><\/pre>\n\n\n\n<p><strong>\u51fd\u6570\u6838\u5fc3\u601d\u60f3<\/strong><\/p>\n\n\n\n<p><strong>\u83b7\u53d6<code>json<\/code>\u89e3\u6790\u7684\u5173\u952e\u5143\u7d20<\/strong><\/p>\n\n\n\n<p>\u8f93\u51fa\u4e00\u4e2a<code>object<\/code>\u65f6\uff0c\u9700\u8981\u83b7\u53d6\u5c42\u6570\u3001\u5f53\u524d\u5c42\u6709\u591a\u5c11\u4e2a\u5143\u7d20\u8fd9\u4e24\u4e2a\u5173\u952e\u4fe1\u606f<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u5f53\u524d\u662f\u5728\u54ea\u4e00\u5c42\uff0c\u4e0e\u9012\u5f52\u7684\u6df1\u5ea6\u76f8\u540c\uff0c\u83b7\u53d6\u9012\u5f52\u6df1\u5ea6\u5373\u53ef\uff0c\u5b58\u50a8\u5728<code>depth<\/code>\u53d8\u91cf\u4e2d<\/li>\n\n\n\n<li>\u672c\u5c42\u5143\u7d20\u6570\u91cf\uff0c\u901a\u8fc7\u904d\u5386\u5f53\u524d<code>cJSON<\/code>\u8282\u70b9\u6240\u5728\u53cc\u5411\u94fe\u8868\u4e0a\u6709\u591a\u5c11\u4e2a\u6210\u5458\u5f97\u5230\uff0c\u5b58\u50a8\u5728<code>numentries<\/code>\u53d8\u91cf\u4e2d\uff1b<\/li>\n<\/ul>\n\n\n\n<p><strong>\u5b58\u50a8\u5173\u952e\u5143\u7d20<\/strong><\/p>\n\n\n\n<p>\u8981\u8f93\u51fa<code>json<\/code>\u8fd8\u9700\u8981\u5f97\u5230\uff0c\u672c\u5c42<code>json<\/code>\u5143\u7d20\u7684\u540d\u79f0\u548c\u5185\u5bb9\u8fd9\u4e24\u9879\u5173\u952e\u5185\u5bb9<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u5143\u7d20\u7684\u540d\u79f0\uff0c\u901a\u8fc7<code>print_string_ptr(child-&gt;string)<\/code>\u83b7\u53d6\uff0c\u5b58\u653e\u5728<code>char *<\/code>\u6570\u7ec4<code>names<\/code>\u4e2d<\/li>\n\n\n\n<li>\u5143\u7d20\u7684\u5185\u5bb9\uff0c\u901a\u8fc7<code>print_value()<\/code>\u83b7\u53d6\uff0c\u5b58\u653e\u5728<code>char *<\/code>\u6570\u7ec4<code>entries<\/code>\u4e2d<\/li>\n<\/ul>\n\n\n\n<p><strong>\u5143\u7d20\u62fc\u63a5<\/strong><\/p>\n\n\n\n<p><strong>\u9996\u5148\u521b\u5efa\u5de6\u4fa7\u5927\u62ec\u53f7<\/strong><\/p>\n\n\n\n<p>\u4f7f\u7528\u4e00\u4e2a<code>for<\/code>\u5faa\u73af\u904d\u5386\u5f53\u524d\u5c42\u7684\u6240\u6709\u5143\u7d20\uff0c\u6309\u7167<code>\"\u540d\u79f0:\u5185\u5bb9,\"<\/code>\u7684\u5f62\u5f0f\u5c06\u5f53\u524d\u5c42\u7684\u5143\u7d20\u62fc\u63a5\u8d77\u6765\uff1b\u9700\u8981\u6ce8\u610f\u7684\u662f\u5728\u6b64\u524d<strong>\u89e3\u6790\u5173\u952e\u5143\u7d20<\/strong>\u65f6\uff0c\u5bf9\u4e8e\u6240\u6709\u7c7b\u578b\u7684\u5143\u7d20\u5df2\u7ecf\u5168\u90e8\u8f6c\u5316\u6210\u4e86\u5b57\u7b26\u4e32\u3002\u5373\u4fbf\u5f53\u524d\u5143\u7d20\u662f\u4e00\u4e2a<code>object<\/code>\u6216\u662f<code>array<\/code>\uff0c\u4e5f\u4e0d\u9700\u8981\u8fdb\u884c\u7279\u6b8a\u5904\u7406<\/p>\n\n\n\n<p><strong>\u6700\u540e\u521b\u5efa\u53f3\u4fa7\u5927\u62ec\u53f7<\/strong><\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><code>print_array(item, 0, 1, 0)<\/code>\u51fd\u6570<\/h3>\n\n\n\n<p><strong>\u6838\u5fc3\u903b\u8f91\u5982\u4e0b<\/strong><\/p>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\">\n<p>\u7b14\u8005\u6ce8\uff1a\u4e0b\u6587\u4ee3\u7801\u5df2\u683c\u5f0f\u5316\u5904\u7406\uff0c\u5e76\u9002\u5f53\u7b80\u5316\u53ea\u4fdd\u7559\u6838\u5fc3\u903b\u8f91\uff1b\u6b64\u5904\u4ec5\u5217\u51fa<code>cJSON_Print<\/code>\u63a5\u53e3\u7684\u903b\u8f91\uff0c\u5373<code>print_array<\/code>\u7b2c\u4e8c\u4e2a\u53c2\u6570<code>int depth<\/code>\u4e3a<code>0<\/code>\uff1b\u7b2c\u4e09\u4e2a\u53c2\u6570<code>int fmt<\/code>\u4e3a<code>1<\/code>\uff1b\u7b2c\u56db\u4e2a\u53c2\u6570<code>printbuffer *p<\/code>\u4e3a<code>NULL<\/code>\u7684\u903b\u8f91\uff0c\u6545\u5220\u9664\u90e8\u5206\u5197\u4f59\u4ee3\u7801<\/p>\n<\/blockquote>\n\n\n\n<pre class=\"wp-block-code\"><code>\/* Render an array to text *\/\nstatic char *print_array(cJSON *item, int depth, int fmt, printbuffer *p)\n{\n    char **entries;\n    char *out = 0, *ptr, *ret;\n    int len = 5;\n    cJSON *child;\n    child = item-&gt;child;\n    int numentries=0, i=0;\n    size_t tmplen=0;\n\n    while (child) {\n        numentries++, child = child-&gt;next;\n    }\n\n    entries=(char**)malloc(numentries * sizeof(char*));\n    memset(entries, 0, numentries * sizeof(char*));\n\n    child = item-&gt;child;\n    while (child) {\n        ret = print_value(child, depth + 1, fmt, 0);\n        entries&#91;i] = ret;\n        i++;\n\n        len += strlen(ret) + 2 + (fmt ? 1 : 0);\n        child = child-&gt;next;\n    }\n\n    out=(char*)malloc(len);\n\n    *out = '&#91;';\n    ptr = out + 1;\n    *ptr = 0;\n\n    for (i = 0; i &lt; numentries; i++) {\n        tmplen = strlen(entries&#91;i]);\n        memcpy(ptr, entries&#91;i], tmplen);\n        ptr += tmplen;\n\n        if (i != numentries - 1) {\n            *ptr++ = ',';\n            *ptr++ = ' ';\n            *ptr = 0;\n        }\n        free(entries&#91;i]);\n    }\n    free(entries);\n    *ptr++ = ']';\n    *ptr++ = 0;\n\n    return out; \n}<\/code><\/pre>\n\n\n\n<p><strong>\u51fd\u6570\u6838\u5fc3\u601d\u60f3<\/strong><\/p>\n\n\n\n<p><code>print_array()<\/code>\u51fd\u6570\u7684\u8bbe\u8ba1\u4e0e<code>print_object()<\/code>\u51fd\u6570\u7684\u8bbe\u8ba1\uff0c\u5982\u51fa\u4e00\u8f99\u3002\u5747\u662f<strong>\u89e3\u6790\u5173\u952e\u5143\u7d20<\/strong>\u3001<strong>\u5b58\u50a8\u5173\u952e\u5143\u7d20<\/strong>\u3001<strong>\u62fc\u63a5\u5173\u952e\u5143\u7d20<\/strong>\u4e09\u6b65\u8d70\u3002<\/p>\n\n\n\n<p><strong><code>print_array()<\/code>\u51fd\u6570<code>print_object()<\/code>\u51fd\u6570\u4e0e\u4e0d\u540c\u70b9<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>print_array()<\/code>\u51fd\u6570\u4e0d\u8bb0\u5f55\u5f53\u524d\u5c42\u6570\uff1b\u5728<code>print_object()<\/code>\u51fd\u6570\u4e2d\u8bb0\u5f55\u5c42\u6570\uff0c\u4e3b\u8981\u662f\u4e3a\u4e86\u5728\u62fc\u63a5\u5143\u7d20\u65f6\u5728\u6bcf\u4e00\u884c\u5f00\u5934\u8f93\u51fa\u5bf9\u5e94\u6570\u91cf\u7684<code>tab<\/code><\/li>\n\n\n\n<li><code>print_array()<\/code>\u51fd\u6570\u8bb0\u5f55\u6bcf\u4e2a\u6210\u5458\u7684\u540d\u79f0\uff0c\u7531\u4e8e<code>array<\/code>\u4e2d\u7684\u5143\u7d20\u5e76\u4e0d\u4ee5\u952e\u503c\u5bf9\u7684\u5f62\u5f0f\u5b58\u5728\u800c\u662f\u4e00\u4e2a\u4e2a\u72ec\u7acb\u7684\u5b57\u7b26\u4e32\u6216\u6570\u5b57\uff0c\u6240\u4ee5\u5e76\u4e0d\u9700\u8981\u533a\u5206\u5143\u7d20\u7684\u540d\u79f0\u548c\u5185\u5bb9\uff1b<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\"><code>print_number(item, 0)<\/code>\u51fd\u6570<\/h3>\n\n\n\n<p>\u53d6\u51fa<code>item-&gt;valuedouble<\/code>\u540e\u6839\u636e\u503c\u7684\uff0c\u6839\u636e\u4e0d\u540c\u7684\u7cbe\u5ea6\u5c06\u7ed3\u679c\u5199\u5165\u4e4b\u524d\u7533\u8bf7\u7684\u5185\u5b58\u4e2d<\/p>\n\n\n\n<p><strong>\u6838\u5fc3\u903b\u8f91\u5982\u4e0b<\/strong><\/p>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\">\n<p>\u7b14\u8005\u6ce8\uff1a\u4e0b\u6587\u4ee3\u7801\u5df2\u683c\u5f0f\u5316\u5904\u7406\uff0c\u5e76\u9002\u5f53\u7b80\u5316\u53ea\u4fdd\u7559\u6838\u5fc3\u903b\u8f91\uff1b\u6b64\u5904\u4ec5\u5217\u51fa<code>cJSON_Print<\/code>\u63a5\u53e3\u7684\u903b\u8f91\uff0c\u5373<code>print_number<\/code>\u7b2c\u4e8c\u4e2a\u53c2\u6570<code>printbuffer *p<\/code>\u4e3a<code>NULL<\/code>\u7684\u903b\u8f91\uff0c\u6545\u5220\u9664\u90e8\u5206\u5197\u4f59\u4ee3\u7801<\/p>\n<\/blockquote>\n\n\n\n<pre class=\"wp-block-code\"><code>\/* Render the number nicely from the given item into a string. *\/\nstatic char *print_number(cJSON *item, printbuffer *p)\n{\n    char *str = 0;\n    double d = item-&gt;valuedouble;\n\n    \/*\n     * \u6839\u636evaluedouble\u503c\u7684\u4e0d\u540c\uff0c\u5206\u914d\u4e0d\u540c\u957f\u5ea6\u7684\u5185\u5b58\u7a7a\u95f4\u5b58\u50a8\n     *\/\n    if (d == 0) {\n        str = (char*)malloc(2);\n        strcpy(str, \"0\");\n\n    } else if (fabs(((double)item-&gt;valueint) - d) &lt;= DBL_EPSILON &amp;&amp; (d &lt;= INT_MAX &amp;&amp; d &gt;= INT_MIN)) {\n        str = (char*)malloc(21);    \/* 2^64+1 can be represented in 21 chars. *\/\n        sprintf(str, \"%d\", item-&gt;valueint);\n\n    } else {\n        str = (char*)malloc(64);    \/* This is a nice tradeoff. *\/\n\n        if (fabs(floor(d) - d) &lt;= DBL_EPSILON &amp;&amp; fabs(d) &lt; 1.0e60) {\n            sprintf(str, \"%.0f\", d);\n        } else if (fabs(d) &lt; 1.0e-6 || fabs(d) &gt; 1.0e9)    {\n            sprintf(str, \"%e\", d);\n        } else {\n            sprintf(str, \"%f\", d);\n        }\n    }\n    return str;\n}<\/code><\/pre>\n\n\n\n<p><strong>\u51fd\u6570\u6838\u5fc3\u601d\u60f3<\/strong><\/p>\n\n\n\n<p>\u6839\u636e\u4e0d\u540c\u7684\u6570\u636e\u8303\u56f4\u5206\u914d\u4e0d\u540c\u957f\u5ea6\u7684\u5185\u5b58<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>valuedouble = 0<\/code>\u65f6\uff0c\u5206\u914d\u5185\u5b58\u957f\u5ea6\u4e3a2\u5b57\u8282<\/li>\n\n\n\n<li><code>INT_MIN &lt;= valuedouble &lt;= INT_MAX<\/code>\u65f6\uff0c\u5206\u914d21\u5b57\u8282<\/li>\n\n\n\n<li>\u66f4\u5927\u7684\u8303\u56f4\u65f6\uff0c\u5206\u914d64\u5b57\u8282<\/li>\n<\/ul>\n\n\n\n<p>\u6839\u636e\u4e0d\u540c\u7684\u6570\u636e\u8303\u56f4\u4f7f\u7528\u4e0d\u540c\u7684\u7c7b\u578b\u5b58\u50a8\u6570\u636e<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u5bf9\u4e8e<code>fabs(d) &lt; 1.0e60<\/code>\u8868\u793a <code>d<\/code> \u7684\u7edd\u5bf9\u503c\u5c0f\u4e8e <code>(1.0 \\times 10^{60})<\/code>\u3002\u5728\u53cc\u7cbe\u5ea6\u6d6e\u70b9\u6570\u8303\u56f4\u5185\uff0c\u4f7f\u7528\u820d\u5f03\u5c0f\u6570\u90e8\u5206\u53ea\u4fdd\u7559\u6574\u6570\u90e8\u5206<\/li>\n\n\n\n<li>\u5982\u679c\u53d8\u91cf <code>d<\/code> \u7684\u7edd\u5bf9\u503c\u5c0f\u4e8e <code>(1.0 \\times 10^{-6})<\/code> \u6216\u8005\u5927\u4e8e <code>(1.0 \\times 10^{9}<\/code>\uff0c\u5c06\u5176\u8f6c\u6362\u4e3a\u79d1\u5b66\u8ba1\u6570\u6cd5\u5b57\u7b26\u4e32\uff0c\u5e76\u5b58\u50a8\u5230\u5b57\u7b26\u4e32 <code>str<\/code> \u4e2d<\/li>\n\n\n\n<li>\u5bf9\u4e8e\u5176\u4ed6\u60c5\u51b5\u8f6c\u5316\u4e3a\u5341\u8fdb\u5236\u6d6e\u70b9\u6570\u5b58\u50a8<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\"><code>print_string(item, 0)<\/code>\u51fd\u6570<\/h3>\n\n\n\n<p>\u8bfb\u53d6<code>item<\/code>\u4e2d\u65e7\u5b57\u7b26\u4e32\uff0c\u7533\u8bf7\u5185\u5b58\u5b58\u653e\u65b0\u5b57\u7b26\u4e32\uff0c\u7136\u540e\u5c06\u4e00\u4e2a\u5b57\u7b26\u4e00\u4e2a\u5b57\u7b26\u7684\u65e7\u5b57\u7b26\u4e32\u4e2d\u7684\u5143\u7d20\u586b\u5145\u5230\u65b0\u5b57\u7b26\u4e32\u4e2d\uff0c\u5bf9\u4e8e\u8f6c\u4e49\u5b57\u7b26\u5219\u9700\u8981\u7279\u6b8a\u5904\u7406<\/p>\n\n\n\n<p><strong>\u6838\u5fc3\u903b\u8f91\u5982\u4e0b<\/strong><\/p>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\">\n<p>\u7b14\u8005\u6ce8\uff1a\u4e0b\u6587\u4ee3\u7801\u5df2\u683c\u5f0f\u5316\u5904\u7406\uff0c\u5e76\u9002\u5f53\u7b80\u5316\u53ea\u4fdd\u7559\u6838\u5fc3\u903b\u8f91\uff1b\u6b64\u5904\u4ec5\u5217\u51fa<code>cJSON_Print<\/code>\u63a5\u53e3\u7684\u903b\u8f91\uff0c\u5373<code>print_string<\/code>\u7b2c\u4e8c\u4e2a\u53c2\u6570<code>printbuffer *p<\/code>\u4e3a<code>NULL<\/code>\u7684\u903b\u8f91\uff0c\u6545\u5220\u9664\u90e8\u5206\u5197\u4f59\u4ee3\u7801<\/p>\n<\/blockquote>\n\n\n\n<pre class=\"wp-block-code\"><code>\/* Invote print_string_ptr (which is useful) on an item. *\/\nstatic char *print_string(cJSON *item, printbuffer *p)\n{\n    return print_string_ptr(item-&gt;valuestring, p);\n}\n\n\/* Render the cstring provided to an escaped version that can be printed. *\/\nstatic char *print_string_ptr(const char *str)\n{\n    const char *ptr;\n    char *ptr2, *out;\n    int len = 0;\n    unsigned char token;\n\n    ptr = str;\n    \/* \u8ba1\u7b97\u5f85\u8f93\u51fa\u5b57\u7b26\u4e32\u7684\u957f\u5ea6 *\/\n    while ((token =* ptr) &amp;&amp; ++len) {\n        if (strchr(\"\\\"\\\\\\b\\f\\n\\r\\t\", token)) {\n            len++;\n        } else if (token &lt; 32) {\n             \/* \u5f53\u524d\u5b57\u7b26\u7684ASCII\u503c\u5c0f\u4e8e32\uff0c\u8fd9\u4e9b\u5b57\u7b26\u4f1a\u88ab\u8f6c\u4e49\u4e3a\u7c7b\u4f3c\\uXXXX \u7684\u5f62\u5f0f\uff0c\u5360\u636e\u4e865\u4e2a\u5b57\u7b26\u7684\u7a7a\u95f4 *\/\n            len += 5;\n        }\n        ptr++;\n    }\n\n    out = (char*)malloc(len + 3);\n\n    \/*\n     * *ptr2++ = '\\\\';\u7b49\u4ef7\u4e8e\n     * *ptr2 = '\\\\';\n     *  ptr2++;\n     *\/\n    ptr2 = out;\n    ptr = str;\n    \/*\n     * \u5411\u4e4b\u524d\u5df2\u7533\u8bf7\u7684\u5185\u5b58\u7a7a\u95f4\u4e00\u4e2a\u5b57\u7b26\u4e00\u4e2a\u5b57\u7b26\u7684\u586b\u5145\u5b57\u7b26\u4e32\n     * \u5f00\u5934\u4e0e\u7ed3\u5c3e\u586b\u5145'\\\"'\u4e2d\u95f4\u90e8\u5206\u904d\u5386\u65e7\u5b57\u7b26\u4e32\u586b\u5145\u5230\u65b0\u5b57\u7b26\u4e32\u4e2d\uff0c\u5bf9\u4e8e\u8f6c\u4e49\u5b57\u7b26\u7279\u6b8a\u5904\u7406\n     *\/\n    *ptr2++ = '\\\"';\n    while (*ptr) {\n        if ((unsigned char)*ptr &gt; 31 &amp;&amp; *ptr != '\\\"' &amp;&amp; *ptr != '\\\\') {\n            *ptr2++ = *ptr++;\n        } else {\n            *ptr2++ = '\\\\';\n            switch (token = *ptr++) {\n                case '\\\\':\n                    *ptr2++ = '\\\\';\n                    break;\n                case '\\\"':\n                    *ptr2++ ='\\\"';\n                    break;\n                case '\\b':\n                    *ptr2++ = 'b';\n                    break;\n                case '\\f':\n                    *ptr2++ = 'f';\n                    break;\n                case '\\n':\n                    *ptr2++ ='n';\n                    break;\n                case '\\r':\n                    *ptr2++ = 'r';\n                    break;\n                case '\\t':\n                    *ptr2++ = 't';\n                    break;\n                default:\n                    sprintf(ptr2, \"u%04x\", token);\n                    ptr2 += 5;\n                    break;  \/* escape and print *\/\n            }\n        }\n    }\n    *ptr2++ = '\\\"';\n    *ptr2++ = 0;\n\n    return out;\n}<\/code><\/pre>\n\n\n\n<p><strong>\u51fd\u6570\u6838\u5fc3\u601d\u60f3<\/strong><\/p>\n\n\n\n<p>\u5bf9\u8f6c\u4e49\u5b57\u7b26\u7279\u6b8a\u5904\u7406<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">6. cJSON\u9500\u6bc1<\/h2>\n\n\n\n<p><strong>\u5728<code>cjson<\/code>\u6d41\u7a0b\u4e2d\u901a\u5e38\u4f1a\u521b\u5efa<code>struct cJSON<\/code>\u7684\u7ed3\u6784\u4f53\u7528\u4e8e\u5b58\u50a8\u89e3\u6790\u6216\u6784\u9020\u7684<code>json<\/code>\u5185\u5bb9\u3002\u5728\u6d41\u7a0b\u7ed3\u675f\u540e\u5219\u9700\u8981\u9500\u6bc1\u5bf9\u5e94\u7684\u6570\u636e\u7ed3\u6784\u51fd\u6570\u63a5\u53e3\u4e3a<code>void cJSON_Delete(cJSON *c)<\/code><\/strong><\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><code>cJSON_Delete()<\/code>\u51fd\u6570<\/h3>\n\n\n\n<p>\u904d\u5386\u6574\u4e2a\u94fe\u8868\uff0c\u5bf9\u4e8e\u94fe\u8868\u4e0a\u7684\u5b69\u5b50\u8282\u70b9\u505a\u9012\u5f52\u5904\u7406<\/p>\n\n\n\n<p><strong>\u6838\u5fc3\u903b\u8f91\u5982\u4e0b<\/strong><\/p>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\">\n<p>\u7b14\u8005\u6ce8\uff1a\u4e0b\u6587\u4ee3\u7801\u5df2\u683c\u5f0f\u5316\u5904\u7406\uff0c\u5e76\u9002\u5f53\u7b80\u5316\u53ea\u4fdd\u7559\u6838\u5fc3\u903b\u8f91<\/p>\n<\/blockquote>\n\n\n\n<pre class=\"wp-block-code\"><code>\/* Delete a cJSON structure. *\/\nvoid cJSON_Delete(cJSON *c)\n{\n    cJSON *next;\n\n    while (c) {\n        next = c-&gt;next;\n        if (!(c-&gt;type &amp; cJSON_IsReference) &amp;&amp; c-&gt;child) {\n            cJSON_Delete(c-&gt;child);\n        }\n\n        if (!(c-&gt;type &amp; cJSON_IsReference) &amp;&amp; c-&gt;valuestring) {\n            cJSON_free(c-&gt;valuestring);\n        }\n\n        if (!(c-&gt;type &amp; cJSON_StringIsConst) &amp;&amp; c-&gt;string) {\n            cJSON_free(c-&gt;string);\n        }\n\n        cJSON_free(c);\n        c = next;\n    }\n}<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>\u6e90\u7801\u4e0b\u8f7d\u94fe\u63a5https:\/\/sourceforge.net\/projects\/cjson\/ 1. \u6838\u5fc3\u6570\u636e\u7ed3\u6784 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_import_markdown_pro_load_document_selector":0,"_import_markdown_pro_submit_text_textarea":"","footnotes":""},"categories":[1],"tags":[],"class_list":["post-37","post","type-post","status-publish","format-standard","hentry","category-source-code-analysis"],"_links":{"self":[{"href":"https:\/\/www.garfield-blog.top\/index.php?rest_route=\/wp\/v2\/posts\/37","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.garfield-blog.top\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.garfield-blog.top\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.garfield-blog.top\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.garfield-blog.top\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=37"}],"version-history":[{"count":5,"href":"https:\/\/www.garfield-blog.top\/index.php?rest_route=\/wp\/v2\/posts\/37\/revisions"}],"predecessor-version":[{"id":252,"href":"https:\/\/www.garfield-blog.top\/index.php?rest_route=\/wp\/v2\/posts\/37\/revisions\/252"}],"wp:attachment":[{"href":"https:\/\/www.garfield-blog.top\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=37"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.garfield-blog.top\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=37"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.garfield-blog.top\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=37"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}