尝试使用 javascript 在终端中记录 JSON 响应
Trying to log JSON response in terminal using javascript
当我键入以下命令时
console.log("Response = " +JSON.stringify(response.body));
我收到以下回复
Response = {"data":{"getPortalByOrganizationAliasAndSlug":{"portalId":"123f3redvq","expireAt":null,"accountId":"1","templateId":"stephendemo","arePortalDefaultsOverriden":null,"template":{"allContent":null,"digitalAssets":[],"recommendedContent":null,"allTopics":null,"recommendedTopics":null,"templateId":"stephendemo","organizationId":"fqewf2134214","header":"","footer":"","favicon":"null","logoPath":"/img/pages/logo.png","logo":"https://dev-v3-23-dev-assets.s3.amazonaws.com/r232r/232332/3232/stephendemo/image-logo-1603086511.png","primaryColor":"undefined","secondaryColor":"#b5b3b3","labels":{"key":"key","value":"value","__typename":"Tuple"},"orgLegalName":"Stephen Carvalho","supportTextLine":"Support","supportEmailAddress":null,"termsOfService":"http://2323.com","copyright":"Stephen Carvalho","invitationDelivery":"MANUALLY","magicLink":false,"emailAndPAssword":false,"scope":"2332","searchPlaceholder":"search","inputPlaceholder":null,"orgDisplayName":"Stephen","styles":{"contentCards":{"type":"STANDARD","__typename":"ContentCardStyle"},"__typename":"TemplateStyles"},"__typename":"TemplatePortalContent"},"templateBuyerPortal":null,"driftEnabled":false,"driftConfig":null,"portalState":"enabled","portalSlug":"stephendemo","userOwnerId":"1","userOwner":{"organizationId":"oSvjLe1paYKg","firstName":"Stephen","lastName":"Carvalho","email":"stephen.omedym+stephensdemo@gmail.com","accountId":"1","portalIds":null,"phone":null,"title":null,"__typename":"User"},"teamMembers":null,"organizationAlias":"stephensdemo","companyId":1,"organizationId":"oSvjLe1paYKg","scope":"CONTENT_PORTAL","isContentPortalv3":true,"landingPageUrl":null,"isLandingPageUrlEnabled":false,"blockedDomains":[],"exclusiveContent":{"digitalAssets":[{"id":"2","status":5,"__typename":"ExclusiveDigitalAsset"}],"__typename":"ExclusiveContentType"},"__typename":"Portal"}}} source=console
我想将门户 ID 保存在变量中。
我试过这个命令首先只打印数据变量中的部分
let responsedata = JSON.parse(response.body.data);
console.log(responsedata);
但是我得到以下错误
ERRO[0002] SyntaxError: invalid character 'u' looking for beginning of value
running at parse (native)
即使我尝试以下命令,我也会得到同样的错误
let responsedata = JSON.parse(response.body.data.getPortalByOrganizationAliasAndSlug.portalId);
console.log(responsedata);
谁能帮我理解如何在响应中打印变量,例如 portalID 或 accounted 的值?
也许 response.body
已经被解析。
并且 JSON.stringify(response.body)
再次将其转换为 JSON 并将有效的 json 打印到控制台。
尝试在没有 JSON.stringify()
的情况下记录 response.body
并查看它是否打印 javascript 对象。
如果有,就没有必要JSON.parse(response.body.data)
。
像通常使用 javascript 对象一样使用它。
您正在尝试解析 json 中的值,而不是 json 响应正文中的值。试试下面的代码。
let responsedata = JSON.parse(response.body).data;
console.log(responsedata);
当我键入以下命令时
console.log("Response = " +JSON.stringify(response.body));
我收到以下回复
Response = {"data":{"getPortalByOrganizationAliasAndSlug":{"portalId":"123f3redvq","expireAt":null,"accountId":"1","templateId":"stephendemo","arePortalDefaultsOverriden":null,"template":{"allContent":null,"digitalAssets":[],"recommendedContent":null,"allTopics":null,"recommendedTopics":null,"templateId":"stephendemo","organizationId":"fqewf2134214","header":"","footer":"","favicon":"null","logoPath":"/img/pages/logo.png","logo":"https://dev-v3-23-dev-assets.s3.amazonaws.com/r232r/232332/3232/stephendemo/image-logo-1603086511.png","primaryColor":"undefined","secondaryColor":"#b5b3b3","labels":{"key":"key","value":"value","__typename":"Tuple"},"orgLegalName":"Stephen Carvalho","supportTextLine":"Support","supportEmailAddress":null,"termsOfService":"http://2323.com","copyright":"Stephen Carvalho","invitationDelivery":"MANUALLY","magicLink":false,"emailAndPAssword":false,"scope":"2332","searchPlaceholder":"search","inputPlaceholder":null,"orgDisplayName":"Stephen","styles":{"contentCards":{"type":"STANDARD","__typename":"ContentCardStyle"},"__typename":"TemplateStyles"},"__typename":"TemplatePortalContent"},"templateBuyerPortal":null,"driftEnabled":false,"driftConfig":null,"portalState":"enabled","portalSlug":"stephendemo","userOwnerId":"1","userOwner":{"organizationId":"oSvjLe1paYKg","firstName":"Stephen","lastName":"Carvalho","email":"stephen.omedym+stephensdemo@gmail.com","accountId":"1","portalIds":null,"phone":null,"title":null,"__typename":"User"},"teamMembers":null,"organizationAlias":"stephensdemo","companyId":1,"organizationId":"oSvjLe1paYKg","scope":"CONTENT_PORTAL","isContentPortalv3":true,"landingPageUrl":null,"isLandingPageUrlEnabled":false,"blockedDomains":[],"exclusiveContent":{"digitalAssets":[{"id":"2","status":5,"__typename":"ExclusiveDigitalAsset"}],"__typename":"ExclusiveContentType"},"__typename":"Portal"}}} source=console
我想将门户 ID 保存在变量中。
我试过这个命令首先只打印数据变量中的部分
let responsedata = JSON.parse(response.body.data);
console.log(responsedata);
但是我得到以下错误
ERRO[0002] SyntaxError: invalid character 'u' looking for beginning of value
running at parse (native)
即使我尝试以下命令,我也会得到同样的错误
let responsedata = JSON.parse(response.body.data.getPortalByOrganizationAliasAndSlug.portalId);
console.log(responsedata);
谁能帮我理解如何在响应中打印变量,例如 portalID 或 accounted 的值?
也许 response.body
已经被解析。
并且 JSON.stringify(response.body)
再次将其转换为 JSON 并将有效的 json 打印到控制台。
尝试在没有 JSON.stringify()
的情况下记录 response.body
并查看它是否打印 javascript 对象。
如果有,就没有必要JSON.parse(response.body.data)
。
像通常使用 javascript 对象一样使用它。
您正在尝试解析 json 中的值,而不是 json 响应正文中的值。试试下面的代码。
let responsedata = JSON.parse(response.body).data;
console.log(responsedata);