title: Livebinding JSON objects and arrays
body: <p>Good evening all.</p>
  
  <p>I'm currently trying to get to grips with livebindings in Delphi as I'd like to refresh one of my current projects (complete rework from the base for the purpose of pushing to other platforms, optimizing performance and minimizing the code). I'm working with a web API which returns JSON data. The returned JSON format for one example call would look like this;</p>
  
  <pre><code>{
    "response": {
      "ips": [
        {
          "ip": "111.222.333.444",
          "classification": "regular",
          "hits": 134,
          "latitude": 0.0000,
          "longitude": 0.0000,
          "zone_name": "example.com"
        },
        {
          "ip": "555.666.777.888",
          "classification": "regular",
          "hits": 134,
          "latitude": 50.0000,
          "longitude: 50.0000,
          "zone_name": "example-2.com"
        },
      ]
  },
  "result": "success",
  "msg": null
  }
  </code></pre>
  
  <p>As you can see, it's a JSON object with an array and some data fields of various types (string, float, integer, etc).</p>
  
  <p>Within my application, I've got the TRESTClient, TRESTRequest, TRESTResponse, TRESTResponseDataSetAdapter, TClientDataSet, and TBindSourceDB components. I've also got a TButton, a TMemo, and a TListView. I've managed to hook all the components up via livebindings and the entire data returned from the call is displayed in the memo when I click the button (which executes the request).</p>
  
  <p>Where I'm struggling is with linking the data to the ListView. I've created the <code>FieldDefs</code> for the <code>TClientDataSource</code> as such (this is the literal tree view in relation to <code>ChildDefs</code>);</p>
  
  <ul>
  <li>|--result (Type: ftString)</li>
  <li>|--response (Type: ftObject)</li>
  <li>|--|--ips (Type: ftArray, Size: 6)</li>
  <li>|--|--|-- ip (Type: ftString)</li>
  <li>|--|--|-- classification (Type: ftString)</li>
  <li>|--|--|-- hits (Type: ftInteger)</li>
  <li>|--|--|-- latitude (Type: ftFloat)</li>
  <li>|--|--|-- longitude (Type: ftFloat)</li>
  <li>|--|--|-- zone_name (Type: ftString)</li>
  </ul>
  
  <p>I've then livebinded/livebound BindSourceDB1's <code>response.ips[0]</code> to the TListView's <code>Item.Text</code> field. Unfortunately, when I run the application and execute the request, I get an error;</p>
  
  <pre><code>ClientDataSet1: Field 'response.ips[0]' not found
  </code></pre>
  
  <p>In this instance, I'm trying to retrieve the <code>response.ips[index].ip</code> field of each item in the array and output it as an individual item in the TListView. Unfortunately, even livebinding the <code>response.ips</code> field without an index still presents a similar error. However, if I link it to the <code>result</code> field, then it returns the 'success' message inside the listview as expected.</p>
  
  <p>I did take a look at <a href="http://delphi.org/2013/09/delphi-xe5-mobile-rest-client-demo-source/" rel="nofollow">Jim McKeeth's REST client example</a> and that got me to the current point, but working out how to adapt it for my own data is proving a little challenging.  I've noticed that the TRESTResponseDataSetAdapter also has it's own <code>FieldDefs</code> property, so I'm not sure whether I should define my fields there or not.</p>
  
  <p>I imagine I've just got the data types setup incorrectly or missed something minor, but I'd appreciate any assistance.</p>