首页 > 解决方案 > 添加到库后如何在 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;
    }

标签: androidandroid-studio

解决方案


暂时可以使用这个简单的方法来获取密钥。

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]);
}

推荐阅读