(Android Volley)JSONObject无法转换为JSONArray

时间:2019-06-25 19:04:20

标签: android json parsing nested android-volley

我在解析android中的JSON数据时遇到问题。

我的android代码

JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(Request.Method.GET, server_url,null,
            new Response.Listener<JSONObject>() {
                @Override
                public void onResponse(JSONObject response) {
                    try {

                JSONArray jsonArray1=response.getJSONArray("employee");

                        for(int i=0;i<jsonArray1.length();i++)
                        {
                  JSONObject employe=jsonArray1.getJSONObject(i);

                 Log.d(TAG, "onResponse emp: "+employe.getString("name"));
                        }
                        }
                    catch (JSONException e) {
                        e.printStackTrace();
                    }
                }
            }

我的JSON数据

{"orgs":[{"id":1551,"employeeId":1501,"orgId":"1151","employeeOrgMembershipStatus":"MEMBER","employeeRole":"SUPER_ADMIN","lMb":"alvin","lMd":"2019-06-01T05:41:00Z"}],"employee":{"id":1501,"employeeStatus":"ACTIVE","userId":"SU9iegDhr3b9r3gCUZzXl9eQjEJ2","name":"Alvin J Babu","cB":"alvin","cD":"2019-06-24T05:41:00Z","lMb":"alvin","lMd":"2019-06-27T05:41:00Z","computedTotalActivityScore":5,"stars":2,"jobTitle":"Developer","visibility":"ORGANIZATION","asset":null,"reportsTo":null},"teams":[{"id":1601,"employeeId":1501,"teamId":"1251","employeeTeamMembershipStatus":"MEMBER","lMb":"alvin","lMd":"2019-06-25T05:41:00Z"}],"status":1}

我需要解析员工数组并从中获取名称。

2 个答案:

答案 0 :(得分:1)

那是你的模型

-----------------------------------com.example.Employee.java-----------------------------------

            package com.example;

            import com.google.gson.annotations.Expose;
            import com.google.gson.annotations.SerializedName;

    public class Employee {

        @SerializedName("id")
        @Expose
        private Integer id;
        @SerializedName("employeeStatus")
        @Expose
        private String employeeStatus;
        @SerializedName("userId")
        @Expose
        private String userId;
        @SerializedName("name")
        @Expose
        private String name;
        @SerializedName("cB")
        @Expose
        private String cB;
        @SerializedName("cD")
        @Expose
        private String cD;
        @SerializedName("lMb")
        @Expose
        private String lMb;
        @SerializedName("lMd")
        @Expose
        private String lMd;
        @SerializedName("computedTotalActivityScore")
        @Expose
        private Integer computedTotalActivityScore;
        @SerializedName("stars")
        @Expose
        private Integer stars;
        @SerializedName("jobTitle")
        @Expose
        private String jobTitle;
        @SerializedName("visibility")
        @Expose
        private String visibility;
        @SerializedName("asset")
        @Expose
        private Object asset;
        @SerializedName("reportsTo")
        @Expose
        private Object reportsTo;

        public Integer getId() {
            return id;
        }

        public void setId(Integer id) {
            this.id = id;
        }

        public String getEmployeeStatus() {
            return employeeStatus;
        }

        public void setEmployeeStatus(String employeeStatus) {
            this.employeeStatus = employeeStatus;
        }

        public String getUserId() {
            return userId;
        }

        public void setUserId(String userId) {
            this.userId = userId;
        }

        public String getName() {
            return name;
        }

        public void setName(String name) {
            this.name = name;
        }

        public String getCB() {
            return cB;
        }

        public void setCB(String cB) {
            this.cB = cB;
        }

        public String getCD() {
            return cD;
        }

        public void setCD(String cD) {
            this.cD = cD;
        }

        public String getLMb() {
            return lMb;
        }

        public void setLMb(String lMb) {
            this.lMb = lMb;
        }

        public String getLMd() {
            return lMd;
        }

        public void setLMd(String lMd) {
            this.lMd = lMd;
        }

        public Integer getComputedTotalActivityScore() {
            return computedTotalActivityScore;
        }

        public void setComputedTotalActivityScore(Integer computedTotalActivityScore) {
            this.computedTotalActivityScore = computedTotalActivityScore;
        }

        public Integer getStars() {
            return stars;
        }

        public void setStars(Integer stars) {
            this.stars = stars;
        }

        public String getJobTitle() {
            return jobTitle;
        }

        public void setJobTitle(String jobTitle) {
            this.jobTitle = jobTitle;
        }

        public String getVisibility() {
            return visibility;
        }

        public void setVisibility(String visibility) {
            this.visibility = visibility;
        }

        public Object getAsset() {
            return asset;
        }

        public void setAsset(Object asset) {
            this.asset = asset;
        }

        public Object getReportsTo() {
            return reportsTo;
        }

        public void setReportsTo(Object reportsTo) {
            this.reportsTo = reportsTo;
        }

    }
    -----------------------------------com.example.Example.java-----------------------------------

            package com.example;

            import java.util.List;
            import com.google.gson.annotations.Expose;
            import com.google.gson.annotations.SerializedName;

    public class Example {

        @SerializedName("orgs")
        @Expose
        private List<Org> orgs = null;
        @SerializedName("employee")
        @Expose
        private Employee employee;
        @SerializedName("teams")
        @Expose
        private List<Team> teams = null;
        @SerializedName("status")
        @Expose
        private Integer status;

        public List<Org> getOrgs() {
            return orgs;
        }

        public void setOrgs(List<Org> orgs) {
            this.orgs = orgs;
        }

        public Employee getEmployee() {
            return employee;
        }

        public void setEmployee(Employee employee) {
            this.employee = employee;
        }

        public List<Team> getTeams() {
            return teams;
        }

        public void setTeams(List<Team> teams) {
            this.teams = teams;
        }

        public Integer getStatus() {
            return status;
        }

        public void setStatus(Integer status) {
            this.status = status;
        }

    }
    -----------------------------------com.example.Org.java-----------------------------------

            package com.example;

            import com.google.gson.annotations.Expose;
            import com.google.gson.annotations.SerializedName;

    public class Org {

        @SerializedName("id")
        @Expose
        private Integer id;
        @SerializedName("employeeId")
        @Expose
        private Integer employeeId;
        @SerializedName("orgId")
        @Expose
        private String orgId;
        @SerializedName("employeeOrgMembershipStatus")
        @Expose
        private String employeeOrgMembershipStatus;
        @SerializedName("employeeRole")
        @Expose
        private String employeeRole;
        @SerializedName("lMb")
        @Expose
        private String lMb;
        @SerializedName("lMd")
        @Expose
        private String lMd;

        public Integer getId() {
            return id;
        }

        public void setId(Integer id) {
            this.id = id;
        }

        public Integer getEmployeeId() {
            return employeeId;
        }

        public void setEmployeeId(Integer employeeId) {
            this.employeeId = employeeId;
        }

        public String getOrgId() {
            return orgId;
        }

        public void setOrgId(String orgId) {
            this.orgId = orgId;
        }

        public String getEmployeeOrgMembershipStatus() {
            return employeeOrgMembershipStatus;
        }

        public void setEmployeeOrgMembershipStatus(String employeeOrgMembershipStatus) {
            this.employeeOrgMembershipStatus = employeeOrgMembershipStatus;
        }

        public String getEmployeeRole() {
            return employeeRole;
        }

        public void setEmployeeRole(String employeeRole) {
            this.employeeRole = employeeRole;
        }

        public String getLMb() {
            return lMb;
        }

        public void setLMb(String lMb) {
            this.lMb = lMb;
        }

        public String getLMd() {
            return lMd;
        }

        public void setLMd(String lMd) {
            this.lMd = lMd;
        }

    }
    -----------------------------------com.example.Team.java-----------------------------------

            package com.example;

            import com.google.gson.annotations.Expose;
            import com.google.gson.annotations.SerializedName;

    public class Team {

        @SerializedName("id")
        @Expose
        private Integer id;
        @SerializedName("employeeId")
        @Expose
        private Integer employeeId;
        @SerializedName("teamId")
        @Expose
        private String teamId;
        @SerializedName("employeeTeamMembershipStatus")
        @Expose
        private String employeeTeamMembershipStatus;
        @SerializedName("lMb")
        @Expose
        private String lMb;
        @SerializedName("lMd")
        @Expose
        private String lMd;

        public Integer getId() {
            return id;
        }

        public void setId(Integer id) {
            this.id = id;
        }

        public Integer getEmployeeId() {
            return employeeId;
        }

        public void setEmployeeId(Integer employeeId) {
            this.employeeId = employeeId;
        }

        public String getTeamId() {
            return teamId;
        }

        public void setTeamId(String teamId) {
            this.teamId = teamId;
        }

        public String getEmployeeTeamMembershipStatus() {
            return employeeTeamMembershipStatus;
        }

        public void setEmployeeTeamMembershipStatus(String employeeTeamMembershipStatus) {
            this.employeeTeamMembershipStatus = employeeTeamMembershipStatus;
        }

        public String getLMb() {
            return lMb;
        }

        public void setLMb(String lMb) {
            this.lMb = lMb;
        }

        public String getLMd() {
            return lMd;
        }

        public void setLMd(String lMd) {
            this.lMd = lMd;
        }

    }

您可以使用此网站通过JSON响应制作模型

http://www.jsonschema2pojo.org/

答案 1 :(得分:1)

首先使用jsonprettyprint.com之类的网站轻松读取json。

这是更具可读性的json:

{
  "orgs": [
    {
      "id": 1551,
      "employeeId": 1501,
      "orgId": "1151",
      "employeeOrgMembershipStatus": "MEMBER",
      "employeeRole": "SUPER_ADMIN",
      "lMb": "alvin",
      "lMd": "2019-06-01T05:41:00Z"
    }
  ],
  "employee": {
    "id": 1501,
    "employeeStatus": "ACTIVE",
    "userId": "SU9iegDhr3b9r3gCUZzXl9eQjEJ2",
    "name": "Alvin J Babu",
    "cB": "alvin",
    "cD": "2019-06-24T05:41:00Z",
    "lMb": "alvin",
    "lMd": "2019-06-27T05:41:00Z",
    "computedTotalActivityScore": 5,
    "stars": 2,
    "jobTitle": "Developer",
    "visibility": "ORGANIZATION",
    "asset": null,
    "reportsTo": null
  },
  "teams": [
    {
      "id": 1601,
      "employeeId": 1501,
      "teamId": "1251",
      "employeeTeamMembershipStatus": "MEMBER",
      "lMb": "alvin",
      "lMd": "2019-06-25T05:41:00Z"
    }
  ],
  "status": 1
}

在JSON内,受{}约束的任何东西都是JSONObject,受[]约束的任何东西都是JSONArray。

就您的情况而言,如您在上述JSON中看到的那样,键“员工”具有JSONObject值,而不是您在代码中编写的JSONArray。

获取“员工”密钥的JSONObject后,您可以使用employee.getString("name")直接从中获取“名称”字符串。

做出上述更改后,您的代码将如下所示:

public void onResponse(JSONObject response) {
    try {
        JSONObject employee=response.getJSONObject("employee");
        String employeeName=employee.getString("name");
        Log.d(TAG, "onResponse emp: "+employeeName);
    }
    catch (JSONException e) {
        e.printStackTrace();
    }
}