将json保存到android中的数组键值

将json保存到android中的数组键值,android,Android,我对json有一个问题。 我从服务器获得以下响应: {"TESTS":true,"TESTS_VIEW":true,"ORDER":true,"PARAMETERS":true,"VIEW":true} 如何将这些数据保存在数组或其他东西中,使其具有schema:key-value JSONObject object = YourObjectHere; Map<String,Boolean> dict = new HashMap<String,Boolean>(); I

我对json有一个问题。 我从服务器获得以下响应:

{"TESTS":true,"TESTS_VIEW":true,"ORDER":true,"PARAMETERS":true,"VIEW":true}
如何将这些数据保存在数组或其他东西中,使其具有schema:key-value

JSONObject object = YourObjectHere;
Map<String,Boolean> dict = new HashMap<String,Boolean>();
Iterator it = object.keyes();

while( it.hasNext() ){
 String key = it.next();
 String value = object.get(key);
 dict.put( key, value );
}
解决方案,或多或少//未签入IDE而编写,因此可能包含错误/错误


解决方案,或多或少//未签入IDE而编写,因此可能包含bug/错误

嗯,我不知道您为什么要这样做。A给了你确切的信息,看看:

不过,如果要迭代所有值,可以执行以下操作:

Map<String, Object> map = new HashMap<String, Object>();
Iterator<String> keys = json.keys();
for(String key : keys) {
    try {
        Object value = json.get(key);
        map.put(key, value);
    }
    catch (JSONException e) {
        // Something went wrong!
    }   
}

嗯,我不知道你为什么想要这个。A给了你确切的信息,看看:

不过,如果要迭代所有值,可以执行以下操作:

Map<String, Object> map = new HashMap<String, Object>();
Iterator<String> keys = json.keys();
for(String key : keys) {
    try {
        Object value = json.get(key);
        map.put(key, value);
    }
    catch (JSONException e) {
        // Something went wrong!
    }   
}
等等,


等等,

您可以将此函数与httpResponse一起使用。这是您的json字符串:

public static YourModel parseJson(String httpResponse) {
    YourModel objObject = new YourModel();
    try {
        JSONArray jsonArrayData = new JSONArray(httpResponse);
        if (jsonArrayData.length() >= 1) {
            for (int i = 0; i < jsonArrayData.length(); i++) {
                JSONObject object = new JSONObject(jsonArrayData.get(0));
                // Setting value by key json
                objObject.setAtrr(object.getString("YourKey"));
            }
        }
    } catch (JSONException e) {
        e.printStackTrace();
        return null;
    }
    return objObject;
}

您可以将此函数用于httpResponse,它是您的json字符串:

public static YourModel parseJson(String httpResponse) {
    YourModel objObject = new YourModel();
    try {
        JSONArray jsonArrayData = new JSONArray(httpResponse);
        if (jsonArrayData.length() >= 1) {
            for (int i = 0; i < jsonArrayData.length(); i++) {
                JSONObject object = new JSONObject(jsonArrayData.get(0));
                // Setting value by key json
                objObject.setAtrr(object.getString("YourKey"));
            }
        }
    } catch (JSONException e) {
        e.printStackTrace();
        return null;
    }
    return objObject;
}

你想要一个保存这些值的类吗?不,我想要数组或其他东西。例如:对于键测试,我想要得到值true。但是如果你有一个对象,为什么需要数组?你想要一个保存这些值的类吗?不,我想要数组或其他东西,例如:对于关键测试,我想要得到值true。但如果您有一个对象,为什么需要数组?