如何从数组中只访问单个json对象?

时间:2016-07-28 04:30:03

标签: jquery angularjs json

我在$ http.get响应中获取了这个json对象数组。我想要显示两个json对象中的任何一个。

// HTML code:

<pre ng-repeat="opportunity in opportunities track by $index">
    <span>{{opportunity | json}}</span>
</pre>

数组中的JSON对象:

    [
      {
        "type": "job",
        "userId": "5791a4cd0a53c59c52fc3230",
        "positionName": "aaaaaaaa",
        "budget": "3",
        "jobLocation": [
          "Bangalore",
          "Pune"
        ],
        "minExp": "0 year",
        "maxExp": "2 years",
        "gradMarks": null,
        "hscOrDiploma": null,
        "ssc": null,
        "jobDescription": "qq",
        "selectionProcess": "cv",
        "lastDate": "2016-07-30T18:30:00.000Z",
        "noOfPositions": "9"
      },

//同一个数组中的第二个json对象

      {
        "type": "job",
        "userId": "5791a4cd0a53c59c52fc3230",
        "positionName": "zz",
        "budget": "3",
        "jobLocation": [
          "Bangalore",
          "Chennai",
          "Pune",
          "Hyderabad",
          "Mumbai",
          "Gurgaon"
        ],
        "minExp": "2 years",
        "maxExp": "5 years",
        "gradMarks": null,
        "hscOrDiploma": null,
        "ssc": null,
        "jobDescription": "ss",
        "selectionProcess": "aa",
        "lastDate": "2016-07-30T18:30:00.000Z",
        "noOfPositions": "5"
      }
]

以上是我正在做的事情。请帮帮我。

3 个答案:

答案 0 :(得分:3)

从数组访问单个特定对象,即此处的机会。你可以这样做:

$scope.theSpecificObject = $scope.opportunities[0];

这将从数组中获取第一个json对象。 希望它有所帮助。

答案 1 :(得分:1)

不确定你的意思我想要显示两个json对象中的任何一个。。考虑您要使用第一个对象:

<pre>
    <span>{{opportunities[0] | json}}</span>
</pre>

对于第二个对象:

<pre>
    <span>{{opportunities[1] | json}}</span>
</pre>

答案 2 :(得分:0)

要达到预期效果,请使用以下

HTML:

<div ng-app="myApp" ng-controller="myCtrl">
  <input type="number" ng-model="getObject">
  <pre ng-repeat="opportunity in opportunities track by $index">

                                        <span ng-if="$index==getObject">{{opportunity | json}}</span>

                                        </pre>
</div>

Codepen - http://codepen.io/nagasai/pen/XKYPGN