簡體   English   中英

Javascript Map 沒有正確轉義

[英]Javascript Map not Escaping properly

出於某種原因,我似乎無法在下面返回一組正確轉義的 JSON。 我的控制台提供了一長串格式不正確的數組。 只是想知道如何正確逃脫? 在此處輸入圖片說明

const songsLdJson = songs.map((song: any) => {
    return `{
        "@type": "MusicRecording",
        "byArtist": "${song.song?.artist.name}",
        "name": "${song.song?.name}",
        "url": "sweet-home-alabama",
        "interactionStatistic": {
            "@type": "InteractionCounter",
            "interactionType": "https://schema.org/ListenAction",
            "userInteractionCount": "${song.played_count}"
        },
        "thumbnailUrl": "${song.song?.spotifyImg300}",
        "audio": {
          "@type": "AudioObject",
          "contentUrl": "${song.song?.preview_url}",
          "description": "${song.sceneDescription}",
          "encodingFormat": "audio/mpeg",
        }
      }`
  })
  console.log(songsLdJson, "JSON")

您真的想將該對象創建為字符串嗎? 最好將其創建為對象,然后根據需要進行字符串化。

const songsLdJson = songs.map((song: any) => {
  return {
    "@type": "MusicRecording",
    "byArtist": `${song.song?.artist.name}`,
    "name": `${song.song?.name}`,
    "url": "sweet-home-alabama",
    "interactionStatistic": {
        "@type": "InteractionCounter",
        "interactionType": "https://schema.org/ListenAction",
        "userInteractionCount": `${song.played_count}`
    },
    "thumbnailUrl": `${song.song?.spotifyImg300}`,
    "audio": {
      "@type": "AudioObject",
      "contentUrl": `${song.song?.preview_url}`,
      "description": `${song.sceneDescription}`,
      "encodingFormat": "audio/mpeg",
    }
  }
})
console.log(songsLdJson, "JSON")
// if you want that then turned into a string
songsLdJson = JSON.stringify(songsLdJson);

出於某種原因,我似乎無法在下面返回一組正確轉義的 JSON。 我的控制台提供了一長串格式不正確的數組。 只是想知道如何正確逃脫? 在此處輸入圖片說明

const songsLdJson = songs.map((song: any) => {
    return `{
        "@type": "MusicRecording",
        "byArtist": "${song.song?.artist.name}",
        "name": "${song.song?.name}",
        "url": "sweet-home-alabama",
        "interactionStatistic": {
            "@type": "InteractionCounter",
            "interactionType": "https://schema.org/ListenAction",
            "userInteractionCount": "${song.played_count}"
        },
        "thumbnailUrl": "${song.song?.spotifyImg300}",
        "audio": {
          "@type": "AudioObject",
          "contentUrl": "${song.song?.preview_url}",
          "description": "${song.sceneDescription}",
          "encodingFormat": "audio/mpeg",
        }
      }`
  })
  console.log(songsLdJson, "JSON")

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM