JavaScript对象EventSource

时间:2015-10-15 18:16:27

标签: javascript object

我试图获取BidPrice对象。我在EventSource中获取此数据并希望处理它。

    var data = {
      "status" : "OK",
      "Quote" : {
        "EUR/USD" : {
          "eventSymbol" : "EUR/USD",
          "bidTime" : 1444932337000,
          "bidExchangeCode" : "",
          "bidPrice" : 1.29805,
          "bidSize" : 1,
          "askTime" : 1444932337000,
          "askExchangeCode" : "",
          "askPrice" : 1.29807,
          "askSize" : 1
        },
        "USD/AUD" : {
          "eventSymbol" : "USD/AUD",
          "bidTime" : 1444932337000,
          "bidExchangeCode" : "",
          "bidPrice" : 0.97457,
          "bidSize" : 1,
          "askTime" : 1444932337000,
          "askExchangeCode" : "",
          "askPrice" : 0.97464,
          "askSize" : 1
        }
      }
    };

    for(var key in data){
        console.log(data[key].Quote.bidPrice);
        //my goal is to output this: console.log('EUR/USD bid price is 1.29805');
    }

2 个答案:

答案 0 :(得分:1)

你所拥有的是亲密的。您需要在data.Quote上迭代keys。请参阅下面的工作示例,该示例使用forEach method循环引用对象的键。

基本想法是这样做:

Object.keys(data["Quote"]).forEach(function (k) {
    console.log(k + " bid price is: " + data["Quote"][k]["bidPrice"]);
});

完整的例子:



var data = {
  "status" : "OK",
  "Quote" : {
    "EUR/USD" : {
      "eventSymbol" : "EUR/USD",
      "bidTime" : 1444932337000,
      "bidExchangeCode" : "",
      "bidPrice" : 1.29805,
      "bidSize" : 1,
      "askTime" : 1444932337000,
      "askExchangeCode" : "",
      "askPrice" : 1.29807,
      "askSize" : 1
    },
    "USD/AUD" : {
      "eventSymbol" : "USD/AUD",
      "bidTime" : 1444932337000,
      "bidExchangeCode" : "",
      "bidPrice" : 0.97457,
      "bidSize" : 1,
      "askTime" : 1444932337000,
      "askExchangeCode" : "",
      "askPrice" : 0.97464,
      "askSize" : 1
    }
  }
};

//Log function for demonstration
function log (s) {
  var e = document.createElement('pre');
  e.innerHTML = s;
  document.body.appendChild(e);
  console.log(s);
}

Object.keys(data["Quote"]).forEach(function (k) {
  log(k + " bid price is: " + data["Quote"][k]["bidPrice"]);
});




答案 1 :(得分:0)

[BeforeScenario("Tag01", "Tag02")]
public static void BeforeScenario()
{
    StepBase.CreateBrowser(ConfigurationManager.AppSettings["BrowserType"]);
    Console.WriteLine("selenium open Called");
}

此处的关键是状态报价

console.log(data.Quote['EUR/USD'].bidPrice);

你真正想要的是(据我所知)。关键是 EUR / USD USD / AUD

for(var key in data){
    console.log(key);
}