douao7937 2018-09-27 20:13
浏览 195

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!.

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 请教一下各位,为什么我这个没有实现模拟点击
    • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
    • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
    • ¥20 有关区间dp的问题求解
    • ¥15 多电路系统共用电源的串扰问题
    • ¥15 slam rangenet++配置
    • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
    • ¥15 ubuntu子系统密码忘记
    • ¥15 保护模式-系统加载-段寄存器
    • ¥15 电脑桌面设定一个区域禁止鼠标操作