Gson不会序列化MAC地址。

huangapple go评论105阅读模式
英文:

Gson doesn't serialyse mac adresses

问题

我想保存蓝牙设备的Mac地址,所以我使用Gson进行序列化,将其转换为String类型,但没有任何返回。

Gson gson = new Gson();
Log.d("my device", String.valueOf(myBluetoothDevice));   //93:39:1B:04:00:71
String dev = gson.toJson(myBluetoothDevice, BluetoothDevice.class);
Log.d("my device after", dev);   //{}

谢谢帮助

英文:

I want to save the Mac Adress of a Bluetooth device, so I serialize it with Gson to convert it into a String type but it returns nothing.

Gson gson = new Gson();
Log.d("my device", String.valueOf(myBluetoothDevice));   //93:39:1B:04:00:71
String dev = gson.toJson(myBluetoothDevice, BluetoothDevice.class);
Log.d("my device after", dev);   //{}

Thanks for help

答案1

得分: 2

获取BluetoothDevice的地址,请调用getAddress方法:

String address = myBluetoothDevice.getAddress();
// address = 00:11:22:AA:BB:CC

如果您想将字符串转换为JSON表示形式的字符串,基本上只需添加引号,那么您可以使用gson.toJson()

String addressInJson = gson.toJson(myBluetoothDevice.getAddress());
// addressInJson = "00:11:22:AA:BB:CC"
英文:

To get the address of a BluetoothDevice, call the getAddress method:

String address = myBluetoothDevice.getAddress();
// address = 00:11:22:AA:BB:CC

If you want to convert the String into a JSON representation of the string, which basically just adds quotes, then you can use gson.toJson():

String addressInJson = gson.toJson(myBluetoothDevice.getAddress());
// addressInJson = "00:11:22:AA:BB:CC"

huangapple
  • 本文由 发表于 2020年8月14日 22:53:07
  • 转载请务必保留本文链接:https://go.coder-hub.com/63415135.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定