我正在通过 http 请求查询一个 graphQL 端点,我得到了一个成功的响应,其中包含我需要收集的数据,但是,我似乎无法浏览数据以提取我需要的字段。
const query = JSON.stringify({query:
'{' +
' all_insight_article('+
' locale: "en-gb"'+
' where: {business_unit: {business_unit: {title: "Some Brand"}, MATCH: ALL}, audience: {MATCH: ALL, audiences: {title: "Management"}}publish_date_gt: "2023-02-01"}'+
' ) {items {'+
' audienceConnection {'+
' edges {'+
' node {... on Audiences {title system {uid}}}}}'+
' system {'+
' uid'+
' publish_details {time}'+
' updated_at}'+
' absolute_url'+
' title'+
' subtitle'+
' main_image'+
' topicsConnection {'+
' edges {'+
' node {'+
' ... on Topics {'+
' title'+
' display_name'+
' system {uid}}}}}}total}}'
});
var req = new HttpClientRequest("https://eu-graphql.contentstack.com/stacks/bltcxxx?environment=xxx&access_token=xxx")
req.header["Content-Type"] = "application/json"
req.method = "POST"
req.body = query
req.execute()
var response = req.response;
var posts = JSON.parse(response.body);
var articleList_json = [];
var i
for ( i = 0; i < 15; i++) {
articleList_json.push({
"title": posts[i].title,
});
}
logInfo(articleList_json);
我得到的错误是
posts is undefined.
注意:
HttpClientRequest
是我的应用程序的一个类,但几乎是一个标准的 http 请求https://experienceleague.adobe.com/developer/campaign-api/api/c-HttpClientRequest.html
第二次更新
我尝试了以下但仍然不起作用
var response = req.response;
var posts = JSON.stringify(response.body);
var articleList_json = [];
var i
for ( i = 0; i < 15; i++) {
var graphQLJSON = JSON.parse(posts[i]);
articleList_json.push({
"title": graphQLJSON.data.title,
});
}
logInfo(articleList_json);
JSON.parse()
取一个json结构的string并解析它:
JSON.parse() 静态方法解析 JSON 字符串,构造字符串描述的 JavaScript 值或对象。可以提供可选的 reviver 函数,以便在返回结果对象之前对其执行转换。
https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Global_Objects/JSON/parse
JSON 字符串示例:
const json = '{"result":true, "count":42}';
const obj = JSON.parse(json);
HttpClientRequest
返回一个 HttpClientResponse
对象,在解析它之前必须先将其转换为字符串(条件是返回的字符串是有效的 json 结构):
var content = response.body.toString(response.codePage);
var posts = JSON.parse(content);
https://experienceleague.adobe.com/developer/campaign-api/api/p-HttpClientResponse-body.html
完整的文档示例:
var http = new HttpClientRequest("http://www.google.com/")
http.execute()
var response = http.response
if( response.code != 200 )
throw "HTTP request failed with " + response.message
var content = response.body.toString(response.codePage)
终于开始工作了
var response = req.response;
var jsonData = JSON.parse(response.body)
var articleList_json = [];
var i;
for (i = 0; (i < 15 && i < jsonData.data.all_insight_article.items.length); i++) {
articleList_json.push({
"title": jsonData.data.all_insight_article.items[i].title.toString()
});
}
logInfo("Count "+i);
logInfo(JSON.stringify(articleList_json));
对象数组的控制台日志
[
{
"title": "Six key differences between private equity and public market investing"
},
{
"title": "Securing the supply of rare earth metals is central to our energy transition"
},
{
"title": "Test article for charts"
},
{
"title": "Central banks hike rates again - but for how much longer?"
}
]