From 926b0f2c7116bf887e9b884a6147e020964bcb10 Mon Sep 17 00:00:00 2001 From: zhengshuxin Date: Sun, 21 Jul 2019 20:08:51 +0800 Subject: [PATCH] modify json sample --- lib_acl/samples/json/json6/main.cpp | 48 ++++++++++++++++++++--------- 1 file changed, 33 insertions(+), 15 deletions(-) diff --git a/lib_acl/samples/json/json6/main.cpp b/lib_acl/samples/json/json6/main.cpp index 63f904d0b..b3cffd4a2 100644 --- a/lib_acl/samples/json/json6/main.cpp +++ b/lib_acl/samples/json/json6/main.cpp @@ -8,26 +8,26 @@ static const char* default_data = \ " 'value{': 'File',\r\n" " 'popup{}': {\r\n" " 'menuitem1}': [\r\n" - " {'value': 'New', 'onclick': 'CreateNewDoc()'},\r\n" - " {'value': 'Open', 'onclick': 'OpenDoc()'},\r\n" - " {'value': 'Close', 'onclick': 'CloseDoc()'}\r\n" + " {'value': 'New1', 'onclick': 'CreateNewDoc()'},\r\n" + " {'value': 'Open1', 'onclick': 'OpenDoc()'},\r\n" + " {'value': 'Close1', 'onclick': 'CloseDoc()'}\r\n" " ],\r\n" " 'menuname[]': 'hello world',\r\n" - " 'inner': { 'value' : 'new ', 'value' : 'open' },\r\n" + " 'inner': { 'value' : 'new2 ', 'value' : 'open2' },\r\n" " 'menuitem2': [\r\n" - " {'value': 'New', 'onclick': 'CreateNewDoc()'},\r\n" - " {'value': 'Open', 'onclick': 'OpenDoc()'},\r\n" - " {'value': 'Close', 'onclick': 'CloseDoc()'},\r\n" - " {{'value': 'Help', 'onclick': 'Help()'}}" + " {'value': 'New3', 'onclick': 'CreateNewDoc()'},\r\n" + " {'value': 'Open3', 'onclick': 'OpenDoc()'},\r\n" + " {'value': 'Close3', 'onclick': 'CloseDoc()'},\r\n" + " {{'value': 'Help3', 'onclick': 'Help()'}}" " ]\r\n" " }\r\n" " }\r\n," " 'help': 'hello world!',\r\n" " 'menuitem2': [\r\n" - " {'value': 'New', 'onclick': 'CreateNewDoc()'},\r\n" - " {'value': 'Open', 'onclick': 'OpenDoc()'},\r\n" - " {'value': 'Close', 'onclick': 'CloseDoc()'},\r\n" - " [{'value': 'Save', 'onclick': 'SaveDoc()'}]" + " {'value': 'New4', 'onclick': 'CreateNewDoc()'},\r\n" + " {'value': 'Open4', 'onclick': 'OpenDoc()'},\r\n" + " {'value': 'Close4', 'onclick': 'CloseDoc()'},\r\n" + " [{'value': 'Save4', 'onclick': 'SaveDoc()'}]" " ]\r\n" "}\r\n" "{ 'hello world' }\r\n"; @@ -40,14 +40,32 @@ static void test_json(void) ACL_VSTRING *buf = acl_vstring_alloc(1024); ptr = acl_json_update(json, ptr); + + printf("-------------------------------------------------------\r\n"); printf("finish: %s, left char: %s\r\n", acl_json_finish(json) ? "yes" : "no", ptr); + printf("-------------------------------------------------------\r\n"); + printf("\r\n%s\r\n", default_data); + printf("-------------------------------------------------------\r\n"); + printf("\r\n%s\r\n", acl_vstring_str(buf)); + printf("-------------------------------------------------------\r\n"); + + ACL_ARRAY *a = acl_json_getElementsByTags(json, "menuitem2/value"); + if (a) { + ACL_ITER iter; + + acl_foreach(iter, a) { + ACL_JSON_NODE* node = (ACL_JSON_NODE*) iter.data; + printf("tag=%s, value=%s\r\n", + acl_vstring_str(node->ltag), + acl_vstring_str(node->text)); + } + + acl_json_free_array(a); + } acl_json_build(json, buf); acl_json_free(json); - - printf("\r\n%s\r\n", default_data); - printf("\r\n%s\r\n", acl_vstring_str(buf)); acl_vstring_free(buf); }