In Case of blog reprint: About memory data and JSON

Source: Internet
Author: User
Tags tojson

Reprint Address:

Http://www.cnblogs.com/del/p/4225871.html
Introduced
This article describes the memory data and JSON, the following is the specific content:
Summary and review of Delphi and memory data:
1. Normal type variable: stores one data.
2. Arrays: Stores a set of data of the same type.
3, Structure: storage of a set of different types of data; Now the structure is getting more and more complex, and you can manipulate these data more and more like "class".
4, "key value" pair: I generally use tstringlist, and often use it instead of INI file class, the same kind also has thashedstringlist, Tstringhash.
5, the classification of the "key value" pair: Tmeminifile, tinifile; In addition, some people use SQLite to do a storage of binary data INI file class (Forget the name), I tried, very useful (in the official example).
6, Memory data table: Now should be preferred tfdmemtable (before the TClientDataSet).
7. Database-level memory (or file) data: SqLite.
8, Memory multi-fork Tree: JSON (or XML), previously used Superobject, will use System.json more later. ----------------------------------------------------------------------------------------------------------- ---------- System.json? Or a superobject?
System.json is official; it saves a lot of trouble, and that's the main reason I think about it.
System.json "read-Write Files" and "format" and other functions, all the functions are not as convenient as superobject.
System.json is primarily concerned with generating JSON from the server and then transmitting it to the client (especially for JavaScript), unlike Superobject chatty.
if not too complex JSON applications, System.json is preferred, and if more features are needed, superobject will be more desirable. ----------------------------------------------------------------------------------------------------------- ---------- 1 minutes to learn about System.json:

Uses System.json;

Tjsonobject procedure Tform1.button1click (sender:tobject);   var j:tjsonobject;     Begin J: = Tjsonobject.create;     J.addpair (' AA ', ' 111 '); J.addpair (' BB ', ' 222 ');

Output string Memo1.text: = j.tostring; Result: {"AA": "111", "BB": "222"} {ToJSON is the same as ToString, but should use ToString instead of ToJSON if possible, because ToJSON is also calling ToString and reallocating memory}

J.free; End

Tjsonarray procedure Tform1.button2click (sender:tobject);   var A:tjsonarray;     Begin A: = Tjsonarray.create; A.add (1);     Tjsonarray is not like a traditional array, it can accept several different types of values, which should be considered as an extension of the Delphi array function.     A.add (2);     A.add (' AAA '); A.add (' BBB ');

Memo1.text: = a.tostring; Results: ["AAA", "BBB"]

A.free; End

Procedure Tform1.button3click (Sender:tobject) when tjsonobject nested other objects;     var j:tjsonobject;   A:tjsonarray;     Begin J: = Tjsonobject.create; A: = Tjsonarray.create (' aaa ', ' BBB ');     A A.add (1) can be initialized with two values; A.add (2);

J.addpair (' arr ', a);

Memo1.text: = j.tostring; Result: {"arr": ["AAA", "BBB", Up to]}

A.free;   {never release its inner object, its parent will automatically release it} J.free; End

The example procedure Tform1.button4click (sender:tobject);   var j:tjsonobject; Begin J: = Tjsonobject.create;

J.addpair (' arr ', tjsonarray.create);

With j.values[' arr '] as Tjsonarray do begin ADD (' AAA ');       ADD (' BBB ');       ADD (1);     ADD (2); End

Memo1.text: = j.tostring;   Result: {"arr": ["AAA", "BBB", Up to]} J.free; End

Re-adapted procedure Tform1.button5click (Sender:tobject);     var j:tjsonobject;   A:tjsonarray; Begin J: = Tjsonobject.create;

J.addpair (' arr ', tjsonarray.create);

A: = Tjsonarray (j.values[' arr ');     A.add (' AAA ');     A.add (' BBB ');     A.add (1); A.add (2);

Memo1.text: = j.tostring;   Result: {"arr": ["AAA", "BBB", Up to]} J.free; End

Add other types of number procedure Tform1.button6click (sender:tobject);     var j:tjsonobject;   A:tjsonarray;     Begin J: = Tjsonobject.create;     J.addpair (' Value ', Tjsonnumber.create (3.1415926));     J.addpair (' Boolean true ', tjsontrue.create);     J.addpair (' Boolean false ', tjsonfalse.create); J.addpair (' null value ', tjsonnull.create);

Memo1.text: = j.tostring;   Result: {"Numeric value": 3.1415926, "Boolean true": True, "Boolean false": false, "NULL": null} j.free; End

Read procedure Tform1.button7click (sender:tobject);     var j:tjsonobject;     str:string;     Num:integer;   arr1,arr2:string;     Begin J: = Tjsonobject.create;     J.addpair (' str ', ' 111 ');     J.addpair (' num ', tjsonnumber.create (222)); J.addpair (' arr ', tjsonarray.create (' arr1 ', ' arr2 '));

Memo1.text: = j.tostring; Result: {"str": "111", "num": 222, "arr": ["arr1", "ARR2"]}

JSON is a set of "key-value pairs" in which "values" may also be "key-value pairs";//So System.json uses Tjsonvalue to represent a value type, which is the parent of all value types and, of course, the parent class of Tjsonobject.

str: = j.values[' str '].             Value; Values[] is called GetValue () and prefers to use the latter num: = j.getvalue (' num '). Value.tointeger;

ARR1: = Tjsonarray (J.getvalue (' arr ')). Items[0].     Value; ARR2: = Tjsonarray (J.getvalue (' arr ')). ITEMS[1]. Value;

MEMO1.LINES.ADD (Format ('%s,%d,%s,%s ', [str, num, arr1, arr2])); 111, 222, ARR1, arr2

J.free; End

Traverse procedure Tform1.button8click (sender:tobject);     var j:tjsonobject;     P:tjsonpair;     V:tjsonvalue;     I:integer;   A:tjsonarray;     Begin J: = Tjsonobject.create;     J.addpair (' AA ', ' 111 ');     J.addpair (' BB ', ' 222 '); J.addpair (' arr ', tjsonarray.create (' arr1 ', ' arr2 '));

Set traversal Tjsonobject for P in J do Memo1.Lines.Add (p.tostring); "AA": "111" \ "BB": "222" \ "arr": ["arr1", "ARR2"]

Index traversal Tjsonobject, where the Get function has not advocated the use of the For I: = 0 to J.count-1 do Memo1.Lines.Add (J.get (i). ToString); "AA": "111" \ "BB": "222" \ "arr": ["arr1", "ARR2"]

Set traversal Tjsonarray for V in j.values[' arr '] as Tjsonarray do Memo1.Lines.Add (V.value); ARR1 \ ARR2

Index Traversal Tjsonarray A: = J.values[' arr '] as Tjsonarray; For I: = 0 to A.count-1 do Memo1.Lines.Add (A.items[i]. Value);   ARR1 \ arr2 J.free; End

Access to File procedure Tform1.button9click (sender:tobject);   var j1,j2:tjsonobject;     Begin//Random virtual data J1: = Tjsonobject.create (tjsonpair.create (' AAA ', ' 111 ')); J1. Addpair (' BBB ', J1. Clone as Tjsonvalue);

ChDir (' C:\Temp ');

Writes a file with the Tstringlist.create do begin ADD (J1.       ToString); SaveToFile (' JsonTest.txt ', Tencoding.utf8);     Result: {"AAA": "111", "BBB": {"AAA": "111"}} Free;     End J1. Free;

Read from file with Tstringlist.create do begin LoadFromFile (' JsonTest.txt ', Tencoding.utf8);       J2: = Tjsonobject.parsejsonvalue (Text) as Tjsonobject;     Free; End

Memo1.text: = J2. ToString; Result: {"AAA": "111", "BBB": {"AAA": "111"}} J2.   Free; End

Modify, delete procedure Tform1.button10click (sender:tobject);   var j:tjsonobject;     Begin J: = Tjsonobject.create;     J.addpair (' AAA ', ' 111 ');     J.addpair (' BBB ', ' 222 '); J.addpair (' CCC ', ' 333 ');

Modify the J.get (' AAA '). Jsonvalue: = Tjsonstring.create (' 11111 '); Tjsonpair jsonstring, Jsonvalue are key, value//j.pairs[0]. Jsonvalue: = Tjsonstring.create (' 11111 ');

Delete J.removepair (' BBB ');

MEMO1.LINES.ADD (j.tostring); Result: {"AAA": "11111", "CCC": "333"}

J.free; End

In Case of blog reprint: About memory data and JSON

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.