动态绑定JSON对象和数组 [英] Livebinding JSON objects and arrays

查看:107
本文介绍了动态绑定JSON对象和数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家晚上好。

我目前正在努力掌握Delphi中的实时绑定,因为我想刷新自己当前的一个项目(完全返工)从基础到推向其他平台,优化性能和最小化代码的目的)。我正在使用一个Web API,该API返回JSON数据。一个示例调用返回的JSON格式如下:

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
}

如您所见,它是一个JSON对象,具有数组和一些数据字段各种类型(字符串,浮点数,整数等)。

As you can see, it's a JSON object with an array and some data fields of various types (string, float, integer, etc).

在我的应用程序中,我有TRESTClient,TRESTRequest,TRESTResponse,TRESTResponseDataSetAdapter,TClientDataSet和TBindSourceDB c抱怨者。我还有一个TButton,一个TMemo和一个TListView。我已经设法通过实时绑定将所有组件连接起来,当我单击按钮(执行请求)时,备忘录中会显示从调用返回的所有数据。

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).

我在努力将数据链接到ListView。我已经为 TClientDataSource 创建了 FieldDefs (这是与<$ c相关的文字树视图$ c> ChildDefs );

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);


  • |-结果(类型:ftString)

  • |-响应(类型:ftObject)

  • |-| --ips(类型:ftArray,大小:6)

  • |-|-|-ip(类型:ftString)

  • |-|-|-分类(类型:ftString)

  • |-||||-点击(类型:ftInteger)

  • |-|-|-纬度(类型:ftFloat)

  • |-|-|-经度(类型:ftFloat)

  • |-|-|-zone_name(类型:ftString)

  • |--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)

然后我将livebinded / livebound BindSourceDB1的 response.ips [0] 到了TListView的 Item.Text 字段。不幸的是,当我运行应用程序并执行请求时,出现错误;

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

在这种情况下,我试图检索数组中每个项目的response.ips [index] .ip 字段,并将其作为单独的项目输出到TListView中。不幸的是,即使不绑定索引而直接绑定 response.ips 字段仍然会出现类似的错误。但是,如果我将其链接到 result 字段,则它将按预期返回列表视图内的成功消息。

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.

我确实看过 Jim McKeeth的REST客户示例,这使我明白了当前的观点,但是研究如何将其调整为适合我自己的数据的方法确实有点挑战。我注意到TRESTResponseDataSetAdapter也具有它自己的 FieldDefs 属性,因此我不确定是否应该在此处定义字段。

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.

推荐答案

我想通了;


  • 设置您的REST组件

  • 对于 TRESTResponseDataSetAdapter ,将其 RootElement 属性设置为 response.ips

  • 然后,添加字段 ip 分类点击纬度经度 zone_name ,因为它是 FieldDefs

  • 右键单击 TRESTResponseDataSetAdapter 并选择'Update DataSet'

  • Livebind从 TRESTResponseDataSetAdapter item的字段之一.text 属性的 TListView

  • Set up your REST components
  • For the TRESTResponseDataSetAdapter, set it's RootElement property to response.ips
  • Then, add the fields ip, classification, hits, latitude, longitude, and zone_name as it's FieldDefs
  • Right-click the TRESTResponseDataSetAdapter and select 'Update DataSet'
  • Livebind one of the fields from the TRESTResponseDataSetAdapter to the item.text property of the TListView

应用程序随后可以正常工作并正确反映数据。

The application then worked correctly and reflects the data properly.

这篇关于动态绑定JSON对象和数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆