gpt4 book ai didi

javascript - 如何解析来自 Bigquery 结果的 json 响应?

转载 作者:塔克拉玛干 更新时间:2023-11-02 21:04:12 24 4
gpt4 key购买 nike

我尝试了示例 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);​

关于javascript - 如何解析来自 Bigquery 结果的 json 响应?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13639779/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com