简体   繁体   English

如何在 javascript 中循环遍历 unciode json 字符串?

[英]how can I loop through a unciode json string in javascript?

I get the following string returned by the CBOE options api:我得到了 CBOE 选项 api 返回的以下字符串:

{u'inputs': {u'stock_price_max': 50.0, u'high_low_stock_max': None, u'time_frame': u'middle', u'hv30_max': None, u'high_low_stock_min': None, u'symbols': None, u'hv30_min': None, u'low_strike': 3.0, u'high_strike': 4.0, u'industry_codes': None, u'spread_ask_price_max': None, u'stock_price_min': 10.0}, u'output': [{u'stock_price': 43.2, u'stock_hi_lo_percent': 72.9651, u'symbol': u'EWZ', u'industry_code': 55501010, u'max_gain': 0.52, u'high_strike_otm_percent': 0.463, u'low_strike_otm_percent': 2.7778, u'spread_ask': 0.48, u'spread': u'43/42 Put', u'expiry': u'2019-04-18', u'max_gain_to_spread_ask_percent': 108.3333, u'hv30': 27.3836}, {u'stock_price': 41.37, u'stock_hi_lo_percent': 21.7957, u'symbol': u'FXI', u'industry_code': 55501010, u'max_gain': 0.26, u'high_strike_otm_percent': 0.8944, u'low_strike_otm_percent': 2.103, u'spread_ask': 0.24, u'spread': u'41/40.5 Put', u'expiry': u'2019-05-17', u'max_gain_to_spread_ask_percent': 108.3333, u'hv30': 20.2925}

I want to loop through it and place elements into cells in a Google spreadsheet.我想遍历它并将元素放入 Google 电子表格中的单元格中。 I have this code:我有这个代码:

function myFunction() {
  var response = UrlFetchApp.fetch(endpoint);
  var data = response.getContentText();
  sheet.getRange("A8").setValue(data);
}

This puts the entire string into cell A8.这会将整个字符串放入单元格 A8。

I have tried to loop through the string with我试图通过字符串循环

for (i = 0; i < jsonlen; i++) {
    sheet.getRange("A:A").setValaue(data['output']['symbol']);
}

This returns "undefined".这将返回“未定义”。 So problems are: 1) how can I extract the elements I need form the "output" part of the string 2) put the symbols into A3, A4 etc then stock_price into B3, B4 etc 3) how to identify the length of the string in order to make the loop work correctly until the string has been entirely looped over?所以问题是:1)如何从字符串的“输出”部分中提取我需要的元素 2)将符号放入 A3、A4 等,然后将 stock_price 放入 B3、B4 等 3)如何识别字符串的长度为了使循环正常工作,直到字符串完全循环?

Many thanks!非常感谢!

  • You want to retrieve the values of symbol and stock_price the property of output and want to put them to the columns "A" and "B" of the active Spreadsheet, respectively.您想要检索symbolstock_price output属性的值,并将它们分别放入活动电子表格的“A”和“B”列。
  • You want to put the values from the row 3.您想放置第 3 行中的值。
  • You want to achieve this using Google Apps Script.您想使用 Google Apps 脚本来实现这一点。

If my understanding is correct, how about this answer?如果我的理解是正确的,这个答案怎么样? Please think of this as just one of several possible answers.请将此视为几种可能的答案之一。

Modification points:改装要点:

  • the property of output has an array. output的属性有一个数组。 And symbol and stock_price are in the array.并且symbolstock_price在数组中。
    • So at first, it is required to prepare the values for putting to Spreadsheet.因此,首先需要准备要放入电子表格的值。

Modified script:修改后的脚本:

function myFunction() {
  var response = UrlFetchApp.fetch(endpoint);
  var data = JSON.parse(response.getContentText());

  var values = data.output.map(function(e) {return [e.symbol, e.stock_price]});
  var sheet = SpreadsheetApp.getActiveSheet();
  sheet.getRange(3, 1, values.length, values[0].length).setValues(values);
}
  • In this case, the values are put to the row 3 of column "A" and "B" on the active sheet.在这种情况下,这些值被放置到活动工作表上“A”和“B”列的第 3 行。

Note:笔记:

  • In your case, if data is actually the string value of {u'inputs': {###}, u'outputs': [###]} which has the unicode literal of python 2, u is required to be replaced.在您的情况下,如果data实际上是{u'inputs': {###}, u'outputs': [###]}的字符串值,它具有 python 2 的 unicode 文字,则需要替换u . So in this case, please put data = JSON.parse(data.replace(/u\\'|\\'/g, "\\"").replace(/None/g, "null")); before var values = data.output.map(function(e) {return [e.symbol, e.stock_price]}); . Please be careful this.所以在这种情况下,请将data = JSON.parse(data.replace(/u\\'|\\'/g, "\\"").replace(/None/g, "null"));放在var values = data.output.map(function(e) {return [e.symbol, e.stock_price]});之前var values = data.output.map(function(e) {return [e.symbol, e.stock_price]}); 。请注意这一点。

References:参考:

If I misunderstood your question and this was not the result you want, I apologize.如果我误解了您的问题并且这不是您想要的结果,我深表歉意。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM