使用LkJSON单元解析的问题(49)

  • 主题发起人 主题发起人 himoo
  • 开始时间 开始时间
H

himoo

Unregistered / Unconfirmed
GUEST, unregistred user!
使用 LkJSON v1.06 解析json时候出的问题。请大家帮忙。字符串如下:{"farmlandStatus":[{"a":15,"b":6,"c":0,"d":0,"e":1,"f":0,"g":0,"h":1,"i":100,"j":1,"k":26,"l":15,"m":15,"n":{"227554197":1,"276748590":1,"239255726":1,"238907507":1,"241702041":1,"250742092":1,"268702762":1,"237278654":1,"230021050":1,"260507944":1,"264453468":1},"o":0,"p":[],"q":1249911384,"r":1250141784,"s":0,"t":0,"u":0},{"a":15,"b":6,"c":0,"d":0,"e":1,"f":0,"g":0,"h":1,"i":100,"j":1,"k":28,"l":16,"m":16,"n":{"227554197":1,"276748590":1,"239255726":1,"238907507":1,"241702041":1,"250742092":1,"268702762":1,"237278654":1,"230021050":1,"260507944":1},"o":0,"p":[],"q":1249911384,"r":1250141784,"s":0,"t":0,"u":0}],"items":{"1":{"itemId":1},"2":{"itemId":2},"4":{"itemId":"4"},"3":{"itemId":"3"}},"exp":19661}farmlandStatus下有个数组,只拿第一个说吧。里面有个n值,也是数组:"n":{"227554197":1,"276748590":1,"239255726":1,"238907507":1,"241702041":1,"250742092":1,"268702762":1,"237278654":1,"230021050":1,"260507944":1,"264453468":1}此时,我不想得到Value值,只想得到Aname的值,比如227554197,因为这些值是不固定的。那个单元里有没有对应的啊?我发现有一个NameOf的方法,但是只能用在js := TlkJSON.ParseText(HtmlString) as TlkJSONobject;js.nameof(integer);而不能这样用:js.Field['farmlandStatus'].Child[0].Field['n'].NameOf[0]请问还有什么办法么?
 
2.6、基本Json类2.6.1、什么是Json?JSON(JavaScript Object Notation) 是一种轻量级的数据交换格式。易于人阅读和编写。同时也易于机器解析和生成。它基于JavaScript Programming Language, Standard ECMA-262 3rd Edition - December 1999的一个子集。 JSON采用完全独立于语言的文本格式,但是也使用了类似于C语言家族的习惯(包括C, C++, C#, Java, JavaScript, Perl, Python等)。这些特性使JSON成为理想的数据交换语言。JSON建构于两种结构:“名称/值”对的集合(A collection of name/value pairs)。不同的语言中,它被理解为对象(object),纪录(record),结构(struct),字典(dictionary),哈希表(hash table),有键列表(keyed list),或者关联数组 (associative array)。 值的有序列表(An ordered list of values)。在大部分语言中,它被理解为数组(array)。 这些都是常见的数据结构。事实上大部分现代计算机语言都以某种形式支持它们。这使得一种数据格式在同样基于这些结构的编程语言之间交换成为可能。JSON具有以下这些形式:对象是一个无序的“‘名称/值’对”集合。一个对象以“{”(左括号)开始,“}”(右括号)结束。每个“名称”后跟一个“:”(冒号);“‘名称/值’ 对”之间使用“,”(逗号)分隔。 数组是值(value)的有序集合。一个数组以“[”(左中括号)开始,“]”(右中括号)结束。值之间使用“,”(逗号)分隔。值(value)可以是双引号括起来的字符串(string)、数值(number)、true、false、 null、对象(object)或者数组(array)。这些结构可以嵌套。 字符串(string)是由双引号包围的任意数量Unicode字符的集合,使用反斜线转义。一个字符(character)即一个单独的字符串(character string)。 字符串(string)与C或者Java的字符串非常相似。 QuickBurro最新的V3.08版本开始全面支持Json数据,向应用程序员提供了QBJson.pas单元。该单元原作者是Jose Fabio Nascimento de Almeida,QuickBurro作者对此进行了注释汉化、代码重排、Bug修正,以及增加了Json类实例与Tstream、文件、TQBParcel等进行数据转换的若干方法,使得该单元更可靠、好用。2.6.2、TQBJson类即“名称/值”对的集合(A collection of name/value pairs)。不同的语言中,它被理解为对象(object),纪录(record),结构(struct),字典(dictionary),哈希表(hash table),有键列表(keyed list),或者关联数组 (associative array)。//// 根据不同的数据来源创建对象的Overload方法… constructor Create
overload
constructor Create (jo : TQBJSon
sa : array of string)
overload
constructor Create (x : TQBJSonTokener)
overload
constructor Create (map : TStringList)
overload
constructor Create (s : string)
overload
constructor Create (aStream: TStream
Bytes: integer)
overLoad
constructor Create (aFilename: string
FailIfNoFile: boolean)
overload
constructor create (aQBParcel: TQBParcel
GoodsName: string)
overload;//// 读属性等相关方法… procedure Clean
function Clone : TQBJSonAbstractObject
override
function Accumulate (key : string
value : TQBJSonAbstractObject): TQBJSon
function Get (key : string) : TQBJSonAbstractObject
function GetBoolean (key : string): boolean
function GetDouble (key : string): double
function GetInt (key : string): integer
function GetJSonArray (key : string) : TQBJSonArray
function GetJSon (key : string) : TQBJSon
function GetString (key : string): string
function Has (key : string) : boolean
function IsNull (key : string) : boolean
function Keys : TStringList
function Length : integer
function Names : TQBJSonArray
class function NumberToString (n: _Number): string
class function ValueToString(value : TQBJSonAbstractObject) : string
overload
class function ValueToString(value : TQBJSonAbstractObject
indentFactor, indent : integer) : string
overload;//// 属性值替换的相关方法… function Opt (key : string) : TQBJSonAbstractObject
function OptBoolean (key : string): boolean
overload
function OptBoolean (key : string
defaultValue : boolean): boolean
overload
function OptDouble (key : string): double
overload
function OptDouble (key : string
defaultValue : double): double
overload
function OptInt (key : string): integer
overload
function OptInt (key : string
defaultValue : integer): integer
overload
function OptString (key : string): string
overload
function OptString (key : string
defaultValue : string): string
overload
function OptJSonArray (key : string): TQBJSonArray
overload
function OptJSon (key : string): TQBJSon
overload;//// 添加属性的若干方法… procedure Put (key : string
value : boolean)
overload
procedure Put (key : string
value : double)
overload
procedure Put (key : string
value : integer)
overload
procedure Put (key : string
value : string)
overload
procedure Put (key : string
value : TQBJSonAbstractObject)
overload;procedure PutOpt (key : string
value : TQBJSonAbstractObject);//// 数据导出的相关方法… class function quote (s : string): string
function Remove (key : string): TQBJSonAbstractObject
procedure AssignTo(JSon: TQBJSon)
function ToJSonArray (names : TQBJSonArray) : TQBJSonArray
function ToString (): string
overload
override
function ToString2 (indentFactor : integer): string
overload
function ToString3 (indentFactor, indent : integer): string
overload
function SaveToStream(aStream: TStream
OutFormat: integer): boolean
function SaveToFile(aFilename: string
OutFormat: integer): boolean
function SaveToParcel(aQBParcel: TQBParcel
GoodsName: string
OutFormat: integer): boolean;//// 其他杂项方法… property PropValues[const Key: String]:String read GetPropValues write SetPropValues
default
property KeyByIndex[index: Integer]:String read GetKeyByIndex
property ValByIndex[index: Integer]:String read GetValByIndex
property AsString:String read ToString write SetAsString
procedure Assign(Source: TQBJSon)
function GetCascadeValue(const Keys: array of String): String
procedure SetCascadeValue(const Value: String
const Keys: array of String)
function GetDiffFrom(Source: TQBJSon): TQBJSon
procedure RemoveByKeyHeader(const Header: String='~')
procedure CleanKey(const Key: String)
function PropCount:Integer
function KeyByVal(const Value: String):String
destructor Destroy;override
class function NULL : _NULL;2.6.3、TQBJsonArray类值的有序列表(An ordered list of values)。在大部分语言中,它被理解为数组(array)。//// 创建JsonArray类的方法…destructor destroy
override
constructor create
overload
constructor create (collection : TList)
overload
constructor create (x : TQBJSonTokener)
overload
constructor create (s : string)
overload
constructor Create (aStream: TStream
Bytes: integer)
overLoad
constructor Create (aFilename: string
FailIfNoFile: boolean)
overload
constructor create (aQBParcel: TQBParcel
GoodsName: string)
overload;function Clone : TQBJSonAbstractObject
override;//// 取值的一些方法… function get (index : integer) : TQBJSonAbstractObject
function getBoolean (index : integer) : boolean
function getDouble (index : integer) : double
function getInt (index : integer): integer
function getJSonArray (index : integer) : TQBJSonArray
function getJSon (index : integer) : TQBJSon
function getString (index : integer) : string
function isNull (index : integer): boolean
function join (separator : string) : string;function length : integer;//// 替换值的方法…. function opt (index : integer) : TQBJSonAbstractObject
function optBoolean ( index : integer) : boolean
overload
function optBoolean ( index : integer
defaultValue : boolean) : boolean
overload
function optDouble (index : integer) : double
overload
function optDouble (index : integer
defaultValue :double ) : double
overload
function optInt (index : integer) : integer
overload
function optInt (index : integer
defaultValue : integer) : integer
overload
function optJSonArray (index : integer) : TQBJSonArray
overload
function optJSon (index : integer) : TQBJSon
overload
function optString (index : integer) : string
overload
function optString (index : integer
defaultValue : string) : string
overload;//// 添加值的方法… procedure put ( value : boolean)
overload
procedure put ( value : double )
overload
procedure put ( value : integer)
overload
procedure put ( value : TQBJSonAbstractObject)
overload
procedure put ( value: string)
overload
procedure put ( index : integer
value : boolean)
overload
procedure put ( index : integer
value : double)
overload
procedure put ( index : integer
value : integer)
overload
procedure put ( index : integer
value : TQBJSonAbstractObject)
overload
procedure put ( index: integer
value: string)
overload;//// 导出数据的一些方法… function toJSon (names : TQBJSonArray ) : TQBJSon
overload
function toString : string
overload
override
function toString2 (indentFactor : integer) : string
overload
function toString3 (indentFactor, indent : integer) : string
overload
function appendJSonArray( value : TQBJSonArray): Integer
function SaveToSteam(aStream: TStream
OutFormat: integer): boolean
function SaveToFile(aFilename: string
OutFormat: integer): boolean
function SaveToParcel(aQBParcel: TQBParcel
GoodsName: string
OutFormat: integer): boolean;2.7、数据库Json类除了基本Json类支持外,QuickBurro V3.08更提供了数据库中的一些对象与Json类对象之间的转换,比如,字段属性、字段属性表、记录、记录列表、数据集等。QBDBJson.pas单元就是为您提供这些转换功能的主要单元。下面是主要的一些方法://// 字段属性Json类:描述一个数据表字段的属性... TFieldAttrJson=class(TQBJson) class function CreateIt(aFieldName: string
aFieldType: string
aJsonType: string
aFieldSize: integer): TFieldAttrJson
Overload
class function CreateIt(aCds: TClientDataset
Index: integer): TFieldAttrJson
Overload
class function CreateIt(aDs: TAdoDataset
Index: integer): TFieldAttrJson
Overload
procedure ResolveTo(var aFieldName: string
var aFieldType: string
var aJsonType: string
var aFieldSize: integer)
end;//// 字段属性表Json类:描述一个数据表的所有字段的属性... TFieldAttrListJson=class(TQBJson) class function CreateIt(aCds: TClientDataset): TFieldAttrListJson
Overload
class function CreateIt(aDs: TAdoDataset): TFieldAttrListJson
Overload
function ResolveTo: TQBJsonArray
function ConvertToCds(aCds: TClientDataset): boolean
end;//// 记录数据Json类:用于储存一个数据库记录的值... TRecordJson=class(TQBJson) class function CreateIt(aFAListJson: TFieldAttrListJson): TRecordJson
Overload
class function CreateIt(aCds: TClientDataset): TRecordJson
Overload
class function CreateIt(aDs: TAdoDataset): TRecordJson
Overload
function ResolveTo(aDs: TAdoDataset): boolean
Overload
function ResolveTo(aCds: TClientDataset): boolean
Overload
end;//// 记录集Json类:用于储存一批记录(不含表结构信息,记录可能异构)的值... TRecordListJson=class(TQBJson) class function CreateIt(aCds: TClientDataset): TRecordListJson
Overload
class function CreateIt(aDs: TAdoDataset): TRecordListJson
Overload
procedure PutRecordJson(aRecordJson: TRecordJson)
Overload
function GetRecordJson(index: integer): TRecordJson
function ResolveTo(aDs: TAdoDataset): boolean
Overload
function ResolveTo(aCds: TClientDataset): boolean
Overload
end;//// 数据集Json类:用于描述某数据表对应的数据集的完整信息(含表头及记录集,记录同构)... TTableJson=class(TQBJson) class function CreateIt(TableName: string
aCds: TClientDataset): TTableJson
Overload
class function CreateIt(TableName: string
aDs: TAdoDataset): TTableJson
Overload
function ResolveTo(aDs: TAdoDataset): boolean
Overload
function ResolveTo(aCds: TClientDataset): boolean
Overload
end;由上面这些类及其方法声明可以看出,我们在QuickBurro的应用开发包中实现了数据表与Json数据类之间的各种转换方法。基于本单元提供的方法,我们在V3.08里进一步实现了服务端的数据表导出到Json格式文件及反向导入、以Json数据作为传递参数的远过程调用等,具体的方法见后续章节。
 
单元名为QBDBJson.pas,提供字段属性、字段属性列表、记录、记录列表、数据表等数据对象的Json化功能,实现的效果如下:1、字段属性描述类TFieldAttrJson:{ "FieldName": "CustomerID", "FieldType": "WideString", "JsonType": "String", "FieldSize": 5}2、字段属性列表描述类TFieldAttrListJson:(其中省略号为后续字段描述){ "FieldAttrCount": 11, "FieldAttrList": [ { "FieldName": "CustomerID", "FieldType": "WideString", "JsonType": "String", "FieldSize": 5 }, ...... ]}3、数据库记录内容描述类TRecordJson:{ "CustomerID": "ALFKI", "CompanyName": "Alfreds Futterkiste", "ContactName": "Maria Anders", "ContactTitle": "Sales Representative", "Address": "Obere Str. 57", "City": "Berlin", "Region": "", "PostalCode": "12209", "Country": "Germany", "Phone": "030-0074321", "Fax": "030-0076545"}4、数据库记录列表描述类TRecordListJson:(省略号为后续字段描述){ "RecordCount": 92, "RecordList": [ { "CustomerID": "ALFKI", "CompanyName": "Alfreds Futterkiste", "ContactName": "Maria Anders", "ContactTitle": "Sales Representative", "Address": "Obere Str. 57", "City": "Berlin", "Region": "", "PostalCode": "12209", "Country": "Germany", "Phone": "030-0074321", "Fax": "030-0076545" }, ...... ]}5、数据表完整描述类:TTableJson:(省略号为后续字段属性或后续记录描述){ "TableName": "Customers", "TableHead": { "FieldAttrCount": 11, "FieldAttrList": [ { "FieldName": "CustomerID", "FieldType": "WideString", "JsonType": "String", "FieldSize": 5 }, ...... ] }, "TableBody": { "RecordCount": 92, "RecordList": [ { "CustomerID": "ALFKI", "CompanyName": "Alfreds Futterkiste", "ContactName": "Maria Anders", "ContactTitle": "Sales Representative", "Address": "Obere Str. 57", "City": "Berlin", "Region": "", "PostalCode": "12209", "Country": "Germany", "Phone": "030-0074321", "Fax": "030-0076545" }, ...... ] }}
 
Jose Fabio Nascimento这个作者已经不更新这个单元了。
 
使用该单元的GetJSon('n').Names.toString 解决!非常感谢!
 

Similar threads

I
回复
0
查看
829
import
I
I
回复
0
查看
850
import
I
I
回复
0
查看
639
import
I
后退
顶部