-2

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

This is the JSON string.

{
  "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.

Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
Leo Bidi
  • 51
  • 1
  • 3
  • 1
    There are tons of Delphi JSON examples floating around. Please show the actual code that you have already tried that is not doing what you need. We are not here to write the entire code for you, we are here to help you work with code you write yourself. – Remy Lebeau Jun 17 '16 at 17:30
  • 1
    you have mistake in your JSON string. Extra comma after `"Address": "5th av"` – Zam Jun 17 '16 at 17:56

2 Answers2

5

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:

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. Start by passing the JSON string to TJSONObject.ParseJSONValue(), which returns a TJSONValue. Type-cast that to a TJSONObject.
  2. Use its Values property to access the client and order values. Type-cast them to TJSONObject and TJSONArray, respectively.
  3. 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.
  4. 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.

See Embarcadero's JSON documentation for more information.

Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
  • Many thanks Remy, I did not want the code, just want to someone to clarify the structure as you did . I have now more to work with. – Leo Bidi Jun 17 '16 at 18:17
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

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

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

zeus
  • 12,173
  • 9
  • 63
  • 184
  • This is not a site for *I recommend my favorite open-source/freeware/shareware/commercial product* posts. If you want to endorse one of those, write a blog post doing so. You can't propose an *alternative* to *I've done nothing, so please write some code for me*. – Ken White Jun 18 '16 at 03:32