Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>JSON is very simple and easy to figure out, once you understand the basic concepts. Have a look at <a href="http://json.org">http://json.org</a>, where it explains things.</p> <p>There are 4 basic concepts in JSON:</p> <p>A <em>value</em> is any JSON element: a basic string or number, an array, or an object. (Anything but a <em>pair</em>.)</p> <p>An <em>array</em> should be a familiar concept: an ordered list of values. The main difference from Delphi arrays is that JSON arrays don't have a defined type for the elements; they're simply "an array of JSON values."</p> <p>A <em>pair</em> is a key-value pair. The key can be a string or a number, and the value can be any JSON value.</p> <p>An <em>object</em> is an associative map of JSON pairs. You can think of it conceptually as a <code>TDictionary&lt;string, JSON value&gt;</code>.</p> <p>So if I wanted to take a JSON array of data like that, and put it in a TListBox, I'd do something like this (DBXJSON example, warning: not tested):</p> <pre><code>procedure TMyForm.LoadListBox(response: TJSONObject); var i: integer; ips: TJSONArray; ip: TJSONObject; pair: TJSONPair; begin ListBox.Clear; pair := response.Get('ips'); if pair = nil then Exit; ips := pair.value as TJSONArray; for i := 0 to ips.size - 1 do begin ip := ips.Get(i) as TJSONObject; pair := ip.Get('ip'); if pair = nil then ListBox.AddItem('???', ip.Clone) else ListBox.AddItem(pair.JsonString, ip.Clone); end; end; </code></pre> <p>Then you have a list of IP addresses, and associated objects containing the full record that you can get at if the user selects one. (If you wanted to put the entire contents of each record into the list control, have a look at <code>TListView</code>. It works better than <code>TListBox</code> for that.)</p> <p>And if you want to build an array of strings containing all the values, do something like this:</p> <pre><code>function JsonObjToStringArray(obj: TJsonObject): TArray&lt;string&gt;; var i: integer; begin SetLength(result, obj.Size); for i := 0 to obj.Size - 1 do result[i] := obj.Get(i).JsonValue.ToString; end; </code></pre> <p>This is all just sample code, of course, but it should give you something to build on.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload