About memory data and JSON

Source: Internet
Author: User
Tags tojson webbroker

Gossip :

is writing a small website with WebBroker, feel a lot:
1, if it is to write a small thing, should first consider the next WebBroker, because it is minutes can be started.
2, if you want to write a big thing, perhaps also should consider WebBroker, because compared to other it has the highest flexibility (but this is just like, because I just wrote a small thing).
3, Delphi 3 has WebBroker, but now only began to use it; with the popularity of "server" and "Web program", WebBroker should be more mentioned.

JSON was used during the period, so there was this blog post.


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 "key value" pairs: Tmeminifile, tinifile; In addition, someone used SQLite to make 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:

Many of the classes, mainly used are: Tjsonobject, Tjsonarray.

usesSystem.json;//tjsonobjectprocedureTform1.button1click (Sender:tobject);varJ:tjsonobject;beginJ: = Tjsonobject.create; J.addpair (' AA ',' 111 '); J.addpair (' BB ',' 222 ');//Output stringMemo1.text: = j.tostring;//Result: {"AA": "111", "BB": "222"}{ToJSON is the same as ToString, but should try to use ToString instead of ToJSON, because ToJSON is also calling ToString and reallocating memory}J.free;End;//tjsonarrayprocedureTform1.button2click (Sender:tobject);varA:tjsonarray;beginA: = 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;//Result: ["AAA", "BBB"]A.free;End;//When tjsonobject nested other objectsprocedureTform1.button3click (Sender:tobject);varJ:tjsonobject; A:tjsonarray;beginJ: = Tjsonobject.create; A: = Tjsonarray.create (' AAA ',' BBB ');//Can be initialized with two valuesA.add (1); A.add (2); J.addpair (' arr ', a); Memo1.text: = j.tostring;//Result: {"arr": ["AAA", "BBB", +]}//A.free; {never release its inner object, its parent will automatically free it}J.free;End;//Work with the example aboveprocedureTform1.button4click (Sender:tobject);varJ:tjsonobject;beginJ: = Tjsonobject.create; J.addpair (' arr ', tjsonarray.create); withj.values[' arr '] asTjsonarray DobeginADD (' AAA '); ADD (' BBB '); ADD (1); ADD (2);End; Memo1.text: = j.tostring;//Result: {"arr": ["AAA", "BBB", +]}J.free;End;//re-workprocedureTform1.button5click (Sender:tobject);varJ:tjsonobject; A:tjsonarray;beginJ: = 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", +]}J.free;End;//Add other types of numbersprocedureTform1.button6click (Sender:tobject);varJ:tjsonobject; A:tjsonarray;beginJ: = 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: {"value": 3.1415926, "Boolean true": True, "Boolean false": false, "null value": null}J.free;End;//ReadprocedureTform1.button7click (Sender:tobject);varJ:tjsonobject; Str:string;  Num:integer; ARR1,ARR2:string;beginJ: = 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", where the "value" 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 directlyNum: = 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, arr2J.free;End;//TraversalprocedureTform1.button8click (Sender:tobject);varJ:tjsonobject;  P:tjsonpair;  V:tjsonvalue;  I:integer; A:tjsonarray;beginJ: = Tjsonobject.create; J.addpair (' AA ',' 111 '); J.addpair (' BB ',' 222 '); J.addpair (' arr ', Tjsonarray.create (' arr1 ',' arr2 '));//Set traversal Tjsonobject forPinchJ DoMEMO1.LINES.ADD (p.tostring);//"AA": "111" \ "BB": "222" \ "arr": ["arr1", "arr2"]//Index traversal tjsonobject, where the Get function is no longer advocating the use of the forI: =0  toJ.count-1  DoMEMO1.LINES.ADD (J.get (i). ToString);//"AA": "111" \ "BB": "222" \ "arr": ["arr1", "arr2"]//Set traversal Tjsonarray forVinchj.values[' arr '] asTjsonarray DoMEMO1.LINES.ADD (V.value);//arr1 \ arr2//Index traversal TjsonarrayA: = j.values[' arr '] asTjsonarray; forI: =0  toA.count-1  DoMEMO1.LINES.ADD (A.items[i]. Value);//arr1 \ arr2J.free;End;//access to fileprocedureTform1.button9click (Sender:tobject);varJ1,j2:tjsonobject;begin//Random virtual dataJ1: = Tjsonobject.create (Tjsonpair.create (' AAA ',' 111 ')); J1. Addpair (' BBB ', J1. Clone asTjsonvalue); ChDir (' C:\Temp ');//write file withTstringlist.create DobeginADD (J1.    ToString); SaveToFile (' JsonTest.txt ', Tencoding.utf8);//Result: {"AAA": "111", "BBB": {"AAA": "111"}}Free;End; J1. Free;//read from file withTstringlist.create Do beginLoadFromFile (' JsonTest.txt ', Tencoding.utf8); J2: = Tjsonobject.parsejsonvalue (Text) asTjsonobject; Free;End; Memo1.text: = J2. ToString;//Result: {"AAA": "111", "BBB": {"AAA": "111"}}J2. Free;End;//Modify, deleteprocedureTform1.button10click (Sender:tobject);varJ:tjsonobject;beginJ: = Tjsonobject.create; J.addpair (' AAA ',' 111 '); J.addpair (' BBB ',' 222 '); J.addpair (' CCC ',' 333 ');//ModifyJ.get (' AAA '). Jsonvalue: = Tjsonstring.create (' 11111 ');//tjsonpair's jsonstring, Jsonvalue are key, value//j.pairs[0]. Jsonvalue: = Tjsonstring.create (' 11111 ');//DeleteJ.removepair (' BBB '); MEMO1.LINES.ADD (j.tostring);//Result: {"AAA": "11111", "CCC": "333"}J.free;End;

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.