android - 添加到库后如何在 Android Studio 中使用 JSONLibrary
问题描述
我必须使用JSONObject.getNames
我下载org.json
的 jar 并添加到 android studio 的库中,但它无法正常工作,但是当将其添加到 netbeans 时它正在工作!Android工作室正在识别。完全沮丧。在这迷失了半天。
static JSONObject convert(JSONObject initial) {
Map<String, Map<String, List<Map<String, Object>>>> stateToCityToAddresses = new HashMap<>();
String[] codes = JSONObject.getNames(initial);//getNames not recognisable
for (String code : codes) {
JSONObject state = initial.getJSONObject(code);
String stateName = state.getString("STATE");
String cityName = state.getString("CITY");
String ifsc = state.getString("IFSC");
String contact = state.getString("BRANCH");
List<Map<String, Object>> addresses = stateToCityToAddresses
.computeIfAbsent(stateName, sn -> new HashMap<>())
.computeIfAbsent(cityName, cn -> new ArrayList<>());
Map<String, Object> address = new HashMap<>();
address.put("ID", ifsc);
address.put("BRANCH", contact);
address.put("CODE", code);
addresses.add(address);
}
JSONObject result = new JSONObject(stateToCityToAddresses);
return result;
}
解决方案
暂时可以使用这个简单的方法来获取密钥。
public String[] getKeysFromJSONObject(JSONObject obj){
List<String> keys = new ArrayList<>();
Iterator<String> iter = obj.keys(); //This should be the iterator you want.
while(iter.hasNext()){
keys.add(iter.next());
return keys.toArray(new String[0]);
}
推荐阅读
- javascript - 为什么本机 javascript 函数 sort() 中的第一项 - 在我的数组中是第二项?
- python - 无法使用硒刮桌子
- html - 在手风琴按钮内对齐 2 个不同的元素 - Bootstrap 5
- typescript - 如何导入用 TypeScript 编写的 Svelte 库
- python - 添加一个层来切片输入数据keras python
- snowflake-cloud-data-platform - 如何使用时区名称从 timestamp_ntz 构造 timestamp_tz
- docker - 有没有办法将 aws-sam 配置为通过 VPN 连接连接到服务器?
- javascript - 什么决定了数据类型是 JavaScript 中的对象还是原始数据类型?
- amazon-web-services - 如何在 CloudFormation 模板中引用现有安全组
- python - 我的 python 代码的“int”和“NoneType”实例之间不支持“>=”