如何在Javascript中将文本转换为字符串?

时间:2019-01-24 07:14:25

标签: javascript angularjs text-parsing string-parsing

我正在使用AngularJs应用程序。

我正在从后端以Base64编码格式获取一些数据。

当我尝试对数据进行解码时,我得到的是文本格式的数据(不带引号)。

因此我无法显示数据(不确定)

有什么方法可以将文本转换为字符串?

这是我的代码:

   //Encoded data
    templateData.content = "U1lOVEFYX1ZFUlNJT04gMTMKCkdFTkVSSUNfU09VUkNFICJTeXNfU3lzbG9nU3RyZWFtaW5nIgpERVNDUklQVElPTiAiVGhpcyBwb2xpY3kgbm9ybWFsaXplcyB0aGUgc3lzbG9nIG1lc3NhZ2VzIGFuZCBjb3ZlcnRzIHRvIEpTT04gZGF0YS4KVGhlIGxpc3Qgb2YgbG9nIGZpbGVzIGFyZSByZWFkIGZyb20gJy9ldGMvcnN5c2xvZy5jb25mJyBvciAnL2V0Yy9zeXNsb2cuY29uZicgKGJhc2VkIG9uIHRoZSBPUywgdmVyc2lvbikuCkZvciBlLmcuIFwiL3Zhci9sb2cvbWVzc2FnZXNcIiwgXCIvdmFyL2xvZy9jcm9uXCIsIFwiL3Zhci9sb2cvbWFpbGxvZ1wiIgpQT0xUWVBFICJnZW5vdXRzdHJ1bG8iCiAgIEdST1VQICJST09UIgogICAgICBHUk9VUCAiY29udGVudCIKICAgICAgICAgR1JPVVAgIm9wdGlvbnMiCiAgICAgICAgIEdST1VQX0VORAogICAgICAgICBHUk9VUCAic291cmNlcyIKICAgICAgICAgICAgR1JPVVAgIktFWU1BUFJVTEUiCiAgICAgICAgICAgICAgIFBBUkFNICJLRUVQX0lOUFVUX0ZJRUxEUyIgICIwIgogICAgICAgICAgICAgICBHUk9VUCAiS0VZRklFTERfTUFQUElOR1MiCiAgICAgICAgICAgICAgICAgIFBBUkFNICJLRl9NQVAiICAiS0VFUCIKICAgICAgICAgICAgICAgICAgQVRUICJLRVlfSU4iICAibG9nX2hvc3RuYW1lIgogICAgICAgICAgICAgICAgICBBVFQgIktFWV9PVVQiICAibG9nX2hvc3RuYW1lIgogICAgICAgICAgICAgICAgICBQQVJBTSAiS0ZfTUFQIiAgIktFRVAiCiAgICAgICAgICAgICAgICAgIEFUVCAiS0VZX0lOIiAgIm1lc3NhZ2UiCiAgICAgICAgICAgICAgICAgIEFUVCAiS0VZX09VVCIgICJtZXNzYWdlIgogICAgICAgICAgICAgICBHUk9VUF9FTkQKICAgICAgICAgICAgICAgR1JPVVAgIkFERElUSU9OQUxfRklFTERTIgogICAgICAgICAgICAgICAgICBQQVJBTSAiQUREX0ZJRUxEIiAgIkFERCIKICAgICAgICAgICAgICAgICAgQVRUICJOQU1FIiAgInRpbWVzdGFtcCIKICAgICAgICAgICAgICAgICAgQVRUICJWQUxVRSIgICIkWVlZWS08JE1BUChtYXBtb250aCw8JERBVEE6bW9udGg+KT4tPCREQVRBOmRhdGU+IDwkREFUQTp0aW1lPiRUSU1FWk9ORSIKICAgICAgICAgICAgICAgICAgUEFSQU0gIkFERF9GSUVMRCIgICJBREQiCiAgICAgICAgICAgICAgICAgIEFUVCAiTkFNRSIgICJkZXZpY2VfdmVuZG9yIgogICAgICAgICAgICAgICAgICBBVFQgIlZBTFVFIiAgIkluZnJhc3RydWN0dXJlIgogICAgICAgICAgICAgICAgICBQQVJBTSAiQUREX0ZJRUxEIiAgIkFERCIKICAgICAgICAgICAgICAgICAgQVRUICJOQU1FIiAgImRldmljZV9wcm9kdWN0IgogICAgICAgICAgICAgICAgICBBVFQgIlZBTFVFIiAgInN5c2xvZyIKICAgICAgICAgICAgICAgICAgUEFSQU0gIkFERF9GSUVMRCIgICJBREQiCiAgICAgICAgICAgICAgICAgIEFUVCAiTkFNRSIgICJkZXZpY2VfdmVyc2lvbiIKICAgICAgICAgICAgICAgICAgQVRUICJWQUxVRSIgICIxIgogICAgICAgICAgICAgICAgICBQQVJBTSAiQUREX0ZJRUxEIiAgIkFERCIKICAgICAgICAgICAgICAgICAgQVRUICJOQU1FIiAgInNldmVyaXR5IgogICAgICAgICAgICAgICAgICBBVFQgIlZBTFVFIiAgIk1lZGl1bSIKICAgICAgICAgICAgICAgICAgUEFSQU0gIkFERF9GSUVMRCIgICJBREQiCiAgICAgICAgICAgICAgICAgIEFUVCAiTkFNRSIgICJob3N0bmFtZSIKICAgICAgICAgICAgICAgICAgQVRUICJWQUxVRSIgICIkSE9TVE5BTUUiCiAgICAgICAgICAgICAgICAgIFBBUkFNICJBRERfRklFTEQiICAiQUREIgogICAgICAgICAgICAgICAgICBBVFQgIk5BTUUiICAicGF0aCIKICAgICAgICAgICAgICAgICAgQVRUICJWQUxVRSIgICIkTE9HU09VUkNFIgogICAgICAgICAgICAgICBHUk9VUF9FTkQKICAgICAgICAgICAgR1JPVVBfRU5ECiAgICAgICAgICAgIFBBUkFNICJsb2dwYXRoIiAgIjxgaXNwaS1vdnBlcmwgZ2V0U3lzbG9nUGF0aC5wbGA+JSVoZWxsbyUlIgogICAgICAgICAgICBQQVJBTSAiaW50ZXJ2YWwiICAiMW0wcyIKICAgICAgICAgICAgUEFSQU0gImNoU2V0IiAgIjY5IgogICAgICAgICAgICBQQVJBTSAicmVhZE1vZGUiICAiZnJvbUxhc3RQb3MiCiAgICAgICAgICAgIFBBUkFNICJub0xvZ2ZpbGVNc2ciICAiMSIKICAgICAgICAgICAgUEFSQU0gImNsb3NlQWZ0ZXJSZWFkIiAgIjEiCiAgICAgICAgICAgIFBBUkFNICJwYXR0ZXJuIiAgIl48MyoubW9udGg+WyAgfCBdPCMuZGF0ZT4gPEAudGltZT4gPEAubG9nX2hvc3RuYW1lPiA8Ki5tZXNzYWdlPiIKICAgICAgICAgICAgR1JPVVAgInN0YXJ0UGF0dGVybnMiCiAgICAgICAgICAgIEdST1VQX0VORAogICAgICAgICBHUk9VUF9FTkQKICAgICAgR1JPVVBfRU5ECiAgIEdST1VQX0VORAoKICAgICAgICBNQVAgIm1hcG1vbnRoIgogICAgICAgIERFU0NSSVBUSU9OICIiCiAgICAgICAgSU5QVVQgIjwkREFUQTptb250aD4iCiAgICAgICAgICBGUk9NICJKYW4iIFRPICIwMSIKICAgICAgICAgIEZST00gIkZlYiIgVE8gIjAyIgogICAgICAgICAgRlJPTSAiTWFyIiBUTyAiMDMiCiAgICAgICAgICBGUk9NICJBcHIiIFRPICIwNCIKICAgICAgICAgIEZST00gIk1heSIgVE8gIjA1IgogICAgICAgICAgRlJPTSAiSnVuIiBUTyAiMDYiCiAgICAgICAgICBGUk9NICJKdWwiIFRPICIwNyIKICAgICAgICAgIEZST00gIkF1ZyIgVE8gIjA4IgogICAgICAgICAgRlJPTSAiU2VwIiBUTyAiMDkiCiAgICAgICAgICBGUk9NICJPY3QiIFRPICIxMCIKICAgICAgICAgIEZST00gIk5vdiIgVE8gIjExIgogICAgICAgICAgRlJPTSAiRGVjIiBUTyAiMTIiCgogICAgICAgIERFRkFVTFRNU0cK";

    //Decoding
    data.content = Base64.decode(templateData.content);

    console.log("data",data) // data { content: undefined }

解码后,数据如下:

SYNTAX_VERSION 13

GENERIC_SOURCE "Sys_SyslogStreaming"
DESCRIPTION "This policy normalizes the syslog messages and coverts to JSON data.
The list of log files are read from '/etc/rsyslog.conf' or '/etc/syslog.conf' (based on the OS, version).
For e.g. \"/var/log/messages\", \"/var/log/cron\", \"/var/log/maillog\""
POLTYPE "genoutstrulo"
   GROUP "ROOT"
      GROUP "content"
         GROUP "options"
         GROUP_END
         GROUP "sources"
            GROUP "KEYMAPRULE"
               PARAM "KEEP_INPUT_FIELDS"  "0"
               GROUP "KEYFIELD_MAPPINGS"
                  PARAM "KF_MAP"  "KEEP"
                  ATT "KEY_IN"  "log_hostname"
                  ATT "KEY_OUT"  "log_hostname"
                  PARAM "KF_MAP"  "KEEP"
                  ATT "KEY_IN"  "message"
                  ATT "KEY_OUT"  "message"
               GROUP_END
               GROUP "ADDITIONAL_FIELDS"
                  PARAM "ADD_FIELD"  "ADD"
                  ATT "NAME"  "timestamp"
                  ATT "VALUE"  "$YYYY-<$MAP(mapmonth,<$DATA:month>)>-<$DATA:date> <$DATA:time>$TIMEZONE"
                  PARAM "ADD_FIELD"  "ADD"
                  ATT "NAME"  "device_vendor"
                  ATT "VALUE"  "Infrastructure"
                  PARAM "ADD_FIELD"  "ADD"
                  ATT "NAME"  "device_product"
                  ATT "VALUE"  "syslog"
                  PARAM "ADD_FIELD"  "ADD"
                  ATT "NAME"  "device_version"
                  ATT "VALUE"  "1.0"
                  PARAM "ADD_FIELD"  "ADD"
                  ATT "NAME"  "severity"
                  ATT "VALUE"  "Medium"
                  PARAM "ADD_FIELD"  "ADD"
                  ATT "NAME"  "hostname"
                  ATT "VALUE"  "$HOSTNAME"
                  PARAM "ADD_FIELD"  "ADD"
                  ATT "NAME"  "path"
                  ATT "VALUE"  "$LOGSOURCE"
               GROUP_END
            GROUP_END
            PARAM "logpath"  "<`ispi-ovperl getSyslogPath.pl`>"
            PARAM "interval"  "1m0s"
            PARAM "chSet"  "69"
            PARAM "readMode"  "fromLastPos"
            PARAM "noLogfileMsg"  "1"
            PARAM "closeAfterRead"  "1"
            PARAM "pattern"  "^<3*.month>[  | ]<#.date> <@.time> <@.log_hostname> <*.message>"
            GROUP "startPatterns"
            GROUP_END
         GROUP_END
      GROUP_END
   GROUP_END

        MAP "mapmonth"
        DESCRIPTION ""
        INPUT "<$DATA:month>"
          FROM "Jan" TO "01"
          FROM "Nov" TO "11"
          FROM "Dec" TO "12"

        DEFAULTMSG

由于angularjs无法识别数据类型而未定义。

我尝试了toString方法。我也尝试在引号周围添加引号。但没有任何效果。

有人可以帮我将这些数据转换为字符串吗?

1 个答案:

答案 0 :(得分:0)

您可以在大多数浏览器上本地执行此操作。您是否尝试过atob()方法。

通过

atob(templateData.content)

在浏览器控制台或angular组件中,您将获得返回的解码字符串。

StackBliz

有关详细说明,请参阅question