Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/json/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用AngularJS显示JSON中引用id为的数据_Json_Angularjs - Fatal编程技术网

使用AngularJS显示JSON中引用id为的数据

使用AngularJS显示JSON中引用id为的数据,json,angularjs,Json,Angularjs,我使用AngularJS显示JSON中引用id为的数据。但JSON结构略有更改,因此引用id没有正确获取 以前的JSON: { "tid": "2440", "name": "Alfredo's Pizzeria", "field_location_category": [ "Food and Dining" ] }, { "tid": "2441", "name": "Allegro Dining Room", "field_location_categ

我使用AngularJS显示JSON中引用id为的数据。但JSON结构略有更改,因此引用id没有正确获取

以前的JSON:

{
  "tid": "2440",
  "name": "Alfredo's Pizzeria",
  "field_location_category": [
      "Food and Dining"
  ]
},
{
  "tid": "2441",
  "name": "Allegro Dining Room",
  "field_location_category": [
      "Food and Dining"
  ]
},
{
  "tid": "2443",
  "name": "Art Gallery",
  "field_location_category": [
      "Gifts & Memories"
  ]
},
{
  "tid": "2435",
  "name": "Bellini's",
  "field_location_category": [
      "Entertainment/Bars"
  ]
},
{
  "tid": "2498",
  "name": "Bullseye",
  "field_location_category": [
      "Pools, Sports & Spa"
  ]
}
当前JSON:

{
  "count": 70,
  "language": "en",
  "0": {
    "id": "2420",
    "title": "Medical Center",
    "description": "The medical staff on-board are available for consultation; hours are posted in the Princess Patter.  Please contact the Medical Center should you suffer from motion sickness and require a remedy.  Professional services as well as medications are provided at reasonable costs.  For medical emergencies, please dial 911.  Your stateroom stateroom steward can provide you with the necessary safe waste receptacle if you have a medical condition that requires the use of sharps or needles.",
    "time": "8:00am-10:00am & 4:30pm-6:30pm",
    "manager": null,
    "cover_charge": "",
  },
  "1": {
    "id": "7840",
    "title": "Conference Room - Deck 5 Portside",
    "description": "Located on Deck 5 on both portside and starboard sides, the conference rooms onboard Regal Princess can be reserved for group meetings.  Please enquire for details at the Passenger Services Desk.",
    "time": null,
    "manager": null,
    "cover_charge": null,   
  },
  "2": {
    "id": "2426",
    "title": "Conference Room - Deck 5 Starboard",
    "description": "Located on Deck 5 on both portside and starboard sides, the conference rooms onboard Regal Princess can be reserved for group meetings.  Please enquire for details at the Passenger Services Desk.",
    "time": null,
    "manager": null,
    "cover_charge": "",  
  }
}
控制器是

var detailResult = $filter('filter')($scope.locationDetail, {id:path})[0];
这里的
$scope.locationDetail
是我的JSON数据,
path
是从URL获取的对应id


我已经检查了
$scope.locationDetail
path
,两者都正确。我认为问题来自最后一节
{id:path})[0]
id
引用未正确获取。

角度过滤器将无法工作。在当前的JSON中,您将其用作“0”、“1”、“2”等

使您的代码正常工作。按如下方式将数组的内部内容分开

代码

var isArrayProccessingDone = false; 
$scope.newLocationDetail = [];
for(var i=0; !isArrayProccessingDone ; i++){
    newLocationDetail.push($scope.locationDetail['"'+i+'"']);
}
现在使用新创建的范围变量进行筛选

var detailResult = $filter('filter')($scope.newLocationDetail, {id: path})[0];
(有关更多详细信息,请参阅)

我不知道你为什么要改变人们通常遵循的旧JSON结构

更新

我建议您将JSON结构更改如下

JSON

$scope.locationDetail = {
        "count": 70,
            "language": "en",
            "subDetail": [{
            "id": "2420",
                "title": "Medical Center",
                "description": "The medical staff on-board are available for consultation; hours are posted in the Princess Patter.  Please contact the Medical Center should you suffer from motion sickness and require a remedy.  Professional services as well as medications are provided at reasonable costs.  For medical emergencies, please dial 911.  Your stateroom stateroom steward can provide you with the necessary safe waste receptacle if you have a medical condition that requires the use of sharps or needles.",
                "time": "8:00am-10:00am & 4:30pm-6:30pm",
                "manager": null,
                "cover_charge": ""
        }, {
            "id": "7840",
                "title": "Conference Room - Deck 5 Portside",
                "description": "Located on Deck 5 on both portside and starboard sides, the conference rooms onboard Regal Princess can be reserved for group meetings.  Please enquire for details at the Passenger Services Desk.",
                "time": null,
                "manager": null,
                "cover_charge": null
        }, {
            "id": "2426",
                "title": "Conference Room - Deck 5 Starboard",
                "description": "Located on Deck 5 on both portside and starboard sides, the conference rooms onboard Regal Princess can be reserved for group meetings.  Please enquire for details at the Passenger Services Desk.",
                "time": null,
                "manager": null,
                "cover_charge": ""
        }]
    }
然后您的过滤器将如下所示

var detailResult = $filter('filter')($scope.locationDetail.subDetail, {id:'7840'})[0];

希望这能对你有所帮助。谢谢