{"id":5396,"date":"2021-12-14T00:17:15","date_gmt":"2021-12-13T16:17:15","guid":{"rendered":"http:\/\/blog.qdac.cc\/?p=5396"},"modified":"2021-12-14T22:34:24","modified_gmt":"2021-12-14T14:34:24","slug":"%e6%95%99%e7%a8%8b%e4%bd%bf%e7%94%a8-tqjsonoutputchannel-%e5%86%99%e5%85%a5-json-%e6%95%b0%e6%8d%ae%e5%88%b0%e6%b5%81%e4%b8%ad","status":"publish","type":"post","link":"https:\/\/blog.qdac.cc\/?p=5396","title":{"rendered":"[\u6559\u7a0b]\u4f7f\u7528 TQJsonOutputChannel \u5199\u5165 JSON \u6570\u636e\u5230\u6d41\u4e2d"},"content":{"rendered":"\n<p>TQJsonOutputChannel \u7684\u4e3b\u8981\u4f5c\u7528\u662f\u7528\u6765\u5feb\u901f\u5199\u5165\u5927\u578b\u7684 JSON \u6570\u636e\u5230\u6587\u4ef6\u4e2d\uff0c\u6240\u4ee5\u6574\u4f53\u662f\u9762\u5411\u5199\u5165\u8fc7\u7a0b\u8bbe\u8ba1\u7684\uff0c\u4e0d\u8981\u8bd5\u56fe\u7528\u5b83\u6765\u63a7\u5236\u5185\u5bb9\u7684\u4fee\u6539\u3002<\/p>\n\n\n\n<p>\u5047\u8bbe\u6211\u4eec\u8981\u5c06\u6570\u636e\u5185\u5bb9\u5199\u5165\u5230\u76ee\u6807\u6570\u636e\u6d41 AStream \u4e2d\uff1a<\/p>\n\n\n\n<p>\u7b2c\u4e00\u6b65\uff1a\u6211\u4eec\u9700\u8981\u5b9a\u4e49\u4e00\u4e2a  IQJsonStreamEncoder \u7684\u5b9e\u4f8b\uff0c\u5b83\u662f\u4e00\u4e2a\u7ed3\u6784\u4f53\uff0c\u6240\u4ee5\u76f4\u63a5\u5728\u6808\u4e0a\u5b9a\u4e49\u8fd9\u4e2a\u53d8\u91cf\u5373\u53ef\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>var\n  AWriter: IQJsonStreamEncoder;<\/code><\/pre>\n\n\n\n<p>\u7b2c\u4e8c\u6b65\uff1a\u521b\u5efa\u5b9e\u4f8b\u5e76\u5efa\u7acb\u4e0e\u76ee\u6807\u6570\u636e\u6d41\u7684\u5173\u8054\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>AWriter := TQJsonOutputChannel.CreateEncoder(teUtf8, AStream);<\/code><\/pre>\n\n\n\n<p>\u8fd8\u6709\u4e00\u4e2a\u6700\u5927\u5c42\u6570\u9650\u5236\uff0c\u9ed8\u8ba4\u4e3a32\uff0c\u53ef\u4ee5\u901a\u8fc7\u7b2c\u4e09\u4e2a\u53c2\u6570\u4fee\u6539\u3002<\/p>\n\n\n\n<p>\u7b2c\u4e09\u6b65\uff1a\u5f00\u59cb\u6309\u7167\u6b63\u5e38\u7684\u89c4\u5219\u5199\u5165 JSON \u6570\u636e\u3002<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>\u8c03\u7528 BeginArray \u5f00\u59cb\u4e00\u4e2a\u6570\u7ec4\u5b9a\u4e49\uff0c\u5b83\u5fc5\u9700\u5728\u5143\u7d20\u8f93\u51fa\u5b8c\u6210\u540e\uff0c\u5bf9\u5e94\u4e00\u4e2a EndArray \u8c03\u7528\u6765\u6807\u5fd7\u6b64\u6570\u7ec4\u8f93\u51fa\u7ed3\u675f\u3002\u4e5f\u53ef\u4ee5\u4f7f\u7528 WriteArray \u6765\u5b9a\u4e49\u6570\u7ec4\u3002<\/li><li>\u8c03\u7528 BeginObject \u5f00\u59cb\u4e00\u4e2a\u5bf9\u8c61\u5b9a\u4e49\uff0c\u5b83\u5fc5\u9700\u5728\u5b50\u7ed3\u70b9\u8f93\u51fa\u5b8c\u6210\u540e\uff0c\u5bf9\u5e94\u4e00\u4e2a EndObject \u8c03\u7528\u6765\u6807\u5fd7\u6b64\u5bf9\u8c61\u8f93\u51fa\u7ed3\u675f\u3002\u4e5f\u53ef\u4ee5\u4f7f\u7528 WriteObject \u6765\u5b9a\u4e49\u5bf9\u8c61\u3002<\/li><li>\u8c03\u7528 WriteElement \u65b9\u6cd5\u5199\u5165\u6570\u7ec4\u5143\u7d20\u7684\u503c<\/li><li>\u8c03\u7528 WriteChild \u65b9\u6cd5\u5199\u5165\u666e\u901a\u7c7b\u578b\u7684\u503c<\/li><\/ul>\n\n\n\n<p>\u7b2c\u56db\u6b65\uff1a\u5df2\u7ecf\u5b8c\u4e8b\u4e86\uff0c\u8865\u5145\u4e00\u70b9\u5427\uff0c\u8fd9\u4e2a\u5199\u5165\u7684JSON\u662f\u4ee5\u538b\u7f29\u683c\u5f0f\u5199\u5165\u7684\uff0c\u6ca1\u6709\u52a0\u5165\u7f29\u8fdb\u652f\u6301\u3002\u53ef\u4ee5\u7528TQJsonInputChannel \u6765\u987a\u5e8f\u8bfb\u53d6\u3002<\/p>\n\n\n\n<p>\u6700\u540e\uff0c\u6765\u4e00\u6bb5\u793a\u4f8b\u4ee3\u7801\uff0cShow you my code:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>procedure TForm2.SpeedButton1Click(Sender: TObject);\nvar\n  AStream: TMemoryStream;\n  AJson: TQJson;\n  I: Integer;\n  AWriter: IQJsonStreamEncoder;\nbegin\n  AJson := AcquireJson;\n  AStream := TMemoryStream.Create;\n  try\n    AJson.Add('O').Add('01');\n    AJson.AddArray('A');\n    AWriter := TQJsonOutputChannel.CreateEncoder(teUtf8, AStream);\n    AWriter.BeginArray('');\n    for I := 0 to 9 do\n    begin\n      AWriter.BeginObject('');\n      AWriter.WriteChild('Ident', 'Json Stream Writer');\n      AWriter.WriteChild('IntValue', I);\n      AWriter.WriteChild('FloatValue', Double(1.333 * I));\n      AWriter.WriteChild('BoolValue', (I mod 2) = 1);\n      AWriter.WriteChild('NullValue');\n      AWriter.WriteChild('DateTime', Now);\n      AJson.ForcePath('O.01').AsString := I.ToString;\n      AWriter.WriteChild('Json', AJson);\n      AWriter.EndObject;\n    end;\n    AWriter.EndArray;\n    AStream.Position := 0;\n    repeat\n      AJson.LoadFromStream(AStream, teUtf8);\n      ShowMessage(AJson.Encode(true));\n    until AStream.Position = AStream.Size;\n\n  finally\n    ReleaseJson(AJson);\n    FreeAndNil(AStream);\n  end;\nend;\n\nprocedure TForm2.SpeedButton2Click(Sender: TObject);\nvar\n  AStream: TMemoryStream;\n  AJson: TQJson;\n  I: Integer;\n  AWriter: IQJsonStreamEncoder;\nbegin\n  AJson := AcquireJson;\n  AStream := TMemoryStream.Create;\n  try\n    AJson.Add('O').Add('01');\n    AJson.AddArray('A');\n    AWriter := TQJsonOutputChannel.CreateEncoder(teUtf8, AStream);\n    AWriter.WriteArray('',\n      procedure(AParent: IQJsonArrayEncoder)\n      var\n        I: Integer;\n      begin\n        for I := 0 to 9 do\n        begin\n          AParent.WriteObject(\n            procedure(AObjParent: IQJsonObjectEncoder)\n            begin\n              AObjParent.Write('Ident', 'Json Stream Writer');\n              AObjParent.Write('IntValue', I);\n              AObjParent.Write('FloatValue', Double(1.333 * I));\n              AObjParent.Write('BoolValue', (I mod 2) = 1);\n              AObjParent.Write('NullValue');\n              AObjParent.Write('DateTime', Now);\n              AJson.ForcePath('O.01').AsString := I.ToString;\n              AObjParent.Write('Json', AJson);\n            end);\n        end\n      end);\n    AStream.Position := 0;\n    repeat\n      AJson.LoadFromStream(AStream, teUtf8);\n      ShowMessage(AJson.Encode(true));\n    until AStream.Position = AStream.Size;\n  finally\n    ReleaseJson(AJson);\n    FreeAndNil(AStream);\n  end;\nend;<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>TQJsonOutputChannel  [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"site-sidebar-layout":"default","site-content-layout":"","ast-site-content-layout":"default","site-content-style":"default","site-sidebar-style":"default","ast-global-header-display":"","ast-banner-title-visibility":"","ast-main-header-display":"","ast-hfb-above-header-display":"","ast-hfb-below-header-display":"","ast-hfb-mobile-header-display":"","site-post-title":"","ast-breadcrumbs-content":"","ast-featured-img":"","footer-sml-layout":"","ast-disable-related-posts":"","theme-transparent-header-meta":"","adv-header-id-meta":"","stick-header-meta":"","header-above-stick-meta":"","header-main-stick-meta":"","header-below-stick-meta":"","astra-migrate-meta-layouts":"default","ast-page-background-enabled":"default","ast-page-background-meta":{"desktop":{"background-color":"var(--ast-global-color-4)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"tablet":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"mobile":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""}},"ast-content-background-meta":{"desktop":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"tablet":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"mobile":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""}},"footnotes":""},"categories":[8],"tags":[],"class_list":["post-5396","post","type-post","status-publish","format-standard","hentry","category-delphi"],"views":1601,"_links":{"self":[{"href":"https:\/\/blog.qdac.cc\/index.php?rest_route=\/wp\/v2\/posts\/5396","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blog.qdac.cc\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.qdac.cc\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.qdac.cc\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.qdac.cc\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=5396"}],"version-history":[{"count":3,"href":"https:\/\/blog.qdac.cc\/index.php?rest_route=\/wp\/v2\/posts\/5396\/revisions"}],"predecessor-version":[{"id":5402,"href":"https:\/\/blog.qdac.cc\/index.php?rest_route=\/wp\/v2\/posts\/5396\/revisions\/5402"}],"wp:attachment":[{"href":"https:\/\/blog.qdac.cc\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=5396"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.qdac.cc\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=5396"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.qdac.cc\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=5396"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}