尝试从JSON响应文本中选择数据

时间:2017-04-04 08:14:02

标签: javascript json api parsing

我试图从我的JSON响应文本中选择一些数据,如下所示:

{
   "status": "success",
   "reservations": [
      {
         "id": "22959",
         "subject": "SubjectName",
         "modifiedDate": "2017-04-03T06:04:24",
         "startDate": "2017-04-03T12:15:00",
         "endDate": "2017-04-03T17:00:00",
         "resources": [
            {
               "id": "17",
               "type": "room",
               "code": "codeName",
               "parent": {
                  "id": "2",
                  "type": "building",
                  "code": "buildingName",
                  "name": ""
               },
               "name": ""
            },
            {
               "id": "2658",
               "type": "student_group",
               "code": "groupCode",
               "name": "groupName"
            },
            {
               "id": "2446",
               "type": "student_group",
               "code": "groupCode",
               "name": "groupName"
            },
            {
               "id": "3137",
               "type": "realization",
               "code": "codeName",
               "name": ""
            },
            {
               "id": "3211",
               "type": "realization",
               "code": "codeName",
               "name": "name"
            }
         ],
         "description": ""
      },
{
         "id": "22960",
         "subject": "subjectName",
         "modifiedDate": "2017-04-04T06:04:33",
         "startDate": "2017-04-04T10:00:00",
         "endDate": "2017-04-04T16:00:00",
         "resources": [
            {
               "id": "17",
               "type": "room",
               "code": "codeName",
               "parent": {
                  "id": "2",
                  "type": "building",
                  "code": "codeName",
                  "name": ""
               },
               "name": ""
            },
            {
               "id": "2658",
               "type": "student_group",
               "code": "groupCode",
               "name": "groupName"
            },
            {
               "id": "2446",
               "type": "student_group",
               "code": "groupCode",
               "name": "groupName"
            }
         ],
         "description": ""
      }
]
}

我一直在尝试使用JSON.parse()并使用for循环遍历响应文本但没有成功。我需要选择主题名称,房间名称,建筑物名称和student_group名称。

这就是我的代码目前的样子:

var getData = {
                "startDate":,
                "endDate":,
                "studentGroup": [
                 ""]
            };



var data = new XMLHttpRequest();
           data.onreadystatechange = function () {             
               if (data.readyState == 4 && data.status == 200) {

                   try {
                       // Parse JSON
                       var json = JSON.parse(data.responseText);
                       // for-loops
                       for (var i = 0; i < json.reservations.length; i++) {
                           for (var x = 0; x < json.reservations[i].length; 
                            x++) { 
                               document.getElementById("test").innerHTML = 
                               json.reservations[i].subject;
                           }
                       }

                   } catch (err) {
                       console.log(err.message);
                       return;
                   }
               }
           };           

            // JSON query
           data.open("POST", "URL", true, "APIKEY", "PASS");
           data.setRequestHeader('Content-Type', 'application/json');
           data.send(JSON.stringify(getData));

如果我有超过1个主题名称,则仅打印最后一个主题名称。

我该怎么做?

2 个答案:

答案 0 :(得分:1)

解析完数据后,忘记它曾经是JSON。现在你有了一个JavaScript对象。

  • 检查data.status以确保一切顺利。
  • data.reservations之上循环,然后在data.reservations[i].resources之上。

答案 1 :(得分:0)

您应该将解析后的数据视为一个对象,为了让您前进,这将从所有返回的资源中获取所有唯一的学生组名称:

var studentGroups = []; 

for (var i = 0; i < json.reservations.length; i++) {
    if(json.reservations[i].resources != null){
        for(var j = 0; j < json.reservations[i].resources.length; j++){
            var resource = json.reservations[i].resources[j];
            if(resource.type === "student_group"){
                if(studentGroups.indexOf("groupName"))
                    studentGroups.push(resource.name);
                }
            }
        }
    }
}

当然我不确定你希望以什么格式获得结果(这应该是一个平面数组还是另一个JSON,也许只有第一个值对你来说很重要?),但我认为你应该已经有了想法如何处理这个话题。