Android 如何分解字符串响应?

Android 如何分解字符串响应?,android,json,rest,Android,Json,Rest,我从以下@Override方法接收到一个字符串响应 @Override publc void onSuccess(String response) { .... } 我面临的冲突是,我不知道如何将这个响应分解为关键值对。这是响应的一个示例 {“action”:{“generic_message”:“test generic message”},“domains”:{“key_example_one”:“https:/google.com”,“api_root_url”:https://test

我从以下@Override方法接收到一个字符串响应

@Override
publc void onSuccess(String response) {
....
}
我面临的冲突是,我不知道如何将这个响应分解为关键值对。这是响应的一个示例

{“action”:{“generic_message”:“test generic message”},“domains”:{“key_example_one”:“https:/google.com”,“api_root_url”:https://test.com/new/0.2/json“}”,第页:null}

我尝试将字符串转换为JSONObject,然后将JSONObject添加到JSONArray

JSONObject mJObj;
try {
    mJObj = new JSONObject(response);
    JSONArray mJArry = mJObj.getJSONArray("action");

    for (int i = 0; i < mJArry.length(); i++) {
        JSONObject newObj = mJArry.getJSONObject(i);    
        String test2 = newObj.getString("generic_example_for_all_platforms");

    }
} catch (JSONException e) {
    e.printStackTrace();
}

提前感谢

您确实想了解更多关于JSON的信息

首先要知道的是{}等于一个Json对象,[]等于一个Json数组

try {
    JSONObject mJObj = new JSONObject(response);
    JSONObject actionJsonObject = mJObj.getJSONObject("action");
    String generic_message = actionJsonObject.getString("generic_message");
    JSONObject domainsJsonObject = mJObj.getJSONObject("domains");
    String key_example_one = domainsJsonObject.getString("key_example_one");
    String api_root_url = domainsJsonObject.getString("api_root_url");
    String page = mJObj.getString("page");
} catch (JSONException e) {
    e.printStackTrace();
}

您确实想了解更多关于JSON的信息

首先要知道的是{}等于一个Json对象,[]等于一个Json数组

try {
    JSONObject mJObj = new JSONObject(response);
    JSONObject actionJsonObject = mJObj.getJSONObject("action");
    String generic_message = actionJsonObject.getString("generic_message");
    JSONObject domainsJsonObject = mJObj.getJSONObject("domains");
    String key_example_one = domainsJsonObject.getString("key_example_one");
    String api_root_url = domainsJsonObject.getString("api_root_url");
    String page = mJObj.getString("page");
} catch (JSONException e) {
    e.printStackTrace();
}
退房

编辑:
只是看到您不能移动到其他Json处理器。属性“action”保存的是JSONObject而不是JSONArray。关于JSONObject JSONObject=mJObj.getJSONObject(“操作”)如何签出

编辑:

只是看到您不能移动到其他Json处理器。属性“action”保存的是JSONObject而不是JSONArray。那么如何处理org.json库呢;我建议调查gson或jacksonYea,实际上我通常使用loopj,但这是工作要求。我只需要和他们现有的一起工作。啊,那太糟糕了。当我运行上面的代码时,我得到错误“org.json.JSONException:JSONObject[“action”]不是JSONArray”。你确定你的问题/json字符串中的错误描述是正确的吗?是的,我直接发布了响应。generic_message是键,值是test generic message.org.json库不是最适合使用的;我建议调查gson或jacksonYea,实际上我通常使用loopj,但这是工作要求。我只需要和他们现有的一起工作。啊,那太糟糕了。当我运行上面的代码时,我得到错误“org.json.JSONException:JSONObject[“action”]不是JSONArray”。你确定你的问题/json字符串中的错误描述是正确的吗?是的,我直接发布了响应。generic_message是键,值为test generic message。
JsonObject jsonObject = new JsonParser().parse(jsonString).getAsJsonObject();