无法在JsonArray中解析JsonArray

时间:2017-11-29 11:31:28

标签: android json parsing

您好我正在尝试解析另一个json数组中的json数组。帮助我。

我的Json数组是

   "results":[  
  {  
     "percentcompleted":0,
     "requirers":[  
        {  
           "activityContext":null,
           "auditReason":null,
           "assignee":{  
              "sourceType":"InternalPerson",
              "activitySourceType":{  
                 "listId":"sysli000000000003037",
                 "sourceType":0,
                 "key":"0",
                 "displayName":"Person"
              },
              "locale":{  
                 "id":"local000000000000001",
                 "displayName":"English"
              },
              "id":"emplo000000006156648",
              "displayName":"Venkat Rao"
           },
           "targetDate":null,
           "assignment":{  
              "id":"stcur000000000003182",
              "displayName":null
           },
           "learningEvent":{  
              "id":"curra000000000003001",
              "displayName":"Studieplan_test för OKQ8"
           },
           "jobTypeId":null,
           "listIds":null,
           "assignedOn":1502084325000,
           "source":{  
              "sourceType":"InternalPerson",
              "activitySourceType":{  
                 "listId":"sysli000000000003037",
                 "sourceType":0,
                 "key":"0",
                 "displayName":"Person"
              },
              "locale":{  
                 "id":"local000000000000001",
                 "displayName":"English"
              },
              "id":"emplo000000006156648",
              "displayName":"Venkat Rao"
           },
           "required":true,
           "actions":null,
           "id":"currh000000000014343"
        }
     ],
     "basicdetail":{  
        "revokedOn":null,
        "need_recert":false,
        "curriculum":{  
           "id":"curra000000000003001",
           "displayName":"Studieplan_test för OKQ8"
        },
        "recert_window_starts_on":null,
        "createdBy":"vrao",
        "updatedBy":"vrao",
        "createdOn":1502084325000,
        "updatedOn":1508357353000,
        "status":{  
           "intStatus":400,
           "description":"Assigned",
           "displayName":"Assigned"
        },
        "type":"CURRICULUM",
        "path":{  
           "id":"track000000000003081",
           "displayName":"Studieplan"
        },
        "acquiredOn":null,
        "assignee":{  
           "sourceType":"InternalPerson",
           "activitySourceType":{  
              "listId":"sysli000000000003037",
              "sourceType":0,
              "key":"0",
              "displayName":"Person"
           },
           "locale":{  
              "id":"local000000000000001",
              "displayName":"English"
           },
           "id":"emplo000000006156648",
           "displayName":"Venkat Rao"
        },
        "targetDate":null,
        "startedOn":null,
        "assignedOnDate":1502084325000,
        "heldCertNo":"00003787",
        "owner":{  
           "sourceType":"InternalPerson",
           "activitySourceType":{  
              "listId":"sysli000000000003037",
              "sourceType":0,
              "key":"0",
              "displayName":"Person"
           },
           "locale":{  
              "id":"local000000000000001",
              "displayName":"English"
           },
           "id":"emplo000000006156648",
           "displayName":"Venkat Rao"
        },
        "id":"stcur000000000003182",
        "assigneeAdditionalDetails":{  
           "person_no":"VRAO",
           "username":"VRAO"
        }
     },
     "isgranted":null
  },

我的代码解析上面的数据是

public class CurriculaParser {
public ArrayList<CurriculaData> getData(String response)
{
    ArrayList<CurriculaData> dataList = new ArrayList<>();
    try {
        JSONObject mainObj = new JSONObject(response);
        JSONArray array = mainObj.getJSONArray("results");
        CurriculaData data;
        for(int i = 0;i<array.length();i++)
        {
            data = new CurriculaData();
            JSONObject resObj = array.getJSONObject(i);
            JSONArray reqArray = resObj.getJSONArray("requires");
            for( int j =0;j<reqArray.length();j++) {
                JSONObject reqObj = reqArray.getJSONObject(j);
                JSONObject innerObj = reqObj.getJSONObject("learningEvent");
                data.setTitle(innerObj.getString("displayName"));
                data.setAssignedOn(reqObj.getString("assignedOn"));

            }
            JSONArray basicDetailArray = resObj.getJSONArray("basicdetail");
            for(int k=0;k<basicDetailArray.length();k++) {
                JSONObject basicDetailObj = basicDetailArray.getJSONObject(k);
                JSONObject statusObj = basicDetailObj.getJSONObject("status");
                data.setStatus(statusObj.getString("displayName"));
                data.setAcquiredOn(basicDetailObj.getString("acquiredOn"));
                JSONObject assignObj = basicDetailObj.getJSONObject("assignee");
                data.setAssignedBy(assignObj.getString("displayName"));
            }
            dataList.add(data);


        }
    } catch (Exception e) {
        e.printStackTrace();
    }

    return dataList;
}

最后我得到了像

这样的错误

org.json.JSONException:没有值

让我知道我所做的错误,我是一个初学者,在另一个jsonarray中处理jsonarray

2 个答案:

答案 0 :(得分:1)

  

org.json.JSONException:没有值

JSONException

  • KEY 查找不匹配

您应该传递 requirers ,而不是 requires

  JSONArray reqArray = resObj.getJSONArray("requirers");

答案 1 :(得分:1)

修复了需要的TYPO错误

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="row filterRecords">
      <div class="input-group col-xs-6 col-sm-6 col-md-3 mb-3">
        <input type="text" id="searchName" class="form-control" placeholder="Search Name">
      </div>
      <div class="input-group col-sm-6 col-md-3 mb-3">
        <input type="text" id="searchLang" class="form-control" placeholder="Search Language">
      </div>
      <div class="input-group col-sm-6 col-md-3 mb-3">
        <input type="text" id="yearMin" class="form-control start" placeholder="Search Start Year">
      </div>
      <div class="input-group col-sm-6 col-md-3 mb-3">
        <input type="text" id="yearMax" class="form-control end" placeholder="Search End Year">
      </div>
    </div>
    <div class="row justify-content-md-center">
      <div class="col-md-auto">
        <button class="btn btn-sm btn-default" type="button" id="resetFilter">Reset Filters</button>
        <hr>
      </div>
    </div>