【问题标题】:Get specific JSON feature based on a variable string comparsion根据变量字符串比较获取特定的 JSON 特征
【发布时间】:2017-10-14 14:02:48
【问题描述】:

我使用两个蓝牙信标和两个飞利浦 Hue 灯。我有一个 JSON 文件,其中包含每个区域的信标 UUID 和灯光 ID:

{
  "features":
  [
    {
      "iot_identifier" : "1",
      "iot_beacon_uuid" : "E2C56DB5-DFFB-48D2-B060-D0F5A71096E1"
    },
    {
      "iot_identifier" : "2",
      "iot_beacon_uuid" : "E2C56DB5-DFFB-48D2-B060-D0F5A71096E0"
    }
  ]
} 

我的代码中还有一个显示当前区域 UUID 的字符串。当我进入另一个区域时它会改变。

这是我在读取 JSON 后控制两个灯的代码:

List<Feature> listFeatures; //Features from JSON file
int count = listFeatures.size();

for(int i=0; i<count; i++){

     // Here I have the UUID (not elegant, but does its work)
     String regionString = beaconManager.getMonitoredRegions().toString();
     String regionUUID = regionString.substring(6, 43).toUpperCase();

     // Here I get the JSON properties for each feature
     String iotIdentifierString = listFeatures.get(i).getIotIdentifier();
     String iotBeaconUuid = listFeatures.get(i).getIotBeaconUuid();

     // need this value as double to associate the JSON-ID with the Hue light ID
     double iotIdentifierDouble = Double.parseDouble(iotIdentifierString);

     if( /* anything */ ){

        // get the light with the ID of the current JSON feature
        String lightId = hueLightObjects.getFeatures().get(i).getIotIdentifier();
        final PHLight light = bridge.getResourceCache().getLights().get(lightId);

        // control the lights, turn on/off

     }
}

因此,每当我切换区域时,"regionUUID" 字符串都会发生变化。目前,无论我在哪个地区,我总是能够控制两个灯。

我正在寻找的是一种仅控制 JSON 文件中具有当前信标 UUID 的灯的方法。

类似:

对于regionUUID.equals(iot_beacon_uuid),只控制特征中regionUUID等于iot_beacon_uuid的ID的灯。

如果我改变区域,那么我只想控制另一个特性中的另一个灯,这句话再次成立。

有人可以帮我解决这个问题吗?

【问题讨论】:

    标签: android json altbeacon philips-hue


    【解决方案1】:

    首先我相信您的问题可以更短类似于 (A Minimal, Complete, and Verifiable example)

    So when I am in the region where the minor ID = 1, I only want to select the lamps, where the property "Minor_ID" is also 1

    在您的情况下,您的代码可以实现这一点,这取决于您在检索过程后如何使用它。

    但你也可以用当前的方式做到这一点:

    for (int i=0; i<count; i++) {
               if(selectedLamp == iotIdentifierDouble ){
                //do what you wish
               }
        }
    

    【讨论】:

    • 我完全修改了这个问题,因为我无法正确表达我的问题。现在我觉得这更容易理解了。我最终要寻找的应该独立于灯光 ID 本身。更像是“选择 regionUUID 等于 JSON 文件特征中相应 UUID 的所有灯光 ID”。我希望现在能更好地理解。
    • @Arastat 您使用哪个 UI 来产生效果? listView 还是什么?
    • 该项目是关于通过指向灯光来控制灯光,因此没有 UI,只有一个按钮可以触发智能手机指向的灯光。当用户进入一个信标区域时,他会得到一张室内地图,当智能手机指向一个灯时,他会得到一个变成绿色的红色按钮。但就目前而言,每盏灯都可以指向,无论灯在哪个区域。是的,根据飞利浦提供的官方演示应用程序,灯光对象本身被组织在一个列表中,用于与色调产品一起工作。但是灯光控制没有考虑这个列表。
    猜你喜欢
    • 1970-01-01
    • 2016-03-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-05-18
    • 1970-01-01
    • 2014-02-17
    相关资源
    最近更新 更多