我有一个使用这个库在 java 中实现的策略管理器服务器
com.google.apis:google-api-services-androidmanagement:v1-rev20211122-1.32.1
文档提到我可以在我的政策中设置一个openNetworkConfiguration,并且有一个支持它的功能。
Policy().setOpenNetworkConfiguration(Map<String, Object>)
一直不知道怎么给参数Map
如何将这样的数据放入上述参数中?有比上面链接更好的示例吗?
"openNetworkConfiguration": {
"NetworkConfigurations": [{
"GUID": "a",
"Name": "Example A",
"Type": "WiFi",
"WiFi": {
"SSID": "Example A",
"Security": "None",
"AutoConnect": true
}
}
}
在
Map<String, Object>
方法中为setOpenNetworkConfiguration
参数填写数据,可以使用Java的HashMap
手动创建嵌套结构
import java.util.HashMap;
import java.util.Map;
// ...
Map<String, Object> openNetworkConfig = new HashMap<>();
Map<String, Object> networkConfiguration = new HashMap<>();
Map<String, Object> wifiConfig = new HashMap<>();
wifiConfig.put("SSID", "Example A");
wifiConfig.put("Security", "None");
wifiConfig.put("AutoConnect", true);
networkConfiguration.put("GUID", "a");
networkConfiguration.put("Name", "Example A");
networkConfiguration.put("Type", "WiFi");
networkConfiguration.put("WiFi", wifiConfig);
openNetworkConfig.put("NetworkConfigurations", new Object[]{networkConfiguration});
Policy policy = new Policy();
policy.setOpenNetworkConfiguration(openNetworkConfig);
或者您可以使用 Gson 库将 JSON 字符串转换为
Map<String, Object>
import java.util.HashMap;
import java.util.Map;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import java.lang.reflect.Type;
// ...
String json = "{'openNetworkConfiguration': {'NetworkConfigurations': [{'GUID': 'a', 'Name': 'Example A', 'Type': 'WiFi', 'WiFi': {'SSID': 'Example A', 'Security': 'None', 'AutoConnect': true}}]}}";
Gson gson = new Gson();
Type type = new TypeToken<Map<String, Object>>() {}.getType();
Map<String, Object> openNetworkConfig = gson.fromJson(json, type);
Policy policy = new Policy();
policy.setOpenNetworkConfiguration(openNetworkConfig);