You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何将deviceId转为JSON对象并与IBeacon的JSON合并发布?

问题描述

我的代码实现如下:

private IBeaconListener createIBeaconListener() {
        return new SimpleIBeaconListener() {
            @Override
            public void onIBeaconDiscovered(IBeaconDevice ibeacon, IBeaconRegion region) {
                Gson gson = new Gson();
                String json = gson.toJson(ibeacon);

                Gson gson2 = new Gson();
                String deviceId = "{deviceId : 67814f71b5bdb4d3}";
                String json2 = gson2.toJson(obj);

                Log.i("Beacon", "IBeacon discovered: " + json);
                Log.i("Gateway", "Gateway discovered: " + json2);

                new Handler().postDelayed(new Runnable(){
                    @Override
                    public void run() {
                        publish(json + json2);
                    }
                }, 5000);
            }
        };
    }

目前合并后得到的结果是两个JSON字符串直接拼接:

{"address":"05:B2:68:5B:BC:92","batteryPower":-1,"distance":2.0765944282461007E9,"firmwareVersion":"-1","hashCodeBuilder":{"iConstant":37,"iTotal":17},"major":1,"minor":0,"proximity":"FAR","proximityUUID":"2686f39c-bada-4658-854a-a62e7e5e8b8d","rssi":-94,"shuffled":false,"timestamp":1680527328760,"txPower":11}"{deviceId : 67814f71b5bdb4d3}"

我期望将两者合并为一个完整的JSON对象:

{"address":"05:B2:68:5B:BC:92","batteryPower":-1,"distance":2.0765944282461007E9,"firmwareVersion":"-1","hashCodeBuilder":{"iConstant":37,"iTotal":17},"major":1,"minor":0,"proximity":"FAR","proximityUUID":"2686f39c-bada-4658-854a-a62e7e5e8b8d","rssi":-94,"shuffled":false,"timestamp":1680527328760,"txPower":11,"deviceId":"67814f71b5bdb4d3"}
解决方案

直接拼接字符串无法得到合法的JSON对象,必须在对象层面完成合并后再转为JSON字符串,以下是三种实用实现方式:

方法一:使用Gson的JsonObject合并

将ibeacon转换为JsonObject,手动添加deviceId属性后再转为JSON字符串:

private IBeaconListener createIBeaconListener() {
    return new SimpleIBeaconListener() {
        @Override
        public void onIBeaconDiscovered(IBeaconDevice ibeacon, IBeaconRegion region) {
            Gson gson = new Gson();
            // 将ibeacon转为JsonObject
            JsonObject beaconJson = gson.toJsonTree(ibeacon).getAsJsonObject();
            // 添加deviceId属性
            beaconJson.addProperty("deviceId", "67814f71b5bdb4d3");
            
            String mergedJson = gson.toJson(beaconJson);
            Log.i("Merged", "Merged JSON: " + mergedJson);

            new Handler().postDelayed(() -> publish(mergedJson), 5000);
        }
    };
}

方法二:借助Map中转

把ibeacon转为Map结构,放入deviceId后再转为JSON:

private IBeaconListener createIBeaconListener() {
    return new SimpleIBeaconListener() {
        @Override
        public void onIBeaconDiscovered(IBeaconDevice ibeacon, IBeaconRegion region) {
            Gson gson = new Gson();
            // 将ibeacon转为Map
            Map<String, Object> beaconMap = gson.fromJson(
                gson.toJson(ibeacon), 
                new TypeToken<Map<String, Object>>(){}.getType()
            );
            // 添加deviceId
            beaconMap.put("deviceId", "67814f71b5bdb4d3");
            
            String mergedJson = gson.toJson(beaconMap);
            Log.i("Merged", "Merged JSON: " + mergedJson);

            new Handler().postDelayed(() -> publish(mergedJson), 5000);
        }
    };
}

方法三:创建包含deviceId的实体类

如果业务中频繁需要该结构,可以定义扩展实体类:

// 定义扩展实体类
public class BeaconWithDeviceId {
    // 复制IBeaconDevice的所有属性
    private String address;
    private int batteryPower;
    private double distance;
    private String firmwareVersion;
    private HashMap<String, Integer> hashCodeBuilder;
    private int major;
    private int minor;
    private String proximity;
    private String proximityUUID;
    private int rssi;
    private boolean shuffled;
    private long timestamp;
    private int txPower;
    // 添加deviceId属性
    private String deviceId;

    // 构造方法:从IBeaconDevice拷贝属性并传入deviceId
    public BeaconWithDeviceId(IBeaconDevice ibeacon, String deviceId) {
        this.address = ibeacon.getAddress();
        this.batteryPower = ibeacon.getBatteryPower();
        this.distance = ibeacon.getDistance();
        this.firmwareVersion = ibeacon.getFirmwareVersion();
        this.hashCodeBuilder = ibeacon.getHashCodeBuilder();
        this.major = ibeacon.getMajor();
        this.minor = ibeacon.getMinor();
        this.proximity = ibeacon.getProximity();
        this.proximityUUID = ibeacon.getProximityUUID();
        this.rssi = ibeacon.getRssi();
        this.shuffled = ibeacon.isShuffled();
        this.timestamp = ibeacon.getTimestamp();
        this.txPower = ibeacon.getTxPower();
        this.deviceId = deviceId;
    }

    // 生成所有属性的getter和setter
    // ...
}

代码中使用该类:

private IBeaconListener createIBeaconListener() {
    return new SimpleIBeaconListener() {
        @Override
        public void onIBeaconDiscovered(IBeaconDevice ibeacon, IBeaconRegion region) {
            Gson gson = new Gson();
            BeaconWithDeviceId beaconWithId = new BeaconWithDeviceId(ibeacon, "67814f71b5bdb4d3");
            String mergedJson = gson.toJson(beaconWithId);
            
            Log.i("Merged", "Merged JSON: " + mergedJson);
            new Handler().postDelayed(() -> publish(mergedJson), 5000);
        }
    };
}

注意:如果IBeaconDevice是第三方库类无法直接继承,前两种方法会更便捷。

内容的提问来源于stack exchange,提问作者matchaLover

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.25 22:39:56