找回密码
 用户注册

QQ登录

只需一步,快速开始

查看: 4306|回复: 0

What is JSON

[复制链接]
发表于 2012-2-13 14:39:19 | 显示全部楼层 |阅读模式
SuperObject
What is JSON ?
JSON (JavaScript Object Notation) is a lightweight data-interchange format.
It is easy for humans to read and write.
It is easy for machines to parse and generate.
It is based on a subset of the JavaScript Programming Language, Standard ECMA-262 3rd Edition - December 1999.
JSON is a text format that is completely language independent but uses conventions that are familiar to programmers.
These properties make JSON an ideal data-interchange language.
You can get more informations on json.org.
  1. {
  2.   "name": "Henri Gourvest", /* this is a comment */
  3.   "vip": true,
  4.   "telephones": ["000000000", "111111111111"],
  5.   "age": 33,
  6.   "size": 1.83,
  7.   "adresses": [
  8.     {
  9.       "adress": "blabla",
  10.       "city": "Metz",
  11.       "pc": 57000
  12.     },
  13.     {
  14.       "adress": "blabla",
  15.       "city": "Nantes",
  16.       "pc": 44000
  17.     }
  18.   ]
  19. }
复制代码
Parsing a JSON data structure
  1. var
  2.   obj: ISuperObject;
  3. begin
  4.   obj := SO('{"foo": true}');
  5.   obj := TSuperObject.ParseString('{"foo": true}');
  6.   obj := TSuperObject.ParseStream(stream);
  7.   obj := TSuperObject.ParseFile(FileName);
  8. end;
复制代码
Accessing data
There isn't individual datastructure for each supported data types.
They are all an object: the ISuperObject.
  val := obj.AsObject.S['foo']; // get a

  1. val := obj.AsString;
  2.   val := obj.AsInteger;
  3.   val := obj.AsBoolean;
  4.   val := obj.AsDouble;
  5.   val := obj.AsArray;
  6.   val := obj.AsObject;
  7.   val := obj.AsMethod;
复制代码
How to read a property value of an object ?
  // the advanced way

  1. string
  2.   val := obj.AsObject.I['foo']; // get an Int64
  3.   val := obj.AsObject.B['foo']; // get a Boolean
  4.   val := obj.AsObject.D['foo']; // get a Double
  5.   val := obj.AsObject.O['foo']; // get an Object (default)
  6.   val := obj.AsObject.M['foo']; // get a Method
  7.   val := obj.AsObject.N['foo']; // get a null object
复制代码
How to read a value from an array ?
  1.   val := obj.AsArray.S[0]; // get a string
  2.   val := obj.AsArray.I[0]; // get an Int64
  3.   val := obj.AsArray.B[0]; // get a Boolean
  4.   val := obj.AsArray.D[0]; // get a Double
  5.   val := obj.AsArray.O[0]; // get an Object (default)
  6.   val := obj.AsArray.M[0]; // get a Method
  7.   val := obj.AsArray.N[0]; // get a null object
复制代码
Using paths

  obj['foo']; // get a property


Using paths is a very productive method to find an object when you know where is it.
This is some usage cases:

  obj := so('{"index": 1, "items": ["item 1", "item 2", "item 3"]}');


  1.   obj['123']; // get an item array
  2.   obj['foo.list']; // get a property from an object
  3.   obj['foo[123]']; // get an item array from an object
  4.   obj['foo(1,2,3)']; // call a method
  5.   obj['foo[]'] := value; // add an item array
复制代码

you also can encapsulate paths:

  obj := so('{"index": 1, "items": ["item 1", "item 2", "item 3"]}');

  1.   obj['items[index]'] // return "item 2"
复制代码

or recreate a new data structure from another:

  1.   obj['{"item": items[index], "index": index}'] // return {"item": "item 2", "index": 1}
复制代码
Browsing data structure
Using Delphi enumerator.

var


Using Delphi enumerator you can browse item's array or property's object value in the same maner.
var

  1.   item: ISuperObject;
  2. begin
  3.   for item in obj['items'] do ...
复制代码

you can also browse the keys and values of an object like this:

var

  1.   item: TSuperAvlEntry;
  2. begin
  3.   for item in obj.AsObject do ...
  4.   begin
  5.     item.Name;
  6.     item.Value;
  7.   end;
复制代码
Browsing object properties without enumerator
var

  1.   item: TSuperObjectIter;
  2. begin
  3.   if ObjectFindFirst(obj, item) then
  4.   repeat
  5.     item.key;
  6.     item.val;
  7.   until not ObjectFindNext(item);
  8.   ObjectFindClose(item);
复制代码
Browsing array items without enumerator
type

  1.   item: Integer;
  2. begin
  3.   for item := 0 to obj.AsArray.Length - 1 do
  4.     obj.AsArray[item]
复制代码
RTTI & marshalling in Delphi 2010
  obj.AsJSon(options);

  1.   TData = record
  2.     str: string;
  3.     int: Integer;
  4.     bool: Boolean;
  5.     flt: Double;
  6.   end;
  7. var
  8.   ctx: TSuperRttiContext;
  9.   data: TData;
  10.   obj: ISuperObject;
  11. begin
  12.   ctx := TSuperRttiContext.Create;
  13.   try
  14.     data := ctx.AsType<TData>(SO('{str: "foo", int: 123, bool: true, flt: 1.23}'));
  15.     obj := ctx.AsJson<TData>(data);
  16.   finally
  17.     ctx.Free;
  18.   end;
  19. end;
复制代码
Saving data
  SO(['prop1', true, 'prop2', 123

  1.   obj.SaveTo(stream);
  2.   obj.SaveTo(filename);
复制代码
Helpers
  1. ]); // return an object {"prop1": true, "prop2": 123}
  2.   SA([true, 123]); // return an array [true, 123]
复制代码
Non canonical forms
The SuperObject is able to parse non canonical forms.
  1. // unquoted identifiers
  2. SO('{foo: true}');
  3. // unescaped or unquoted strings
  4. SO('{собственность: bla bla bla}');
  5. // excadecimal
  6. SO('{foo: \xFF}');
复制代码
您需要登录后才可以回帖 登录 | 用户注册

本版积分规则

Archiver|手机版|小黑屋|ACE Developer ( 京ICP备06055248号 )

GMT+8, 2024-5-2 16:45 , Processed in 0.018075 second(s), 6 queries , Redis On.

Powered by Discuz! X3.5

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表