gpt4 book ai didi

google-apps-script - 我将如何从解析的 json 对象中捕获和处理 "undefined"

转载 作者:行者123 更新时间:2023-12-04 22:49:09 26 4
gpt4 key购买 nike

我正在尝试从 RESTful 服务填充电子表格,有时没有这样的属性,所以我的电子表格中出现“未定义”,脚本停止并给出错误。我不知道如何处理它,或者跳过“未定义”部分。

这是 json 中的示例查询

{

"rel": "self",
"url": "https://www.sciencebase.gov/catalog/items?max=2&s=Search&q=project+
2009+WLCI&format=json&fields=title%2Csummary%2Cspatial%2Cfacets",
"total": 65,
"nextlink": {
"rel": "next",
"url": "https://www.sciencebase.gov/catalog/items?max=2&s=
Search&q=project+2009+WLCI&format=json&fields=title%2Csummary%2
Cspatial%2Cfacets&offset=2"
},
"items": [
{
"link": {
"rel": "self",
"url": "https://www.sciencebase.gov/catalog/item/
4f4e4ac3e4b07f02db67875f"
},
"id": "4f4e4ac3e4b07f02db67875f",
"title": "Decision-Making and Evaluation - Social and Economic
Evaluation Supporting Adaptive Management for WLCI",
"summary": "Provide information on the social and economic environment
for the WLCI area and provide context for the biological and physical
aspects of this project.",
"spatial": {
"representationalPoint": [
-108.585,
42.141
]
},
"facets": [
{
"startDate": "2007-10-01 00:00:00",
"projectStatus": "Active",
"facetName": "Project",
"_class": "ProjectFacet",
"active": true,
"className": "gov.sciencebase.catalog.item.facet.ProjectFacet",
"endDate": null,
"projectType": "Science",
"_embeddedClassName": "gov.sciencebase.catalog.item.facet.
ProjectFacet"
}
]
},
{
"link": {
"rel": "self",
"url": "https://www.sciencebase.gov/catalog/item
/4f4e4ac0e4b07f02db676d57"
},
"id": "4f4e4ac0e4b07f02db676d57",
"title": "Data and Information Management Products for the Wyoming
Landscape Conservation Initiative"
}
]

}

由于第二个项目只有一个标题,之后没有更多内容,如果我尝试使用循环获取项目的摘要或方面等,我会得到“未定义”。我已经想到并尝试使用 if 语句,例如
if (parsedResponse.items[i].summary === "undefined") {
Do something here;
}

但这似乎不起作用。我可以尝试的任何建议表示赞赏。谢谢

最佳答案

一个解决方案是

if (parsedResponse.items[i].summary == undefined) {
Do something here;
}

或者
if (parsedResponse.items[i].summary == null) {
Do something here;
}

关于google-apps-script - 我将如何从解析的 json 对象中捕获和处理 "undefined",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11771384/

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