JSON个文件怎么解析成name:value?

How can JSON files be parse into name: value?

我使用 puppeteer 生成了一个网页 cookie,如下所示:

[
  {
    "name": "__Secure-3PSIDCC",
    "value": "sec",
    "domain": ".youtube.com",
    "path": "/",
    "expires": 1677041890.31475,
    "size": 88,
    "httpOnly": true,
    "secure": true,
    "session": false,
    "sameSite": "None",
    "sameParty": false,
    "sourceScheme": "Secure",
    "sourcePort": 443
  },
  {
    "name": "SIDCC",
    "value": "sec",
    "domain": ".youtube.com",
    "path": "/",
    "expires": 1677041890.314666,
    "size": 79,
    "httpOnly": false,
    "secure": false,
    "session": false,
    "sameParty": false,
    "sourceScheme": "Secure",
    "sourcePort": 443
  }... //and so on

如何解析 json 使其成为 name.value? 我现在做的是:

const cookies = await page.cookies();
await fs.writeFile('./cookies.json', JSON.stringify(cookies, null, 2));

问题是 youtube 不允许在文件中使用 cookie... 任何解决方案,以便我可以将 json 格式化为 name.value? 想要的输出将是 The reseult shold be like this __Secure-3PSIDCC:sec; SIDCC=sec; another=value; 谢谢

假设你问题中的数组是cookies中的值,你可以map the array to the values you want and then join把它变成最终的字符串

const cookies = [{"name":"__Secure-3PSIDCC","value":"sec","domain":".youtube.com","path":"/","expires":1677041890.31475,"size":88,"httpOnly":true,"secure":true,"session":false,"sameSite":"None","sameParty":false,"sourceScheme":"Secure","sourcePort":443},{"name":"SIDCC","value":"sec","domain":".youtube.com","path":"/","expires":1677041890.314666,"size":79,"httpOnly":false,"secure":false,"session":false,"sameParty":false,"sourceScheme":"Secure","sourcePort":443}]

const cookieString = cookies.map(({ name, value }) =>
  `${name}=${value}`).join("; ")
  
console.log("cookieString:", cookieString)