编程语言
首页 > 编程语言> > javascript – 如何从Bigquery结果中解析json响应?

javascript – 如何从Bigquery结果中解析json响应?

作者:互联网

我尝试使用示例javascript代码来调用Google bigQuery API(https://developers.google.com/bigquery/docs/authorization#client-side-javascript)

JS:

function runQuery() {
   var request = gapi.client.bigquery.jobs.query({
      'projectId': project_id,
      'timeoutMs': '30000',
      'query': 'SELECT TOP(repository_language, 5) as language, COUNT(*) as count FROM [publicdata:samples.github_timeline] WHERE repository_language != "";'
    });
    request.execute(function(response) {     
        console.log(response);
        var results = response.result.rows ;
        $('#result_box').html(JSON.stringify(results, null));
    });
}

以上大查询返回:

[{"f":[{"v":"JavaScript"},{"v":"949899"}]},{"f":[{"v":"Ruby"},{"v":"640659"}]},{"f":[{"v":"Java"},{"v":"568202"}]},{"f":[{"v":"Python"},{"v":"484852"}]},{"f":[{"v":"PHP"},{"v":"453830"}]}]

请帮我解决如何以JSON格式解析上述结果中的值?

{"JavaScript": "949899", "Ruby": "640659", "Java": "568202", "Python": "484852", "PHP": "453830" }

解决方法:

Eval是一种安全风险.

var text = '[{"f":[{"v":"JavaScript"},{"v":"949899"}]},{"f":[{"v":"Ruby"},{"v":"640659"}]},{"f":[{"v":"Java"},{"v":"568202"}]},{"f":[{"v":"Python"},{"v":"484852"}]},{"f":[{"v":"PHP"},{"v":"453830"}]}]';
myData = JSON.parse(text);
alert(myData[4].f[0].v);​

标签:jquery,javascript,bigdata,google-bigquery
来源: https://codeday.me/bug/20190629/1330270.html