Java 如何提取JSON值?

Java 如何提取JSON值?,java,android,json,Java,Android,Json,我正在从android studio中的服务器获得错误响应。如何提取值 Json: { "error": { "phone": [ "The phone field is required." ] } } try { JSONArray arr = new JSONArray(response); for (int i = 0; i < arr.length(); i++) { JSONObject mJsonOb

我正在从android studio中的服务器获得错误响应。如何提取值

Json:

{
  "error": {
    "phone": [
      "The phone field is required."
    ]
  }
}
 try {
     JSONArray arr = new JSONArray(response);
     for (int i = 0; i < arr.length(); i++) {
        JSONObject mJsonObject = arr.getJSONObject(i);
        Log.d("OutPut", mJsonObject.getString("phone"));
     }
 } catch (JSONException e) {
     e.printStackTrace();                     
 }
代码:

{
  "error": {
    "phone": [
      "The phone field is required."
    ]
  }
}
 try {
     JSONArray arr = new JSONArray(response);
     for (int i = 0; i < arr.length(); i++) {
        JSONObject mJsonObject = arr.getJSONObject(i);
        Log.d("OutPut", mJsonObject.getString("phone"));
     }
 } catch (JSONException e) {
     e.printStackTrace();                     
 }
试试看{
JSONArray arr=新JSONArray(响应);
对于(int i=0;i
实际上,您的响应是一个
对象
而不是
数组
。请尝试以下内容:

String response = "{\"error\":{\"phone\":[\"The phone field is required.\"]}}";

try {
    JSONObject jsonObject = new JSONObject(response);
    JSONObject errorObject = jsonObject.optJSONObject("error");
    JSONArray phoneArray = errorObject.getJSONArray("phone");

    for (int i = 0; i < phoneArray.length(); i++) {
        String  errorString = phoneArray.optString(i);
        Log.d("OutPut", errorString);
    }
} catch (JSONException e) {
    e.printStackTrace();
}
String response=“{\'error\”:{\'phone\:[\'电话字段是必需的。\“]}”;
试一试{
JSONObject JSONObject=新JSONObject(响应);
JSONObject errorObject=JSONObject.optJSONObject(“错误”);
JSONArray phoneArray=errorObject.getJSONArray(“电话”);
对于(int i=0;i
有一些有用的答案你得到了什么错误?{“error”:{“phone”:[“phone字段是必需的。”]}他说{“error”:{“phone”:[“phone字段是必需的。”]}不是回答,而是错误