Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p><strong>EDIT2:</strong> AV Fixed with extreme ease.</p> <p><strong>EDIT:</strong> After further examining my own code, i realised it would cause a massive amount of memory leaks. However, i have since switched over to <code>SuperObject</code> and found the same result can be achieved in 2 lines of code with only 2 variables and no memory leaks;</p> <pre><code>Procedure ParseIPs; ISO : ISuperObject; MyItem : ISuperObject; begin ISO := SO(RetrievedJSON); for MyItem in ISO['response.ips'] do Memo2.Lines.Add(MyItem.S['ip']); end; </code></pre> <p><code>RetrievedJSON</code> is simply a <code>string</code> containing the unparsed, plaintext JSON (i.e. not a <code>JSONString</code> but an actual string).</p> <p>I've left the original code underneath for sake of continuity.</p> <hr> <p>With assistance from Mason Wheeler in an earlier answer, as well as an answer provided by "teran" on <a href="https://stackoverflow.com/questions/9608794/delphi-accessing-json-objects-within-a-json-array">question 9608794</a>, i successfully built the following to parse down to the actual level (i.e. the "array" containing the data) i needed to access, and then output all items with a specific <code>JSONString.Value</code> into a listbox (named <code>LB1</code> in the sample below);</p> <pre><code>Procedure ParseIP; var o, Jso, OriginalObject : TJSONObject; ThePair, JsPair : TJSONPair; TheVal, jsv : TJSONValue; jsArr : TJsonArray; StrL1 : String; i, num : Integer; begin num := 0; o := TJSONObject.ParseJSONValue(TEncoding.ASCII.GetBytes(Memo1.Text), 0) as TJSONObject; ThePair := o.Get('response'); TheVal := ThePair.JsonValue; STRL1 := TheVal.ToString; JSV := TJSONObject.ParseJSONValue(STRL1); OriginalObject := JSV as TJSONObject; JSPair := OriginalObject.Get('ips'); JSARR := JSPair.JsonValue as TJSONArray; for i := 0 to JsArr.Size-1 do begin JSO := JSArr.Get(i) as TJSONObject; for JSPAIR in JSO do begin num := num+1; if JSPAIR.JsonString.Value = 'ip' then begin LB1.Items.Add(JSPair.JsonValue.Value); end else null; end; end; ShowMessage('Items in listbox: ' + IntToStr(LB1.Items.Count)); ShowMessage('Items in JSON: ' + IntToStr(num div JSO.Size)); Jsv.Free; end; </code></pre> <p>While this is an extremely round-about way of doing it, it allows me to look at each individual step, and see where it's iterating down through the JSON and with extreme ease, and to change it into a function where i can output any piece or range of data as a result based on one of multiple criteria. For the sake of verifying i got the correct number of items, i added 2 <code>ShowMessage</code> routines at the end; One for the items in the listbox, and one for the number of instances of "ip" data that i was parsing.</p> <p>This code was specifically tested in Firemonkey with CloudFlare API JSON results which were output into a <code>TMemo</code> exactly as they were retrieved (on an <code>&amp;calls_left&amp;a=zone_ips&amp;class=t&amp;geo=1</code> API call, of course with your <code>zone</code>, <code>token</code> and <code>email</code> appended in addition). It should be relatively easy to modify it to work with other results from the numerous other API calls too.</p> <p>To clarify, i did try Mason's code, but unfortunately i couldn't get it working. However, i have accepted his answer for the time being on the basis that the explanation he gave on the basics was worthy of it and assisted me in getting to an end-solution and coming up with something i can build from and teach myself.</p>
    singulars
    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. This table or related slice is empty.
    1. This table or related slice is empty.
    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