添加到库后如何在Android Studio中使用JSONLibrary

时间:2019-01-04 06:54:08

标签: android android-studio

我必须使用JSONObject.getNames我下载了org.json jar并添加到android studio库中,但是它不起作用,但是当将相同的内容添加到netbeans中时,它就起作用了! Android Studio正在意识到这一点,完全感到沮丧,在此过程中浪费了半天时间。

enter image description here

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

1 个答案:

答案 0 :(得分:0)

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

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