从asp到json的新行,然后返回jquery

时间:2017-12-11 09:12:51

标签: jquery vbscript asp-classic

我有一个.asp文件,可以将数据从数据库转换为json。问题是出现新的线符号时。在为json调用文件时,我得到了:

  

"错误:第1行的解析错误: - json code sample--期待   ' STRING',' NUMBER',' NULL'' TRUE',' FALSE',' { ',' ['"

在新线路的地方。 我的问题是有没有办法解决它而不改变json接收程序结束时的代码。并且能够使用jquery用json中的数据填充aps表单字段并保持新行完整。

我试图将vbcrvbcrlf替换为\n\ \ n\ \ \ \n,没有任何帮助。

我希望我能够清楚地解释清楚。

UPDATE 它的clasic ASP VBSCIPT。 (source.asp - > json - > dest.asp使用jquery来解析json并将数据放到表单字段中。) 一切正常,除非有新的线符号" cr fl"在DATA中的某个地方,source.asp正在制作json。然后,dest.asp使用json数据,jquery用json数据填充dest.asp表单字段。问题是json似乎无法识别由vb脚本放置的vbCrLf符号来标记新行。如果我用""在source.asp中,一切正常,但后来我无法在dest.asp中保留换行符,我想知道是否有任何方法可以在不更改dest.asp中的代码的情况下修复/破解它

3 个答案:

答案 0 :(得分:0)

请从链接下载此图片

http://uupload.ir/files/vir8_capture.jpg

如果您使用\ n可能是您的代码不正确

答案 1 :(得分:0)

如果您不愿意显示源代码和导致问题的数据,那么我们分析问题的能力就会受到限制。但是,我可以提出另一个建议。

如果您正在使用某种自制代码来解析/格式化JSON,这会阻塞简单字符(如换行符),那么您可能需要考虑使用已被数百人尝试和测试的其他内容。我提供的是一个名为VBSJson的类,它有两个名为Encode的公共函数(将VBScript变量,数组或字典转换为JSON)和Decode(将JSON转换为VBScript本机值)。它为我们的团队服务了2年,而且记录良好。

<%

    Class VbsJson
        ' Author: Demon
        ' Date: 2012/5/3
        ' Website: http://demon.tw/my-work/vbs-json.html
        Private Whitespace, NumberRegex, StringChunk
        Private b, f, r, n, t

        Private Sub Class_Initialize
            Whitespace = " " & vbTab & vbCr & vbLf
            b = ChrW(8)
            f = vbFormFeed
            r = vbCr
            n = vbLf
            t = vbTab

            Set NumberRegex = New RegExp
            NumberRegex.Pattern = "(-?(?:0|[1-9]\d*))(\.\d+)?([eE][-+]?\d+)?"
            NumberRegex.Global = False
            NumberRegex.MultiLine = True
            NumberRegex.IgnoreCase = True

            Set StringChunk = New RegExp
            StringChunk.Pattern = "([\s\S]*?)([""\\\x00-\x1f])"
            StringChunk.Global = False
            StringChunk.MultiLine = True
            StringChunk.IgnoreCase = True
        End Sub

        ' Return a JSON string representation of a VBScript data structure
        ' Supports the following objects and types
        ' +-------------------+---------------+
        ' | VBScript          | JSON          |
        ' +===================+===============+
        ' | Dictionary        | object        |
        ' | Array             | array         |
        ' | String            | string        |
        ' | Number            | number        |
        ' | True              | true          |
        ' | False             | false         |
        ' | Null              | null          |
        ' +-------------------+---------------+
        Public Function Encode(ByRef obj)
            Dim buf, i, c, g
            Set buf = CreateObject("Scripting.Dictionary")
            Select Case VarType(obj)
                Case vbNull
                    buf.Add buf.Count, "null"
                Case vbBoolean
                    If obj Then
                        buf.Add buf.Count, "true"
                    Else
                        buf.Add buf.Count, "false"
                    End If
                Case vbInteger, vbLong, vbSingle, vbDouble
                    buf.Add buf.Count, obj
                Case vbString
                    buf.Add buf.Count, """"
                    For i = 1 To Len(obj)
                        c = Mid(obj, i, 1)
                        Select Case c
                            Case """" buf.Add buf.Count, "\"""
                            Case "\"  buf.Add buf.Count, "\\"
                            Case "/"  buf.Add buf.Count, "/"
                            Case b    buf.Add buf.Count, "\b"
                            Case f    buf.Add buf.Count, "\f"
                            Case r    buf.Add buf.Count, "\r"
                            Case n    buf.Add buf.Count, "\n"
                            Case t    buf.Add buf.Count, "\t"
                            Case Else
                                If AscW(c) >= 0 And AscW(c) <= 31 Then
                                    c = Right("0" & Hex(AscW(c)), 2)
                                    buf.Add buf.Count, "\u00" & c
                                Else
                                    buf.Add buf.Count, c
                                End If
                        End Select
                    Next
                    buf.Add buf.Count, """"
                Case vbArray + vbVariant
                    g = True
                    buf.Add buf.Count, "["
                    For Each i In obj
                        If g Then g = False Else buf.Add buf.Count, ","
                        buf.Add buf.Count, Encode(i)
                    Next
                    buf.Add buf.Count, "]"
                Case vbObject
                    If TypeName(obj) = "Dictionary" Then
                        g = True
                        buf.Add buf.Count, "{"
                        For Each i In obj
                            If g Then g = False Else buf.Add buf.Count, ","
                            buf.Add buf.Count, """" & i & """" & ":" & Encode(obj(i))
                        Next
                        buf.Add buf.Count, "}"
                    Else
                        Err.Raise 8732,,"None dictionary object"
                    End If
                Case Else
                    buf.Add buf.Count, """" & CStr(obj) & """"
            End Select
            Encode = Join(buf.Items, "")
        End Function

        ' Return the VBScript representation of ``str(``
        ' Performs the following translations in decoding
        ' +---------------+-------------------+
        ' | JSON          | VBScript          |
        ' +===============+===================+
        ' | object        | Dictionary        |
        ' | array         | Array             |
        ' | string        | String            |
        ' | number        | Double            |
        ' | true          | True              |
        ' | false         | False             |
        ' | null          | Null              |
        ' +---------------+-------------------+
        Public Function Decode(ByRef str)
            Dim idx
            idx = SkipWhitespace(str, 1)

            If Mid(str, idx, 1) = "{" Then
                Set Decode = ScanOnce(str, 1)
            Else
                Decode = ScanOnce(str, 1)
            End If
        End Function

        Private Function ScanOnce(ByRef str, ByRef idx)
            Dim c, ms

            idx = SkipWhitespace(str, idx)
            c = Mid(str, idx, 1)

            If c = "{" Then
                idx = idx + 1
                Set ScanOnce = ParseObject(str, idx)
                Exit Function
            ElseIf c = "[" Then
                idx = idx + 1
                ScanOnce = ParseArray(str, idx)
                Exit Function
            ElseIf c = """" Then
                idx = idx + 1
                ScanOnce = ParseString(str, idx)
                Exit Function
            ElseIf c = "n" And StrComp("null", Mid(str, idx, 4)) = 0 Then
                idx = idx + 4
                ScanOnce = Null
                Exit Function
            ElseIf c = "t" And StrComp("true", Mid(str, idx, 4)) = 0 Then
                idx = idx + 4
                ScanOnce = True
                Exit Function
            ElseIf c = "f" And StrComp("false", Mid(str, idx, 5)) = 0 Then
                idx = idx + 5
                ScanOnce = False
                Exit Function
            End If

            Set ms = NumberRegex.Execute(Mid(str, idx))
            If ms.Count = 1 Then
                idx = idx + ms(0).Length
                ScanOnce = CDbl(ms(0))
                Exit Function
            End If

            Err.Raise 8732,,"No JSON object could be ScanOnced"
        End Function

        Private Function ParseObject(ByRef str, ByRef idx)
            Dim c, key, value
            Set ParseObject = CreateObject("Scripting.Dictionary")
            idx = SkipWhitespace(str, idx)
            c = Mid(str, idx, 1)

            If c = "}" Then
                Exit Function
            ElseIf c <> """" Then
                Err.Raise 8732,,"Expecting property name"
            End If

            idx = idx + 1

            Do
                key = ParseString(str, idx)

                idx = SkipWhitespace(str, idx)
                If Mid(str, idx, 1) <> ":" Then
                    Err.Raise 8732,,"Expecting : delimiter"
                End If

                idx = SkipWhitespace(str, idx + 1)
                If Mid(str, idx, 1) = "{" Then
                    Set value = ScanOnce(str, idx)
                Else
                    value = ScanOnce(str, idx)
                End If
                ParseObject.Add key, value

                idx = SkipWhitespace(str, idx)
                c = Mid(str, idx, 1)
                If c = "}" Then
                    Exit Do
                ElseIf c <> "," Then
                    Err.Raise 8732,,"Expecting , delimiter. Got " & c & " at " & idx
                End If

                idx = SkipWhitespace(str, idx + 1)
                c = Mid(str, idx, 1)
                If c <> """" Then
                    Err.Raise 8732,,"Expecting property name"
                End If

                idx = idx + 1
            Loop

            idx = idx + 1
        End Function

        Private Function ParseArray(ByRef str, ByRef idx)
            Dim c, values, value
            Set values = CreateObject("Scripting.Dictionary")
            idx = SkipWhitespace(str, idx)
            c = Mid(str, idx, 1)

            If c = "]" Then
                idx = idx + 1
                ParseArray = values.Items
                Exit Function
            End If

            Do
                idx = SkipWhitespace(str, idx)
                If Mid(str, idx, 1) = "{" Then
                    Set value = ScanOnce(str, idx)
                Else
                    value = ScanOnce(str, idx)
                End If
                values.Add values.Count, value

                idx = SkipWhitespace(str, idx)
                c = Mid(str, idx, 1)
                If c = "]" Then
                    Exit Do
                ElseIf c <> "," Then
                    Err.Raise 8732,,"Expecting , delimiter"
                End If

                idx = idx + 1
            Loop

            idx = idx + 1
            ParseArray = values.Items
        End Function

        Private Function ParseString(ByRef str, ByRef idx)
            Dim chunks, content, terminator, ms, esc, char
            Set chunks = CreateObject("Scripting.Dictionary")

            Do
                Set ms = StringChunk.Execute(Mid(str, idx))
                If ms.Count = 0 Then
                    Err.Raise 8732,,"Unterminated string starting"
                End If

                content = ms(0).Submatches(0)
                terminator = ms(0).Submatches(1)
                If Len(content) > 0 Then
                    chunks.Add chunks.Count, content
                End If

                idx = idx + ms(0).Length

                If terminator = """" Then
                    Exit Do
                ElseIf terminator <> "\" Then
                    Err.Raise 8732,,"Invalid control character"
                End If

                esc = Mid(str, idx, 1)

                If esc <> "u" Then
                    Select Case esc
                        Case """" char = """"
                        Case "\"  char = "\"
                        Case "/"  char = "/"
                        Case "b"  char = b
                        Case "f"  char = f
                        Case "n"  char = n
                        Case "r"  char = r
                        Case "t"  char = t
                        Case Else Err.Raise 8732,,"Invalid escape"
                    End Select
                    idx = idx + 1
                Else
                    char = ChrW("&H" & Mid(str, idx + 1, 4))
                    idx = idx + 5
                End If

                chunks.Add chunks.Count, char
            Loop

            ParseString = Join(chunks.Items, "")
        End Function

        Private Function SkipWhitespace(ByRef str, ByVal idx)
            Do While idx <= Len(str) And _
                InStr(Whitespace, Mid(str, idx, 1)) > 0
                idx = idx + 1
            Loop
            SkipWhitespace = idx
        End Function

    End Class

%>

答案 2 :(得分:0)

我发现的最佳答案是JsonConverter模块带来的。 安装以下说明后,您必须先使用json=JsonConverter.ParseJson(your_string)然后使用JsonConverter.ConvertToJson(json)解析字符串。