0

新手 Javascript 程序员在这里,可能比我能咀嚼的更多。我正在尝试使用在 Javascript 中创建的自定义函数将来自 YQL 查询的 JSON 格式数据放入 Google Drive 电子表格中。我不是,重复不是,填充网页,只是将数据移动到电子表格。

这是 JSON 响应(从直接将其写入电子表格单元格的测试函数中复制):

{"query":{"count":1,"created":"2013-08-29T20:56:58Z","lang":"en-US","re​​sults":{"optionsChain":{"expiration ":"2013-12-21","symbol":"HYS","option":[{"symbol":"HYS131221C00103000","type":"C","strikePrice":"103","lastPrice ":"3.00","change":"0","changeDir":null,"bid":"NaN","ask":"5","vol":"2","openInt":"2 "},{"symbol":"HYS131221C00105000","type":"C","strikePrice":"105","lastPrice":"0.40","change":"0","changeDir":null, “出价”:"NaN","ask":"5","vol":"10","openInt":"11"},{"symbol":"HYS131221C00106000","type":"C","strikePrice" :"106","lastPrice":"0.05","change":"0","changeDir":null,"bid":"NaN","ask":"5","vol":"1" ,"openInt":"120"},{"symbol":"HYS131221C00107000","type":"C","strikePrice":"107","lastPrice":"0.10","change":"0" ,"changeDir":null,"bid":"NaN","ask":"0.2","vol":"40","openInt":"80"},{"symbol":"HYS131221P00100000","type":"P","strikePrice":"100","lastPrice":"0.70","change":"0","changeDir":null,"bid":"NaN"," ask":"5","vol":"1","openInt":"1"},{"symbol":"HYS131221P00102000","type":"P","strikePrice":"102"," lastPrice":"1.00","change":"0","changeDir":null,"bid":"NaN","ask":"5","vol":"15","openInt":" 15"},{"symbol":"HYS131221P00103000","type":"P","strikePrice":"103","lastPrice":"1.55","change":"0","changeDir":null,"bid":"NaN","ask":"5","vol":"40","openInt":"50"},{"symbol":"HYS131221P00105000" ,"type":"P","strikePrice":"105","lastPrice":"2.35","change":"0","changeDir":null,"bid":"0.3","ask" :"5.2","vol":"15","openInt":"15"},{"symbol":"HYS131221P00107000","type":"P","strikePrice":"107","lastPrice" :"6.50","change":"0","changeDir":null,"bid":"2.05","ask":"7","vol":"2","openInt":"20"}]}}}}

现在,如果我简单地将上面列出的 JSON 格式的对象分配给一个变量(即硬编码它),我可以看到名称/值对、提取数据、进行迭代等。但是当我将响应直接分配给一个变量时(即即时接收)我无法看到或迭代任何东西。例如,可以看到接收变量“o”并将其写入电子表格单元格,但看不到它的第一个孩子 - o[“query”]。启动查询的函数位于使用它的函数内部,所以我认为这不是时间问题。

4

1 回答 1

0

好吧,我正在用我不完全理解的解决方案来回答我自己的问题。这是有效的代码(解释和进一步的问题):

function YQLquery() { // A YQL query returning JSONP with XJSON2CSV function embedded in the response
  var fetchstring1 = "http://query.yahooapis.com/v1/public/yql?"; // constructing YQL query in 4 parts
  var fetchstring2 = "q=SELECT%20*%20FROM%20yahoo.finance.options";
  var fetchstring3 = "%20WHERE%20symbol%3D'HYS'%20AND%20expiration%3D'2013-12'";
  var fetchstring4 = "&format=json&callback=XJSON2CSV&env=store%3A%2F%2Fdatatables.org%2Falltableswithkeys";
  var fetchstring = fetchstring1 + fetchstring2 + fetchstring3 + fetchstring4;  
  return UrlFetchApp.fetch(fetchstring); 
  /* This returns (truncated after first option array item, but you get the point):
  XJSON2CSV({"query":{"count":1,"created":"2013-09-13T21:39:35Z","lang":"en-US",
  "results":{"optionsChain":{"expiration":"2013-12-21","symbol":"HYS","option":
  [{"symbol":"HYS131221C00103000","type":"C","strikePrice":"103","lastPrice":
  "3.00","change":"0","changeDir":null,"bid":"1.65","ask":"2","vol":"NaN","openInt":"2"},{ etc etc*/
}//end of function YQLquery

function XJSON2CSV(YQLQueryResponse) { //converts the YQLquery JSONP response payload (i.e. JSON portion) to CSV string
    var vx = YQLQueryResponse;
    var JSONobj = vx["query"]["results"]["optionsChain"]["option"]; // obtain the option array within JSON
    var CSVLine = [];
    var i = 0;
    var j = "";
    for (var i in JSONobj) {
        CSVLine[i] = []; // gets the JSON array objects
        for (var j in JSONobj[i]){
            CSVLine[i].push(JSONobj[i][j]); // get the name/value pairs from each array object
        }; //end of inner loop
    }; //end of outer loop
   return CSVLine; 
} //end of function XJSON2CSV()

function xxx() //test it
{
  return eval(YQLquery().toString());
}

注释:

1)与我在原始问题中的假设相反,时间是问题所在。所以我求助于使用回调函数——在这种情况下是 XJSON2CSV;让 YQL 在 JSONP 响应中返回对它的调用(以完整的 JSON 字符串有效负载作为参数)。

2) 在某种程度上,所有这些都运行良好——返回的 XJSON2CSV 调用是一个对象,我需要一个函数。经过相当多的实验,我通过将其转换为字符串并(反对所有相反的建议)“eval()-”来获得解决方案。

问题:有没有更好的方法来完成不涉及“eval()”的最后一步(在函数“xxx”中)?或者,换一种方式问,为什么我不能只使用函数 xxx "return QYLquery;"

向安迪道歉,直到现在才回复。

于 2013-09-14T14:40:26.657 回答