github.com/gogf/gf/v2@v2.7.4/encoding/gjson/gjson_z_example_test.go (about) 1 package gjson_test 2 3 import ( 4 "fmt" 5 6 "github.com/gogf/gf/v2/encoding/gjson" 7 ) 8 9 func ExampleJson_SetSplitChar() { 10 data := 11 `{ 12 "users" : { 13 "count" : 2, 14 "list" : [ 15 {"name" : "Ming", "score" : 60}, 16 {"name" : "John", "score" : 99.5} 17 ] 18 } 19 }` 20 if j, err := gjson.DecodeToJson(data); err != nil { 21 panic(err) 22 } else { 23 j.SetSplitChar('#') 24 fmt.Println("John Score:", j.Get("users#list#1#score").Float32()) 25 } 26 // Output: 27 // John Score: 99.5 28 } 29 30 func ExampleJson_SetViolenceCheck() { 31 data := 32 `{ 33 "users" : { 34 "count" : 100 35 }, 36 "users.count" : 101 37 }` 38 if j, err := gjson.DecodeToJson(data); err != nil { 39 fmt.Println(err) 40 } else { 41 j.SetViolenceCheck(false) 42 fmt.Println("Users Count:", j.Get("users.count")) 43 j.SetViolenceCheck(true) 44 fmt.Println("Users Count:", j.Get("users.count")) 45 } 46 // Output: 47 // Users Count: 100 48 // Users Count: 101 49 } 50 51 // ======================================================================== 52 // JSON 53 // ======================================================================== 54 func ExampleJson_ToJson() { 55 type BaseInfo struct { 56 Name string 57 Age int 58 } 59 60 info := BaseInfo{ 61 Name: "John", 62 Age: 18, 63 } 64 65 j := gjson.New(info) 66 jsonBytes, _ := j.ToJson() 67 fmt.Println(string(jsonBytes)) 68 69 // Output: 70 // {"Age":18,"Name":"John"} 71 } 72 73 func ExampleJson_ToJsonString() { 74 type BaseInfo struct { 75 Name string 76 Age int 77 } 78 79 info := BaseInfo{ 80 Name: "John", 81 Age: 18, 82 } 83 84 j := gjson.New(info) 85 jsonStr, _ := j.ToJsonString() 86 fmt.Println(jsonStr) 87 88 // Output: 89 // {"Age":18,"Name":"John"} 90 } 91 92 func ExampleJson_ToJsonIndent() { 93 type BaseInfo struct { 94 Name string 95 Age int 96 } 97 98 info := BaseInfo{ 99 Name: "John", 100 Age: 18, 101 } 102 103 j := gjson.New(info) 104 jsonBytes, _ := j.ToJsonIndent() 105 fmt.Println(string(jsonBytes)) 106 107 // Output: 108 // { 109 // "Age": 18, 110 // "Name": "John" 111 // } 112 } 113 114 func ExampleJson_ToJsonIndentString() { 115 type BaseInfo struct { 116 Name string 117 Age int 118 } 119 120 info := BaseInfo{ 121 Name: "John", 122 Age: 18, 123 } 124 125 j := gjson.New(info) 126 jsonStr, _ := j.ToJsonIndentString() 127 fmt.Println(jsonStr) 128 129 // Output: 130 // { 131 // "Age": 18, 132 // "Name": "John" 133 // } 134 } 135 136 func ExampleJson_MustToJson() { 137 type BaseInfo struct { 138 Name string 139 Age int 140 } 141 142 info := BaseInfo{ 143 Name: "John", 144 Age: 18, 145 } 146 147 j := gjson.New(info) 148 jsonBytes := j.MustToJson() 149 fmt.Println(string(jsonBytes)) 150 151 // Output: 152 // {"Age":18,"Name":"John"} 153 } 154 155 func ExampleJson_MustToJsonString() { 156 type BaseInfo struct { 157 Name string 158 Age int 159 } 160 161 info := BaseInfo{ 162 Name: "John", 163 Age: 18, 164 } 165 166 j := gjson.New(info) 167 jsonStr := j.MustToJsonString() 168 fmt.Println(jsonStr) 169 170 // Output: 171 // {"Age":18,"Name":"John"} 172 } 173 174 func ExampleJson_MustToJsonIndent() { 175 type BaseInfo struct { 176 Name string 177 Age int 178 } 179 180 info := BaseInfo{ 181 Name: "John", 182 Age: 18, 183 } 184 185 j := gjson.New(info) 186 jsonBytes := j.MustToJsonIndent() 187 fmt.Println(string(jsonBytes)) 188 189 // Output: 190 // { 191 // "Age": 18, 192 // "Name": "John" 193 // } 194 } 195 196 func ExampleJson_MustToJsonIndentString() { 197 type BaseInfo struct { 198 Name string 199 Age int 200 } 201 202 info := BaseInfo{ 203 Name: "John", 204 Age: 18, 205 } 206 207 j := gjson.New(info) 208 jsonStr := j.MustToJsonIndentString() 209 fmt.Println(jsonStr) 210 211 // Output: 212 // { 213 // "Age": 18, 214 // "Name": "John" 215 // } 216 } 217 218 // ======================================================================== 219 // XML 220 // ======================================================================== 221 func ExampleJson_ToXml() { 222 type BaseInfo struct { 223 Name string 224 Age int 225 } 226 227 info := BaseInfo{ 228 Name: "John", 229 Age: 18, 230 } 231 232 j := gjson.New(info) 233 xmlBytes, _ := j.ToXml() 234 fmt.Println(string(xmlBytes)) 235 236 // Output: 237 // <doc><Age>18</Age><Name>John</Name></doc> 238 } 239 240 func ExampleJson_ToXmlString() { 241 type BaseInfo struct { 242 Name string 243 Age int 244 } 245 246 info := BaseInfo{ 247 Name: "John", 248 Age: 18, 249 } 250 251 j := gjson.New(info) 252 xmlStr, _ := j.ToXmlString() 253 fmt.Println(string(xmlStr)) 254 255 // Output: 256 // <doc><Age>18</Age><Name>John</Name></doc> 257 } 258 259 func ExampleJson_ToXmlIndent() { 260 type BaseInfo struct { 261 Name string 262 Age int 263 } 264 265 info := BaseInfo{ 266 Name: "John", 267 Age: 18, 268 } 269 270 j := gjson.New(info) 271 xmlBytes, _ := j.ToXmlIndent() 272 fmt.Println(string(xmlBytes)) 273 274 // Output: 275 // <doc> 276 // <Age>18</Age> 277 // <Name>John</Name> 278 // </doc> 279 } 280 281 func ExampleJson_ToXmlIndentString() { 282 type BaseInfo struct { 283 Name string 284 Age int 285 } 286 287 info := BaseInfo{ 288 Name: "John", 289 Age: 18, 290 } 291 292 j := gjson.New(info) 293 xmlStr, _ := j.ToXmlIndentString() 294 fmt.Println(string(xmlStr)) 295 296 // Output: 297 // <doc> 298 // <Age>18</Age> 299 // <Name>John</Name> 300 // </doc> 301 } 302 303 func ExampleJson_MustToXml() { 304 type BaseInfo struct { 305 Name string 306 Age int 307 } 308 309 info := BaseInfo{ 310 Name: "John", 311 Age: 18, 312 } 313 314 j := gjson.New(info) 315 xmlBytes := j.MustToXml() 316 fmt.Println(string(xmlBytes)) 317 318 // Output: 319 // <doc><Age>18</Age><Name>John</Name></doc> 320 } 321 322 func ExampleJson_MustToXmlString() { 323 type BaseInfo struct { 324 Name string 325 Age int 326 } 327 328 info := BaseInfo{ 329 Name: "John", 330 Age: 18, 331 } 332 333 j := gjson.New(info) 334 xmlStr := j.MustToXmlString() 335 fmt.Println(string(xmlStr)) 336 337 // Output: 338 // <doc><Age>18</Age><Name>John</Name></doc> 339 } 340 341 func ExampleJson_MustToXmlIndent() { 342 type BaseInfo struct { 343 Name string 344 Age int 345 } 346 347 info := BaseInfo{ 348 Name: "John", 349 Age: 18, 350 } 351 352 j := gjson.New(info) 353 xmlBytes := j.MustToXmlIndent() 354 fmt.Println(string(xmlBytes)) 355 356 // Output: 357 // <doc> 358 // <Age>18</Age> 359 // <Name>John</Name> 360 // </doc> 361 } 362 363 func ExampleJson_MustToXmlIndentString() { 364 type BaseInfo struct { 365 Name string 366 Age int 367 } 368 369 info := BaseInfo{ 370 Name: "John", 371 Age: 18, 372 } 373 374 j := gjson.New(info) 375 xmlStr := j.MustToXmlIndentString() 376 fmt.Println(string(xmlStr)) 377 378 // Output: 379 // <doc> 380 // <Age>18</Age> 381 // <Name>John</Name> 382 // </doc> 383 } 384 385 // ======================================================================== 386 // YAML 387 // ======================================================================== 388 func ExampleJson_ToYaml() { 389 type BaseInfo struct { 390 Name string 391 Age int 392 } 393 394 info := BaseInfo{ 395 Name: "John", 396 Age: 18, 397 } 398 399 j := gjson.New(info) 400 YamlBytes, _ := j.ToYaml() 401 fmt.Println(string(YamlBytes)) 402 403 // Output: 404 // Age: 18 405 // Name: John 406 } 407 408 func ExampleJson_ToYamlString() { 409 type BaseInfo struct { 410 Name string 411 Age int 412 } 413 414 info := BaseInfo{ 415 Name: "John", 416 Age: 18, 417 } 418 419 j := gjson.New(info) 420 YamlStr, _ := j.ToYamlString() 421 fmt.Println(string(YamlStr)) 422 423 // Output: 424 // Age: 18 425 // Name: John 426 } 427 428 func ExampleJson_ToYamlIndent() { 429 type BaseInfo struct { 430 Name string 431 Age int 432 } 433 434 info := BaseInfo{ 435 Name: "John", 436 Age: 18, 437 } 438 439 j := gjson.New(info) 440 YamlBytes, _ := j.ToYamlIndent("") 441 fmt.Println(string(YamlBytes)) 442 443 // Output: 444 // Age: 18 445 // Name: John 446 } 447 448 func ExampleJson_MustToYaml() { 449 type BaseInfo struct { 450 Name string 451 Age int 452 } 453 454 info := BaseInfo{ 455 Name: "John", 456 Age: 18, 457 } 458 459 j := gjson.New(info) 460 YamlBytes := j.MustToYaml() 461 fmt.Println(string(YamlBytes)) 462 463 // Output: 464 // Age: 18 465 // Name: John 466 } 467 468 func ExampleJson_MustToYamlString() { 469 type BaseInfo struct { 470 Name string 471 Age int 472 } 473 474 info := BaseInfo{ 475 Name: "John", 476 Age: 18, 477 } 478 479 j := gjson.New(info) 480 YamlStr := j.MustToYamlString() 481 fmt.Println(string(YamlStr)) 482 483 // Output: 484 // Age: 18 485 // Name: John 486 } 487 488 // ======================================================================== 489 // TOML 490 // ======================================================================== 491 func ExampleJson_ToToml() { 492 type BaseInfo struct { 493 Name string 494 Age int 495 } 496 497 info := BaseInfo{ 498 Name: "John", 499 Age: 18, 500 } 501 502 j := gjson.New(info) 503 TomlBytes, _ := j.ToToml() 504 fmt.Println(string(TomlBytes)) 505 506 // Output: 507 // Age = 18 508 // Name = "John" 509 } 510 511 func ExampleJson_ToTomlString() { 512 type BaseInfo struct { 513 Name string 514 Age int 515 } 516 517 info := BaseInfo{ 518 Name: "John", 519 Age: 18, 520 } 521 522 j := gjson.New(info) 523 TomlStr, _ := j.ToTomlString() 524 fmt.Println(string(TomlStr)) 525 526 // Output: 527 // Age = 18 528 // Name = "John" 529 } 530 531 func ExampleJson_MustToToml() { 532 type BaseInfo struct { 533 Name string 534 Age int 535 } 536 537 info := BaseInfo{ 538 Name: "John", 539 Age: 18, 540 } 541 542 j := gjson.New(info) 543 TomlBytes := j.MustToToml() 544 fmt.Println(string(TomlBytes)) 545 546 // Output: 547 // Age = 18 548 // Name = "John" 549 } 550 551 func ExampleJson_MustToTomlString() { 552 type BaseInfo struct { 553 Name string 554 Age int 555 } 556 557 info := BaseInfo{ 558 Name: "John", 559 Age: 18, 560 } 561 562 j := gjson.New(info) 563 TomlStr := j.MustToTomlString() 564 fmt.Println(string(TomlStr)) 565 566 // Output: 567 // Age = 18 568 // Name = "John" 569 } 570 571 // ======================================================================== 572 // INI 573 // ======================================================================== 574 func ExampleJson_ToIni() { 575 type BaseInfo struct { 576 Name string 577 Age int 578 } 579 580 info := BaseInfo{ 581 Name: "John", 582 Age: 18, 583 } 584 585 j := gjson.New(info) 586 IniBytes, _ := j.ToIni() 587 fmt.Println(string(IniBytes)) 588 589 // May Output: 590 // Name=John 591 // Age=18 592 } 593 594 func ExampleJson_ToIniString() { 595 type BaseInfo struct { 596 Name string 597 } 598 599 info := BaseInfo{ 600 Name: "John", 601 } 602 603 j := gjson.New(info) 604 IniStr, _ := j.ToIniString() 605 fmt.Println(string(IniStr)) 606 607 // Output: 608 // Name=John 609 } 610 611 func ExampleJson_MustToIni() { 612 type BaseInfo struct { 613 Name string 614 } 615 616 info := BaseInfo{ 617 Name: "John", 618 } 619 620 j := gjson.New(info) 621 IniBytes := j.MustToIni() 622 fmt.Println(string(IniBytes)) 623 624 // Output: 625 // Name=John 626 } 627 628 func ExampleJson_MustToIniString() { 629 type BaseInfo struct { 630 Name string 631 } 632 633 info := BaseInfo{ 634 Name: "John", 635 } 636 637 j := gjson.New(info) 638 IniStr := j.MustToIniString() 639 fmt.Println(string(IniStr)) 640 641 // Output: 642 // Name=John 643 } 644 645 // ======================================================================== 646 // Properties 647 // ======================================================================== 648 func ExampleJson_ToProperties() { 649 type BaseInfo struct { 650 Name string 651 Age int 652 } 653 654 info := BaseInfo{ 655 Name: "John", 656 Age: 18, 657 } 658 659 j := gjson.New(info) 660 pr, _ := j.ToProperties() 661 fmt.Println(string(pr)) 662 663 // May Output: 664 // name = John 665 // age = 18 666 } 667 668 func ExampleJson_ToPropertiesString() { 669 type BaseInfo struct { 670 Name string 671 } 672 673 info := BaseInfo{ 674 Name: "John", 675 } 676 677 j := gjson.New(info) 678 pr, _ := j.ToPropertiesString() 679 fmt.Println(pr) 680 681 // Output: 682 // name = John 683 } 684 685 func ExampleJson_MustToProperties() { 686 type BaseInfo struct { 687 Name string 688 } 689 690 info := BaseInfo{ 691 Name: "John", 692 } 693 694 j := gjson.New(info) 695 pr := j.MustToProperties() 696 fmt.Println(string(pr)) 697 698 // Output: 699 // name = John 700 } 701 702 func ExampleJson_MustToPropertiesString() { 703 type BaseInfo struct { 704 Name string 705 } 706 707 info := BaseInfo{ 708 Name: "John", 709 } 710 711 j := gjson.New(info) 712 pr := j.MustToPropertiesString() 713 fmt.Println(pr) 714 715 // Output: 716 // name = John 717 } 718 719 func ExampleJson_MarshalJSON() { 720 type BaseInfo struct { 721 Name string 722 Age int 723 } 724 725 info := BaseInfo{ 726 Name: "John", 727 Age: 18, 728 } 729 730 j := gjson.New(info) 731 jsonBytes, _ := j.MarshalJSON() 732 fmt.Println(string(jsonBytes)) 733 734 // Output: 735 // {"Age":18,"Name":"John"} 736 } 737 738 func ExampleJson_UnmarshalJSON() { 739 jsonStr := `{"Age":18,"Name":"John"}` 740 741 j := gjson.New("") 742 j.UnmarshalJSON([]byte(jsonStr)) 743 fmt.Println(j.Map()) 744 745 // Output: 746 // map[Age:18 Name:John] 747 } 748 749 func ExampleJson_UnmarshalValue_Yaml() { 750 yamlContent := 751 `base: 752 name: john 753 score: 100` 754 755 j := gjson.New("") 756 j.UnmarshalValue([]byte(yamlContent)) 757 fmt.Println(j.Var().String()) 758 759 // Output: 760 // {"base":{"name":"john","score":100}} 761 } 762 763 func ExampleJson_UnmarshalValue_Xml() { 764 xmlStr := `<?xml version="1.0" encoding="UTF-8"?><doc><name>john</name><score>100</score></doc>` 765 766 j := gjson.New("") 767 j.UnmarshalValue([]byte(xmlStr)) 768 fmt.Println(j.Var().String()) 769 770 // Output: 771 // {"doc":{"name":"john","score":"100"}} 772 } 773 774 func ExampleJson_MapStrAny() { 775 type BaseInfo struct { 776 Name string 777 Age int 778 } 779 780 info := BaseInfo{ 781 Name: "John", 782 Age: 18, 783 } 784 785 j := gjson.New(info) 786 fmt.Println(j.MapStrAny()) 787 788 // Output: 789 // map[Age:18 Name:John] 790 } 791 792 func ExampleJson_Interfaces() { 793 type BaseInfo struct { 794 Name string 795 Age int 796 } 797 798 infoList := []BaseInfo{ 799 { 800 Name: "John", 801 Age: 18, 802 }, 803 { 804 Name: "Tom", 805 Age: 20, 806 }, 807 } 808 809 j := gjson.New(infoList) 810 fmt.Println(j.Interfaces()) 811 812 // Output: 813 // [{John 18} {Tom 20}] 814 } 815 816 func ExampleJson_Interface() { 817 type BaseInfo struct { 818 Name string 819 Age int 820 } 821 822 info := BaseInfo{ 823 Name: "John", 824 Age: 18, 825 } 826 827 j := gjson.New(info) 828 fmt.Println(j.Interface()) 829 830 var nilJ *gjson.Json = nil 831 fmt.Println(nilJ.Interface()) 832 833 // Output: 834 // map[Age:18 Name:John] 835 // <nil> 836 } 837 838 func ExampleJson_Var() { 839 type BaseInfo struct { 840 Name string 841 Age int 842 } 843 844 info := BaseInfo{ 845 Name: "John", 846 Age: 18, 847 } 848 849 j := gjson.New(info) 850 fmt.Println(j.Var().String()) 851 fmt.Println(j.Var().Map()) 852 853 // Output: 854 // {"Age":18,"Name":"John"} 855 // map[Age:18 Name:John] 856 } 857 858 func ExampleJson_IsNil() { 859 data1 := []byte(`{"n":123456789, "m":{"k":"v"}, "a":[1,2,3]}`) 860 data2 := []byte(`{"n":123456789, "m":{"k":"v"}, "a":[1,2,3]`) 861 862 j1, _ := gjson.LoadContent(data1) 863 fmt.Println(j1.IsNil()) 864 865 j2, _ := gjson.LoadContent(data2) 866 fmt.Println(j2.IsNil()) 867 868 // Output: 869 // false 870 // true 871 } 872 873 func ExampleJson_Get() { 874 data := 875 `{ 876 "users" : { 877 "count" : 1, 878 "array" : ["John", "Ming"] 879 } 880 }` 881 882 j, _ := gjson.LoadContent(data) 883 fmt.Println(j.Get(".")) 884 fmt.Println(j.Get("users")) 885 fmt.Println(j.Get("users.count")) 886 fmt.Println(j.Get("users.array")) 887 888 var nilJ *gjson.Json = nil 889 fmt.Println(nilJ.Get(".")) 890 891 // Output: 892 // {"users":{"array":["John","Ming"],"count":1}} 893 // {"array":["John","Ming"],"count":1} 894 // 1 895 // ["John","Ming"] 896 } 897 898 func ExampleJson_GetJson() { 899 data := 900 `{ 901 "users" : { 902 "count" : 1, 903 "array" : ["John", "Ming"] 904 } 905 }` 906 907 j, _ := gjson.LoadContent(data) 908 909 fmt.Println(j.GetJson("users.array").Array()) 910 911 // Output: 912 // [John Ming] 913 } 914 915 func ExampleJson_GetJsons() { 916 data := 917 `{ 918 "users" : { 919 "count" : 3, 920 "array" : [{"Age":18,"Name":"John"}, {"Age":20,"Name":"Tom"}] 921 } 922 }` 923 924 j, _ := gjson.LoadContent(data) 925 926 jsons := j.GetJsons("users.array") 927 for _, json := range jsons { 928 fmt.Println(json.Interface()) 929 } 930 931 // Output: 932 // map[Age:18 Name:John] 933 // map[Age:20 Name:Tom] 934 } 935 936 func ExampleJson_GetJsonMap() { 937 data := 938 `{ 939 "users" : { 940 "count" : 1, 941 "array" : { 942 "info" : {"Age":18,"Name":"John"}, 943 "addr" : {"City":"Chengdu","Company":"Tencent"} 944 } 945 } 946 }` 947 948 j, _ := gjson.LoadContent(data) 949 950 jsonMap := j.GetJsonMap("users.array") 951 952 for _, json := range jsonMap { 953 fmt.Println(json.Interface()) 954 } 955 956 // May Output: 957 // map[City:Chengdu Company:Tencent] 958 // map[Age:18 Name:John] 959 } 960 961 func ExampleJson_Set() { 962 type BaseInfo struct { 963 Name string 964 Age int 965 } 966 967 info := BaseInfo{ 968 Name: "John", 969 Age: 18, 970 } 971 972 j := gjson.New(info) 973 j.Set("Addr", "ChengDu") 974 j.Set("Friends.0", "Tom") 975 fmt.Println(j.Var().String()) 976 977 // Output: 978 // {"Addr":"ChengDu","Age":18,"Friends":["Tom"],"Name":"John"} 979 } 980 981 func ExampleJson_MustSet() { 982 type BaseInfo struct { 983 Name string 984 Age int 985 } 986 987 info := BaseInfo{ 988 Name: "John", 989 Age: 18, 990 } 991 992 j := gjson.New(info) 993 j.MustSet("Addr", "ChengDu") 994 fmt.Println(j.Var().String()) 995 996 // Output: 997 // {"Addr":"ChengDu","Age":18,"Name":"John"} 998 } 999 1000 func ExampleJson_Remove() { 1001 type BaseInfo struct { 1002 Name string 1003 Age int 1004 } 1005 1006 info := BaseInfo{ 1007 Name: "John", 1008 Age: 18, 1009 } 1010 1011 j := gjson.New(info) 1012 j.Remove("Age") 1013 fmt.Println(j.Var().String()) 1014 1015 // Output: 1016 // {"Name":"John"} 1017 } 1018 1019 func ExampleJson_MustRemove() { 1020 type BaseInfo struct { 1021 Name string 1022 Age int 1023 } 1024 1025 info := BaseInfo{ 1026 Name: "John", 1027 Age: 18, 1028 } 1029 1030 j := gjson.New(info) 1031 j.MustRemove("Age") 1032 fmt.Println(j.Var().String()) 1033 1034 // Output: 1035 // {"Name":"John"} 1036 } 1037 1038 func ExampleJson_Contains() { 1039 type BaseInfo struct { 1040 Name string 1041 Age int 1042 } 1043 1044 info := BaseInfo{ 1045 Name: "John", 1046 Age: 18, 1047 } 1048 1049 j := gjson.New(info) 1050 fmt.Println(j.Contains("Age")) 1051 fmt.Println(j.Contains("Addr")) 1052 1053 // Output: 1054 // true 1055 // false 1056 } 1057 1058 func ExampleJson_Len() { 1059 data := 1060 `{ 1061 "users" : { 1062 "count" : 1, 1063 "nameArray" : ["Join", "Tom"], 1064 "infoMap" : { 1065 "name" : "Join", 1066 "age" : 18, 1067 "addr" : "ChengDu" 1068 } 1069 } 1070 }` 1071 1072 j, _ := gjson.LoadContent(data) 1073 1074 fmt.Println(j.Len("users.nameArray")) 1075 fmt.Println(j.Len("users.infoMap")) 1076 1077 // Output: 1078 // 2 1079 // 3 1080 } 1081 1082 func ExampleJson_Append() { 1083 data := 1084 `{ 1085 "users" : { 1086 "count" : 1, 1087 "array" : ["John", "Ming"] 1088 } 1089 }` 1090 1091 j, _ := gjson.LoadContent(data) 1092 1093 j.Append("users.array", "Lily") 1094 1095 fmt.Println(j.Get("users.array").Array()) 1096 1097 // Output: 1098 // [John Ming Lily] 1099 } 1100 1101 func ExampleJson_MustAppend() { 1102 data := 1103 `{ 1104 "users" : { 1105 "count" : 1, 1106 "array" : ["John", "Ming"] 1107 } 1108 }` 1109 1110 j, _ := gjson.LoadContent(data) 1111 1112 j.MustAppend("users.array", "Lily") 1113 1114 fmt.Println(j.Get("users.array").Array()) 1115 1116 // Output: 1117 // [John Ming Lily] 1118 } 1119 1120 func ExampleJson_Map() { 1121 data := 1122 `{ 1123 "users" : { 1124 "count" : 1, 1125 "info" : { 1126 "name" : "John", 1127 "age" : 18, 1128 "addr" : "ChengDu" 1129 } 1130 } 1131 }` 1132 1133 j, _ := gjson.LoadContent(data) 1134 1135 fmt.Println(j.Get("users.info").Map()) 1136 1137 // Output: 1138 // map[addr:ChengDu age:18 name:John] 1139 } 1140 1141 func ExampleJson_Array() { 1142 data := 1143 `{ 1144 "users" : { 1145 "count" : 1, 1146 "array" : ["John", "Ming"] 1147 } 1148 }` 1149 1150 j, _ := gjson.LoadContent(data) 1151 1152 fmt.Println(j.Get("users.array")) 1153 1154 // Output: 1155 // ["John","Ming"] 1156 } 1157 1158 func ExampleJson_Scan() { 1159 data := `{"name":"john","age":"18"}` 1160 1161 type BaseInfo struct { 1162 Name string 1163 Age int 1164 } 1165 1166 info := BaseInfo{} 1167 1168 j, _ := gjson.LoadContent(data) 1169 j.Scan(&info) 1170 1171 fmt.Println(info) 1172 1173 // May Output: 1174 // {john 18} 1175 } 1176 1177 func ExampleJson_Dump() { 1178 data := `{"name":"john","age":"18"}` 1179 1180 j, _ := gjson.LoadContent(data) 1181 j.Dump() 1182 1183 // May Output: 1184 // { 1185 // "name": "john", 1186 // "age": "18", 1187 // } 1188 }