Good evening all.
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;
{
"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
}
As you can see, it's a JSON object with an array and some data fields of various types (string, float, integer, etc).
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).
Where I'm struggling is with linking the data to the ListView. I've created the FieldDefs
for the TClientDataSource
as such (this is the literal tree view in relation to ChildDefs
);
- |--result (Type: ftString)
- |--response (Type: ftObject)
- |--|--ips (Type: ftArray, Size: 6)
- |--|--|-- ip (Type: ftString)
- |--|--|-- classification (Type: ftString)
- |--|--|-- hits (Type: ftInteger)
- |--|--|-- latitude (Type: ftFloat)
- |--|--|-- longitude (Type: ftFloat)
- |--|--|-- zone_name (Type: ftString)
I've then livebinded/livebound BindSourceDB1's response.ips[0]
to the TListView's Item.Text
field. Unfortunately, when I run the application and execute the request, I get an error;
ClientDataSet1: Field 'response.ips[0]' not found
In this instance, I'm trying to retrieve the response.ips[index].ip
field of each item in the array and output it as an individual item in the TListView. Unfortunately, even livebinding the response.ips
field without an index still presents a similar error. However, if I link it to the result
field, then it returns the 'success' message inside the listview as expected.
I did take a look at Jim McKeeth's REST client example 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 FieldDefs
property, so I'm not sure whether I should define my fields there or not.
I imagine I've just got the data types setup incorrectly or missed something minor, but I'd appreciate any assistance.
I figured it out;
- Set up your REST components
- For the
TRESTResponseDataSetAdapter
, set it'sRootElement
property toresponse.ips
- Then, add the fields
ip
,classification
,hits
,latitude
,longitude
, andzone_name
as it'sFieldDefs
- Right-click the
TRESTResponseDataSetAdapter
and select 'Update DataSet' - Livebind one of the fields from the
TRESTResponseDataSetAdapter
to theitem.text
property of theTListView
The application then worked correctly and reflects the data properly.
来源:https://stackoverflow.com/questions/19755959/livebinding-json-objects-and-arrays