Java json对象中的json数组读取

Java json对象中的json数组读取,java,android,json,Java,Android,Json,我正在构建一个需要下载并与在线数据库同步的android应用程序,我正在将我的查询从该应用程序发送到一个php页面,该页面以JSON格式返回数据库中的相关行 Json java代码 public void onResponse(String response) { try { JSONObject object = new JSONObject

我正在构建一个需要下载并与在线数据库同步的android应用程序,我正在将我的查询从该应用程序发送到一个php页面,该页面以JSON格式返回数据库中的相关行

Json

java代码

                public void onResponse(String response) {
                        try {

                                    JSONObject object = new JSONObject(response);//TODO
               // 1st jsonarray     
           JSONArray empArray = object.getJSONArray("employee");
                                    Employees = new ArrayList<>();


                                    for (int i = 0; i < empArray.length(); i++) {
                                        JSONObject currentobject =empArray.getJSONObject(i);
                                        String name = currentobject.getString("name");
                                        String email = currentobject.getString("email");

//here i try to add a jsonarray to read department
 JSONArray deptArry =object.getJSONArray("department");
                                        ArrayList<String> deptt = new ArrayList<String>();
                                        for (int j = 0; j < deptArry.length(); j++) {
                                            deptt.add((String) deptArry.get(j));
                                    employee= new Employee(id,name,departement,email,phone);
                                        Employees.add(employee);
                }
                }
public void onResponse(字符串响应){
试一试{
JSONObject对象=新JSONObject(响应);//TODO
//第一圣安娜酒店
JSONArray empArray=object.getJSONArray(“员工”);
Employees=newarraylist();
对于(int i=0;i
如何解析我的json结果? 如何将此代码更正为正在运行?

请更换
JSONArray deptArry=object.getJSONArray(“部门”);
to
JSONArray deptArry=currentobject.getJSONArray(“部门”);
试试这个

使用这个

JSONArray deptArry = currentobject.getJSONArray("department");
JSONArray deptArry =object.getJSONArray("department");
而不是这个

JSONArray deptArry = currentobject.getJSONArray("department");
JSONArray deptArry =object.getJSONArray("department");
带有outpot的代码片段

JSONArray empArray = object.getJSONArray("employee");

for (int i = 0; i < empArray.length(); i++) {

  JSONObject currentobject = empArray.getJSONObject(i);
  String name = currentobject.getString("name");
  String email = currentobject.getString("email");

  Log.i("Name", ":" + name);
  Log.i("Email", ":" + email);

  JSONArray deptArry = currentobject.getJSONArray("department");

  for (int j = 0; j < deptArry.length(); j++) {

      Log.i("Dept", ":" + deptArry.get(j));
  }

}
JSONArray empArray=object.getJSONArray(“员工”);
对于(int i=0;i
输出


employee=新员工(id、姓名、部门、电子邮件、电话);
代码中指定的
department
在哪里?请尝试
employee=新员工(id、姓名、(字符串)deptary.get(1)、电子邮件、电话);Employees.add(员工)
超出此范围loop@joker_7请用输出检查我的答案。谢谢,Aswin P Ashok请在他的代码中添加您所做更改的描述?我可以看到您的代码与问题相同。@joker_7谢谢,很高兴帮助您。