在下面的JSON中。如果我不了解子对象,我怎样才能使用角度JS?

时间:2016-10-27 06:14:50

标签: javascript angularjs json



{
  "GE": {
    "symbol": "GE",
    "dxSymbol": "GE",
    "exchange": "XNYS",
    "isoExchange": "XNYS",
    "bzExchange": "NYSE",
    "type": "STOCK",
    "name": "General Electric",
    "description": "General Electric Company Common Stock",
    "sector": "Industrials",
    "industry": "Diversified Industrials",
    "open": 28.59,
    "high": 29.07,
    "low": 28.55,
    "close": 28.87,
    "bidPrice": 28.82,
    "askPrice": 28.94,
    "askSize": 7,
    "bidSize": 10,
    "size": 1676188,
    "bidTime": 1477526400000,
    "askTime": 1477526400000,
    "lastTradePrice": 28.87,
    "lastTradeTime": 1477512029000,
    "volume": 24357188,
    "change": 0.22,
    "changePercent": 0.77,
    "previousClosePrice": 28.65,
    "fiftyDayAveragePrice": 31.0652,
    "fiftyTwoWeekHigh": 33.0,
    "fiftyTwoWeekLow": 19.37,
    "marketCap": 291853729930,
    "sharesOutstanding": 10109239000,
    "pe": 36.964286,
    "forwardPE": 20.7039,
    "dividendYield": 2.96,
    "payoutRatio": 109.52,
    "ethPrice": 28.87,
    "ethVolume": 777095,
    "ethTime": 1477526274000
  }
}




  

在上面的JSON中。如果我不知道子对象是GE,我怎么能用角度JS来获取它。我从get端点得到这个响应,我想访问直接孩子是可能的,如果我不知道它是" GE"在手边

2 个答案:

答案 0 :(得分:0)

Object.keys(object)获取密钥数组。然后拿第一个



var myObj = {
  "GE": {
    "symbol": "GE",
    "dxSymbol": "GE",
    "exchange": "XNYS",
    "isoExchange": "XNYS",
    "bzExchange": "NYSE",
    "type": "STOCK",
    "name": "General Electric",
    "description": "General Electric Company Common Stock",
    "sector": "Industrials",
    "industry": "Diversified Industrials",
    "open": 28.59,
    "high": 29.07,
    "low": 28.55,
    "close": 28.87,
    "bidPrice": 28.82,
    "askPrice": 28.94,
    "askSize": 7,
    "bidSize": 10,
    "size": 1676188,
    "bidTime": 1477526400000,
    "askTime": 1477526400000,
    "lastTradePrice": 28.87,
    "lastTradeTime": 1477512029000,
    "volume": 24357188,
    "change": 0.22,
    "changePercent": 0.77,
    "previousClosePrice": 28.65,
    "fiftyDayAveragePrice": 31.0652,
    "fiftyTwoWeekHigh": 33.0,
    "fiftyTwoWeekLow": 19.37,
    "marketCap": 291853729930,
    "sharesOutstanding": 10109239000,
    "pe": 36.964286,
    "forwardPE": 20.7039,
    "dividendYield": 2.96,
    "payoutRatio": 109.52,
    "ethPrice": 28.87,
    "ethVolume": 777095,
    "ethTime": 1477526274000
  }
};
var key = Object.keys(myObj)[0];
console.log(myObj[key]);




答案 1 :(得分:-1)

注意:在您提出问题之前,请检查一下这个问题,如果您没有找到答案,请提出问题。

使用,

Object.keys(hash) 属性。

所以,

Object.keys(object)[0] 会为您提供第一个键的值

// Instantiate the app, the 'myApp' parameter must 
// match what is in ng-app
var myApp = angular.module('myApp', []);

// Create the controller, the 'ToddlerCtrl' parameter 
// must match an ng-controller directive
myApp.controller('ToddlerCtrl', function ($scope) {
  
  $scope.data = {
  "GE": {
    "symbol": "GE",
    "dxSymbol": "GE",
    "exchange": "XNYS",
    "isoExchange": "XNYS",
    "bzExchange": "NYSE",
    "type": "STOCK",
    "name": "General Electric",
    "description": "General Electric Company Common Stock",
    "sector": "Industrials",
    "industry": "Diversified Industrials",
    "open": 28.59,
    "high": 29.07,
    "low": 28.55,
    "close": 28.87,
    "bidPrice": 28.82,
    "askPrice": 28.94,
    "askSize": 7,
    "bidSize": 10,
    "size": 1676188,
    "bidTime": 1477526400000,
    "askTime": 1477526400000,
    "lastTradePrice": 28.87,
    "lastTradeTime": 1477512029000,
    "volume": 24357188,
    "change": 0.22,
    "changePercent": 0.77,
    "previousClosePrice": 28.65,
    "fiftyDayAveragePrice": 31.0652,
    "fiftyTwoWeekHigh": 33.0,
    "fiftyTwoWeekLow": 19.37,
    "marketCap": 291853729930,
    "sharesOutstanding": 10109239000,
    "pe": 36.964286,
    "forwardPE": 20.7039,
    "dividendYield": 2.96,
    "payoutRatio": 109.52,
    "ethPrice": 28.87,
    "ethVolume": 777095,
    "ethTime": 1477526274000
  }
}

alert(Object.keys($scope.data)[0])
  
  
});
<!DOCTYPE html>
<html ng-app="myApp">

  <head>
    <script data-require="angular.js@1.2.7" data-semver="1.2.7" src="http://code.angularjs.org/1.2.7/angular.js"></script>
    <link href="style.css" rel="stylesheet" />
    <script src="script.js"></script>
  </head>
  <body>
    
    <div ng-controller="ToddlerCtrl">

    </div>
  </body>

</html>

请运行此代码段