Delphi从json对象访问数据

时间:2021-10-02 15:55:32

I need to write some code to retrieve data from a JSON string that my application will receive from a web site.

我需要编写一些代码来从我的应用程序将从网站接收的JSON字符串中检索数据。

This is the JSON string.

这是JSON字符串。

{
  "client" :
     {
      "Name" : "john doe",
      "Phone" : 12345678,
      "Address": "5th av",
     },
  "order" : [
     {
      "Code" : 101,
      "Quantity" : 1,
      "Cost": 10.50,
      "Coment" : ""
     },
     {
      "Code" : 102,
      "Quantity" : 3,
      "Cost": 8.50,
      "Coment" : ""
     },
     {
      "Code" : 103,
      "Quantity" : 1,
      "Cost": 21.50,
      "Coment" : ""
     }
  ]
}

I am getting very confused about how to read pairs and arrays. I've tried lots of code posted here and other forums, but I still cannot get it done.

我对如何读取对和数组感到非常困惑。我已尝试在这里和其他论坛发布了很多代码,但我仍然无法完成它。

Can anyone give me some hints to get it done? I am using XE5 and JSON units.

任何人都可以给我一些提示来完成它吗?我正在使用XE5和JSON单元。

2 个解决方案

#1


4  

I'm not going to write the actual code for you, but I am going to give you a hint.

我不打算给你写实际代码,但我会给你一个提示。

Here is the structure of the JSON you have shown:

以下是您显示的JSON的结构:

object
|
|_ client (object)
|  |_ Name (string)
|  |_ Phone (string)
|  |_ Address (string)
|
|_ order (array)
  |_ [0] (object)
  |  |_ Code (number)
  |  |_ Quantity (number)
  |  |_ Code (number)
  |  |_ Coment (string)
  |
  |_ [1] (object)
  |  |_ Code (number)
  |  |_ Quantity (number)
  |  |_ Code (number)
  |  |_ Coment (string)
  |
  |_ [2] (object)
     |_ Code (number)
     |_ Quantity (number)
     |_ Code (number)
     |_ Coment (string)

You can map that 1-to-1 to Delphi's native JSON classes in the Data.DBXJSON unit (they were moved to the System.JSON unit in XE6).

您可以将这个1对1映射到Data.DBXJSON单元中的Delphi的本机JSON类(它们被移动到XE6中的System.JSON单元)。

  1. Start by passing the JSON string to TJSONObject.ParseJSONValue(), which returns a TJSONValue. Type-cast that to a TJSONObject.
  2. 首先将JSON字符串传递给TJSONObject.ParseJSONValue(),它返回一个TJSONValue。将其类型转换为TJSONObject。

  3. Use its Values property to access the client and order values. Type-cast them to TJSONObject and TJSONArray, respectively.
  4. 使用其Values属性可以访问客户端和订单值。分别将它们类型转换为TJSONObject和TJSONArray。

  5. For the client object, use its Values property to access the Name, Phone, and Address values. You can call their Value() method to get their string values.
  6. 对于客户端对象,使用其Values属性访问Name,Phone和Address值。您可以调用它们的Value()方法来获取它们的字符串值。

  7. for the order array, use its Count and Items properties, or its GetEnumerator method (indirectly in a for..in loop, to iterate through the array items. Type-cast each one to a TJSONObject to access its Values property. For the number values, type-cast them to TJSONNumber to access their AsInt, AsInt64, and AsDouble properties as needed.
  8. 对于order数组,使用其Count和Items属性或其GetEnumerator方法(间接在for..in循环中)迭代数组项。将每个类型转换为TJSONObject以访问其Values属性。值,键入它们到TJSONNumber以根据需要访问它们的AsInt,AsInt64和AsDouble属性。

See Embarcadero's JSON documentation for more information.

有关更多信息,请参阅Embarcadero的JSON文档。

#2


0  

if you want to work with json i advise you to work with alcinoe. but use the SVN version it's the most accurate: svn checkout svn://svn.code.sf.net/p/alcinoe/code/ alcinoe-code

如果你想和json一起工作,我建议你和alcinoe一起工作。但使用SVN版本是最准确的:svn checkout svn://svn.code.sf.net/p/alcinoe/code/alcinoe-code

don't use the delphi json object (dbxjson), it's terribly slow (their is a demo in the alcinoe that compare aljsonDoc / SuperObject / dbxjon you will see by yourself the disaster with dbxjson

不要使用delphi json对象(dbxjson),它非常慢(它们是alcino的一个演示,比较aljsonDoc / SuperObject / dbxjon,你会看到自己用dbxjson发生的灾难

exemple of how to use

例如如何使用

{
  _id: 1,
  name: { first: "John", last: "Backus" },
  birth: new Date('1999-10-21T21:04:54.234Z'),
  contribs: [ "Fortran", "ALGOL", "Backus-Naur Form", "FP" ],
  awards: [
            { award: "National Medal of Science",
              year: 1975,
              by: "National Science Foundation" },
            { award: "Turing Award",
              year: 1977,
              by: "ACM" }
          ],
  spouse: "",
  address: {},
  phones: []
}

to access the document node :

访问文档节点:

MyJsonDoc.loadFromJson(AJsonStr, False);
MyJsonDoc.childnodes['_id'].int32;
MyJsonDoc.childnodes['name'].childnodes['first'].text;
MyJsonDoc.childnodes['name'].childnodes['last'].text;
MyJsonDoc.childnodes['birth'].datetime;
for i := 0 to MyJsonDoc.childnodes['contribs'].ChildNodes.count - 1 do
  MyJsonDoc.childnodes['contribs'].childnodes[i].text;
for i := 0 to MyJsonDoc.childnodes['awards'].ChildNodes.count - 1 do begin
  MyJsonDoc.childnodes['awards'].childnodes[i].childnodes['award'].text;
  MyJsonDoc.childnodes['awards'].childnodes[i].childnodes['year'].text;
  MyJsonDoc.childnodes['awards'].childnodes[i].childnodes['by'].text;
end;

you can read the full doc of aljsondoc here

你可以在这里阅读aljsondoc的完整文档

#1


4  

I'm not going to write the actual code for you, but I am going to give you a hint.

我不打算给你写实际代码,但我会给你一个提示。

Here is the structure of the JSON you have shown:

以下是您显示的JSON的结构:

object
|
|_ client (object)
|  |_ Name (string)
|  |_ Phone (string)
|  |_ Address (string)
|
|_ order (array)
  |_ [0] (object)
  |  |_ Code (number)
  |  |_ Quantity (number)
  |  |_ Code (number)
  |  |_ Coment (string)
  |
  |_ [1] (object)
  |  |_ Code (number)
  |  |_ Quantity (number)
  |  |_ Code (number)
  |  |_ Coment (string)
  |
  |_ [2] (object)
     |_ Code (number)
     |_ Quantity (number)
     |_ Code (number)
     |_ Coment (string)

You can map that 1-to-1 to Delphi's native JSON classes in the Data.DBXJSON unit (they were moved to the System.JSON unit in XE6).

您可以将这个1对1映射到Data.DBXJSON单元中的Delphi的本机JSON类(它们被移动到XE6中的System.JSON单元)。

  1. Start by passing the JSON string to TJSONObject.ParseJSONValue(), which returns a TJSONValue. Type-cast that to a TJSONObject.
  2. 首先将JSON字符串传递给TJSONObject.ParseJSONValue(),它返回一个TJSONValue。将其类型转换为TJSONObject。

  3. Use its Values property to access the client and order values. Type-cast them to TJSONObject and TJSONArray, respectively.
  4. 使用其Values属性可以访问客户端和订单值。分别将它们类型转换为TJSONObject和TJSONArray。

  5. For the client object, use its Values property to access the Name, Phone, and Address values. You can call their Value() method to get their string values.
  6. 对于客户端对象,使用其Values属性访问Name,Phone和Address值。您可以调用它们的Value()方法来获取它们的字符串值。

  7. for the order array, use its Count and Items properties, or its GetEnumerator method (indirectly in a for..in loop, to iterate through the array items. Type-cast each one to a TJSONObject to access its Values property. For the number values, type-cast them to TJSONNumber to access their AsInt, AsInt64, and AsDouble properties as needed.
  8. 对于order数组,使用其Count和Items属性或其GetEnumerator方法(间接在for..in循环中)迭代数组项。将每个类型转换为TJSONObject以访问其Values属性。值,键入它们到TJSONNumber以根据需要访问它们的AsInt,AsInt64和AsDouble属性。

See Embarcadero's JSON documentation for more information.

有关更多信息,请参阅Embarcadero的JSON文档。

#2


0  

if you want to work with json i advise you to work with alcinoe. but use the SVN version it's the most accurate: svn checkout svn://svn.code.sf.net/p/alcinoe/code/ alcinoe-code

如果你想和json一起工作,我建议你和alcinoe一起工作。但使用SVN版本是最准确的:svn checkout svn://svn.code.sf.net/p/alcinoe/code/alcinoe-code

don't use the delphi json object (dbxjson), it's terribly slow (their is a demo in the alcinoe that compare aljsonDoc / SuperObject / dbxjon you will see by yourself the disaster with dbxjson

不要使用delphi json对象(dbxjson),它非常慢(它们是alcino的一个演示,比较aljsonDoc / SuperObject / dbxjon,你会看到自己用dbxjson发生的灾难

exemple of how to use

例如如何使用

{
  _id: 1,
  name: { first: "John", last: "Backus" },
  birth: new Date('1999-10-21T21:04:54.234Z'),
  contribs: [ "Fortran", "ALGOL", "Backus-Naur Form", "FP" ],
  awards: [
            { award: "National Medal of Science",
              year: 1975,
              by: "National Science Foundation" },
            { award: "Turing Award",
              year: 1977,
              by: "ACM" }
          ],
  spouse: "",
  address: {},
  phones: []
}

to access the document node :

访问文档节点:

MyJsonDoc.loadFromJson(AJsonStr, False);
MyJsonDoc.childnodes['_id'].int32;
MyJsonDoc.childnodes['name'].childnodes['first'].text;
MyJsonDoc.childnodes['name'].childnodes['last'].text;
MyJsonDoc.childnodes['birth'].datetime;
for i := 0 to MyJsonDoc.childnodes['contribs'].ChildNodes.count - 1 do
  MyJsonDoc.childnodes['contribs'].childnodes[i].text;
for i := 0 to MyJsonDoc.childnodes['awards'].ChildNodes.count - 1 do begin
  MyJsonDoc.childnodes['awards'].childnodes[i].childnodes['award'].text;
  MyJsonDoc.childnodes['awards'].childnodes[i].childnodes['year'].text;
  MyJsonDoc.childnodes['awards'].childnodes[i].childnodes['by'].text;
end;

you can read the full doc of aljsondoc here

你可以在这里阅读aljsondoc的完整文档