JSONObject中的Java / Android JSONArray返回null

I'm developing an app for android that communicates with a server and receives JSON responses from it. Recently I changed the responses structure to contain more information about the requests.

Initially my responses where just the response from a mysqli_query encoded to JSON by the php function json_encode, and looked like this:

[
  {"id":"31","description":"Hello"},
  {"id":"32","description":"World"},
]

And to retrieve the objects, I would just create a new JSONArray object by calling my_json_array=new JSONArray(response) and iterate over it.

But now, my new responses contain extra data, like this:

{  
  "error":false,
  "idCode":0,
  "message":[  
    {  
      "id":"32",
      "description":"Hello"
    },
    {  
      "id":"31",
      "description":"World"
    }
  ]
} 

So now, as far as I understand, I need to convert this response to a JSONObject, and then extract the message as a JSONarray object by calling my_json_object.getJSONArray("message");. I can extract the data from the "error" and the "idCode" fields normally, but when I try to extract the JSONArray from "message" it returns null, and no exceptions are thrown except for the NullPointerException.

I've validated the JSON string here, escaped the characters that could be problematic for java like the double quotes, removed uppercases, written the response as a single line, removed the double quotes surrounding the "id" value, used single quotes instead of double quotes, used harcoded string instead of the response from the server, checked the unicode representation of the string to see if contains any invalid characters (and it looks like it doesn't) and I don't know what else to do. Something to notice: the method toString() on the object returns the string "null", and based on the toString() method of the JSONObjects, it seems like the problem comes from the JSONStringer library, and going deeper on the classes it looks like the exception that there is a nesting problem, but I can't find any problem in my JSON string.

The important part of the code:

public String getResponse(){
  web_helper.sendRequest();
  //everything fine over here
  return web_helper.getResponse();
}

public void printData(){ throws JSONExpection
  JSONObject my_json_object=new JSONObject(getResponse());
  System.out.println("error: "+my_json_object.getBoolean("error"));
  System.out.println("idCode: "+my_json_object.getInt("idCode"));
  JSONArray my_json_array=my_json_object.getJSONArray("message");
  //not sure if the next part works because my_json_array is null
  for(int i=0; i<my_json_array.length(); i++){
    JSONObject obj=my_json_array.getJSONObject(i);
    System.out.println("id: "+obj.getInt("id"));
    System.out.println("description: "+obj.getString("description"));
  }
}

Any help is appreciated, thank you in advance!.

dongsheng8664
dongsheng8664 真?这就是我得到的{“error”:false,“idCode”:0,“message”:[{“id”:32,“description”:“hello”},{“id”:31,“description”:“世界”}]}。感谢您的时间!我很确定这个问题会因为我的无能而变得非常简单和愚蠢。
接近 2 年之前 回复
duanhao5038
duanhao5038 你的代码对我来说很完美。你真的收到了什么json?请记录jsonSystem.out.println(getResponse());在printData()方法中
接近 2 年之前 回复
Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问