javascript - 从 Google Apps Script 中的 api 解析 JSON 响应

原文 标签 javascript json google-apps-script

我正在使用 Google Apps Script 来解析这个 json 响应。

{"reports":[{"columnHeader":{"dimensions":["ga:country"],"metricHeader":{"metricHeaderEntries":[{"name":"ga:sessions","type":"INTEGER"}]}},"data":{"rows":[{"dimensions":["(not set)"],"metrics":[{"values":["73"]}]},{"dimensions":["Albania"],"metrics":[{"values":["1"]}]},{"dimensions":["Algeria"],"metrics":[{"values":["3"]}]},{"dimensions":["Angola"],"metrics":[{"values":["2"]}]},{"dimensions":["Antigua & Barbuda"],"metrics":[{"values":["3"]}]},{"dimensions":["Argentina"],"metrics":[{"values":["3"]}]},{"dimensions":["Armenia"],"metrics":[{"values":["1"]}]},{"dimensions":["Aruba"],"metrics":[{"values":["2"]}]},{"dimensions":["Australia"],"metrics":[{"values":["344"]}]},{"dimensions":["Austria"],"metrics":[{"values":["11"]}]},{"dimensions":["Azerbaijan"],"metrics":[{"values":["2"]}]},{"dimensions":["Bahamas"],"metrics":[{"values":["6"]}]},{"dimensions":["Bahrain"],"metrics":[{"values":["8"]}]},{"dimensions":["Bangladesh"],"metrics":[{"values":["1"]}]},{"dimensions":["Barbados"],"metrics":[{"values":["1"]}]},{"dimensions":["Belarus"],"metrics":[{"values":["1"]}]},{"dimensions":["Belgium"],"metrics":[{"values":["29"]}]},{"dimensions":["Bermuda"],"metrics":[{"values":["3"]}]},{"dimensions":["Bosnia & Herzegovina"],"metrics":[{"values":["4"]}]},{"dimensions":["Botswana"],"metrics":[{"values":["3"]}]},{"dimensions":["Brazil"],"metrics":[{"values":["12"]}]},{"dimensions":["Brunei"],"metrics":[{"values":["1"]}]},{"dimensions":["Bulgaria"],"metrics":[{"values":["24"]}]},{"dimensions":["Cameroon"],"metrics":[{"values":["1"]}]},{"dimensions":["Canada"],"metrics":[{"values":["260"]}]},{"dimensions":["Cayman Islands"],"metrics":[{"values":["1"]}]},{"dimensions":["Chile"],"metrics":[{"values":["5"]}]},{"dimensions":["China"],"metrics":[{"values":["101"]}]},{"dimensions":["Colombia"],"metrics":[{"values":["2"]}]},{"dimensions":["Costa Rica"],"metrics":[{"values":["2"]}]},{"dimensions":["Croatia"],"metrics":[{"values":["18"]}]},{"dimensions":["Curaçao"],"metrics":[{"values":["3"]}]},{"dimensions":["Cyprus"],"metrics":[{"values":["21"]}]},{"dimensions":["Czechia"],"metrics":[{"values":["99"]}]},{"dimensions":["Denmark"],"metrics":[{"values":["25"]}]},{"dimensions":["Egypt"],"metrics":[{"values":["14"]}]},{"dimensions":["El Salvador"],"metrics":[{"values":["1"]}]},{"dimensions":["Estonia"],"metrics":[{"values":["25"]}]},{"dimensions":["Eswatini"],"metrics":[{"values":["1"]}]},{"dimensions":["Falkland Islands (Islas Malvinas)"],"metrics":[{"values":["3"]}]},{"dimensions":["Faroe Islands"],"metrics":[{"values":["1"]}]},{"dimensions":["Fiji"],"metrics":[{"values":["1"]}]},{"dimensions":["Finland"],"metrics":[{"values":["22"]}]},{"dimensions":["France"],"metrics":[{"values":["147"]}]},{"dimensions":["Georgia"],"metrics":[{"values":["4"]}]},{"dimensions":["Germany"],"metrics":[{"values":["79"]}]},{"dimensions":["Ghana"],"metrics":[{"values":["9"]}]},{"dimensions":["Gibraltar"],"metrics":[{"values":["1"]}]},{"dimensions":["Greece"],"metrics":[{"values":["22"]}]},{"dimensions":["Guatemala"],"metrics":[{"values":["1"]}]},{"dimensions":["Guernsey"],"metrics":[{"values":["27"]}]},{"dimensions":["Guinea"],"metrics":[{"values":["1"]}]},{"dimensions":["Guyana"],"metrics":[{"values":["2"]}]},{"dimensions":["Haiti"],"metrics":[{"values":["1"]}]},{"dimensions":["Hong Kong"],"metrics":[{"values":["16"]}]},{"dimensions":["Hungary"],"metrics":[{"values":["26"]}]},{"dimensions":["Iceland"],"metrics":[{"values":["3"]}]},{"dimensions":["India"],"metrics":[{"values":["165"]}]},{"dimensions":["Indonesia"],"metrics":[{"values":["14"]}]},{"dimensions":["Iran"],"metrics":[{"values":["9"]}]},{"dimensions":["Iraq"],"metrics":[{"values":["6"]}]},{"dimensions":["Ireland"],"metrics":[{"values":["721"]}]},{"dimensions":["Israel"],"metrics":[{"values":["18"]}]},{"dimensions":["Italy"],"metrics":[{"values":["47"]}]},{"dimensions":["Jamaica"],"metrics":[{"values":["3"]}]},{"dimensions":["Japan"],"metrics":[{"values":["18"]}]},{"dimensions":["Jersey"],"metrics":[{"values":["82"]}]},{"dimensions":["Jordan"],"metrics":[{"values":["4"]}]},{"dimensions":["Kenya"],"metrics":[{"values":["5"]}]},{"dimensions":["Kosovo"],"metrics":[{"values":["2"]}]},{"dimensions":["Kuwait"],"metrics":[{"values":["3"]}]},{"dimensions":["Latvia"],"metrics":[{"values":["3"]}]},{"dimensions":["Lebanon"],"metrics":[{"values":["4"]}]},{"dimensions":["Lesotho"],"metrics":[{"values":["3"]}]},{"dimensions":["Libya"],"metrics":[{"values":["1"]}]},{"dimensions":["Lithuania"],"metrics":[{"values":["5"]}]},{"dimensions":["Luxembourg"],"metrics":[{"values":["4"]}]},{"dimensions":["Malaysia"],"metrics":[{"values":["35"]}]},{"dimensions":["Malta"],"metrics":[{"values":["25"]}]},{"dimensions":["Mauritius"],"metrics":[{"values":["1"]}]},{"dimensions":["Mexico"],"metrics":[{"values":["17"]}]},{"dimensions":["Moldova"],"metrics":[{"values":["3"]}]},{"dimensions":["Monaco"],"metrics":[{"values":["1"]}]},{"dimensions":["Montenegro"],"metrics":[{"values":["2"]}]},{"dimensions":["Morocco"],"metrics":[{"values":["4"]}]},{"dimensions":["Myanmar (Burma)"],"metrics":[{"values":["1"]}]},{"dimensions":["Namibia"],"metrics":[{"values":["2"]}]},{"dimensions":["Netherlands"],"metrics":[{"values":["105"]}]},{"dimensions":["New Zealand"],"metrics":[{"values":["53"]}]},{"dimensions":["Nigeria"],"metrics":[{"values":["32"]}]},{"dimensions":["North Macedonia"],"metrics":[{"values":["7"]}]},{"dimensions":["Norway"],"metrics":[{"values":["26"]}]},{"dimensions":["Oman"],"metrics":[{"values":["7"]}]},{"dimensions":["Pakistan"],"metrics":[{"values":["27"]}]},{"dimensions":["Philippines"],"metrics":[{"values":["23"]}]},{"dimensions":["Poland"],"metrics":[{"values":["30"]}]},{"dimensions":["Portugal"],"metrics":[{"values":["27"]}]},{"dimensions":["Puerto Rico"],"metrics":[{"values":["3"]}]},{"dimensions":["Qatar"],"metrics":[{"values":["13"]}]},{"dimensions":["Réunion"],"metrics":[{"values":["1"]}]},{"dimensions":["Romania"],"metrics":[{"values":["30"]}]},{"dimensions":["Russia"],"metrics":[{"values":["8"]}]},{"dimensions":["Saudi Arabia"],"metrics":[{"values":["12"]}]},{"dimensions":["Senegal"],"metrics":[{"values":["3"]}]},{"dimensions":["Serbia"],"metrics":[{"values":["16"]}]},{"dimensions":["Singapore"],"metrics":[{"values":["40"]}]},{"dimensions":["Sint Maarten"],"metrics":[{"values":["1"]}]},{"dimensions":["Slovakia"],"metrics":[{"values":["19"]}]},{"dimensions":["Slovenia"],"metrics":[{"values":["13"]}]},{"dimensions":["South Africa"],"metrics":[{"values":["162"]}]},{"dimensions":["South Korea"],"metrics":[{"values":["10"]}]},{"dimensions":["South Sudan"],"metrics":[{"values":["1"]}]},{"dimensions":["Spain"],"metrics":[{"values":["90"]}]},{"dimensions":["Sri Lanka"],"metrics":[{"values":["3"]}]},{"dimensions":["St. Vincent & Grenadines"],"metrics":[{"values":["2"]}]},{"dimensions":["Suriname"],"metrics":[{"values":["1"]}]},{"dimensions":["Sweden"],"metrics":[{"values":["43"]}]},{"dimensions":["Switzerland"],"metrics":[{"values":["32"]}]},{"dimensions":["Taiwan"],"metrics":[{"values":["3"]}]},{"dimensions":["Tajikistan"],"metrics":[{"values":["1"]}]},{"dimensions":["Tanzania"],"metrics":[{"values":["3"]}]},{"dimensions":["Thailand"],"metrics":[{"values":["17"]}]},{"dimensions":["Trinidad & Tobago"],"metrics":[{"values":["7"]}]},{"dimensions":["Tunisia"],"metrics":[{"values":["8"]}]},{"dimensions":["Turkey"],"metrics":[{"values":["24"]}]},{"dimensions":["Turkmenistan"],"metrics":[{"values":["1"]}]},{"dimensions":["Turks & Caicos Islands"],"metrics":[{"values":["1"]}]},{"dimensions":["Ukraine"],"metrics":[{"values":["22"]}]},{"dimensions":["United Arab Emirates"],"metrics":[{"values":["65"]}]},{"dimensions":["United Kingdom"],"metrics":[{"values":["104807"]}]},{"dimensions":["United States"],"metrics":[{"values":["4281"]}]},{"dimensions":["Vietnam"],"metrics":[{"values":["8"]}]},{"dimensions":["Zambia"],"metrics":[{"values":["2"]}]},{"dimensions":["Zimbabwe"],"metrics":[{"values":["7"]}]}],"totals":[{"values":["112788"]}],"rowCount":134,"minimums":[{"values":["1"]}],"maximums":[{"values":["104807"]}],"isDataGolden":true}}]}
我需要获取“.colimnHeader”dimensionsmetricHeader值和“.data.rows”dimensionsmetrics值。我的代码不起作用:
    var json = JSON.parse(response.getContentText()); 
    
        var dataSet = json.reports[0].data.rows;
        var rows2 = [],
              json;
        
        for (var i in json.reports[0].data.rows) {
        data = dataSet[i];
  
            rows2.push(json.reports[0].columnHeader.dimensions[i], json.reports[0].columnHeader.metricHeader[0].name[i], json.reports[0].data.rows[0].dimensions[0], json.reports[0].data.rows[0].metrics[0].values[i]);
        }

最佳答案

这个答案怎么样?
retrofit 要点:

  • json.reports[0].columnHeader.dimensions[i]可能是 json.reports[0].columnHeader.dimensions[0] .
  • json.reports[0].columnHeader.metricHeader[0].name[i]可能是 json.reports[0].columnHeader.metricHeader.metricHeaderEntries[0].name .
  • json.reports[0].data.rows[0].dimensions[0]可能是 json.reports[0].data.rows[i].dimensions[0] .
  • json.reports[0].data.rows[0].metrics[0].values[i]可能是 json.reports[0].data.rows[i].metrics[0].values[0] .
  • 在您的情况下,我认为 push 中的值至 rows2因为数组可能是合适的。
  • var dataSet = json.reports[0].data.rows;未在您的脚本中使用。

  • 当以上几点反射(reflect)到你的脚本中时,它变成如下。
    修改后的脚本:
    从:
    var rows2 = [],
                  json;
    for (var i in json.reports[0].data.rows) {
      data = dataSet[i];
      rows2.push(json.reports[0].columnHeader.dimensions[i], json.reports[0].columnHeader.metricHeader[0].name[i], json.reports[0].data.rows[0].dimensions[0], json.reports[0].data.rows[0].metrics[0].values[i]);
    }
    
    到:
    var rows2 = [];
    for (var i = 0; i < dataSet.length; i++) {
      data = dataSet[i];
      rows2.push([
        json.reports[0].columnHeader.dimensions[0],
        json.reports[0].columnHeader.metricHeader.metricHeaderEntries[0].name,
        data.dimensions[0],
        data.metrics[0].values[0]
      ]);
    }
    

    关于javascript - 从 Google Apps Script 中的 api 解析 JSON 响应,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62616911/

    相关文章:

    javascript - 使用 Jquery 更改检查单选按钮 ID

    javascript - 以编程方式添加的样式表元素不被 dom 接受

    javascript - Javascript-循环内循环

    javascript - 如何使用jQuery隐藏特定的JSON对象数据

    css - 为Apps脚本UI服务设置单独的CSS文件

    mysql - 无法通过 Apps Script (JDBC) 连接到本地 MySQL 服务器

    javascript - 在循环内向数组添加元素

    javascript - 在卸载之前动画出一个 React 组件,它丢失了它需要显示的 Prop 数据?

    javascript - 如何在 javascript 文件中键入我的 json 文件的正确路径

    java - 在 Java 中将 JSON 转换为 Parquet