Gson().tojson() 返回 {} 大括号
Gson().tojson() returning {} braces
请有人帮我解决问题..我想将 BluetoothDevice 对象存储到 SharedPrefrences 中,这样下次我打开应用程序时就不需要 select 配对设备 again.For 我必须将此 BluetoothDevice 对象转换为 String 变量,然后将其存储到 SharedPrefrence 以供使用我用过
Gson gson = new Gson();
String json = gson.toJson(selectedDevice,BluetoothDevice.class) ;
其中 selectedDevice 是 BluetoothDevice class 的对象。但是在 运行 代码之后,我在 json 变量中得到了 {} 大括号。谁能知道为什么会这样。
假设您指的是 https://android.googlesource.com/platform/frameworks/base/+/master/core/java/android/bluetooth/BluetoothDevice.java,它包含静态字段,但是,
Java 序列化仅序列化对象的 non-static 和 non-transient 字段
但如果你真的需要它,你可以尝试同样的方法:
BluetoothDevice selectedDevice = new BluetoothDevice();
GsonBuilder gsonBuilder = new GsonBuilder();
gsonBuilder.excludeFieldsWithModifiers(java.lang.reflect.Modifier.TRANSIENT);
Gson gson = gsonBuilder.create();
String json = gson.toJson(selectedDevice, BluetoothDevice.class);
android.bluetooth.BluetoothDevice
中的几乎所有属性都是static final
。因此,这些属性不依赖于实例。
一个 address
属性 可用并且取决于实例变量。您可以使用 getAddress()
.
将此 address
属性 保存到您的 shared-preferences
如果启用蓝牙,您还可以通过getName()
获取设备名称。
所以,我建议您在 shared-preferences;
中保存这样的内容
class BluetoothDeviceInformation {
String address;
String name;
@RequiresPermission(Manifest.permission.BLUETOOTH)
public BluetoothDeviceInformation (
BluetoothDevice device
) {
this.address = device.getAddress();
this.name = device.getName();
}
}
您可以使用以下方式创建此实例:
BluetoothDeviceInformation deviceInformation =
new BluetoothDeviceInformation(selectedBluetoothDevice);
还有其他方法,如getType()
、getAlias()
等。如果您需要它们的信息,只需将它们添加到您的BluetoothDeviceInformation
class.
请有人帮我解决问题..我想将 BluetoothDevice 对象存储到 SharedPrefrences 中,这样下次我打开应用程序时就不需要 select 配对设备 again.For 我必须将此 BluetoothDevice 对象转换为 String 变量,然后将其存储到 SharedPrefrence 以供使用我用过
Gson gson = new Gson();
String json = gson.toJson(selectedDevice,BluetoothDevice.class) ;
其中 selectedDevice 是 BluetoothDevice class 的对象。但是在 运行 代码之后,我在 json 变量中得到了 {} 大括号。谁能知道为什么会这样。
假设您指的是 https://android.googlesource.com/platform/frameworks/base/+/master/core/java/android/bluetooth/BluetoothDevice.java,它包含静态字段,但是,
Java 序列化仅序列化对象的 non-static 和 non-transient 字段
但如果你真的需要它,你可以尝试同样的方法:
BluetoothDevice selectedDevice = new BluetoothDevice();
GsonBuilder gsonBuilder = new GsonBuilder();
gsonBuilder.excludeFieldsWithModifiers(java.lang.reflect.Modifier.TRANSIENT);
Gson gson = gsonBuilder.create();
String json = gson.toJson(selectedDevice, BluetoothDevice.class);
android.bluetooth.BluetoothDevice
中的几乎所有属性都是static final
。因此,这些属性不依赖于实例。
一个 address
属性 可用并且取决于实例变量。您可以使用 getAddress()
.
address
属性 保存到您的 shared-preferences
如果启用蓝牙,您还可以通过getName()
获取设备名称。
所以,我建议您在 shared-preferences;
中保存这样的内容class BluetoothDeviceInformation {
String address;
String name;
@RequiresPermission(Manifest.permission.BLUETOOTH)
public BluetoothDeviceInformation (
BluetoothDevice device
) {
this.address = device.getAddress();
this.name = device.getName();
}
}
您可以使用以下方式创建此实例:
BluetoothDeviceInformation deviceInformation =
new BluetoothDeviceInformation(selectedBluetoothDevice);
还有其他方法,如getType()
、getAlias()
等。如果您需要它们的信息,只需将它们添加到您的BluetoothDeviceInformation
class.