在vb.net中解析一个字符串

时间:2014-09-12 14:29:47

标签: json vb.net

通常我解析XML String如下:

我收到的XML字符串

<Status>string</Status>
        <RedirectURL>string</RedirectURL>

我解析它的方式

Dim sReason As String = "Unknown"

        Try
            xml.LoadXml(sResult)
            If xml.SelectSimpleNode("Status").InnerText = "PURCHASED" Then
                app.Outcome.RedirectURL = xml.SelectSimpleNode("RedirectUrl").InnerText
                AcceptLead()
                Return True
            End If

现在我需要解析一个未以XML格式传递的字符串,但是按照后面的方式传递给我

{"RedirectUrl":"www.test.com","Commission":5.0000,"Status":"accepted"}

我该怎么做?

值只是一个字符串,有人告诉我,我可以从字符串中提取数据,然后将其分配到我需要的地方,但我不知道如何。

1 个答案:

答案 0 :(得分:0)

sResult = sResult.Replace("""", String.Empty)
            If sResult.Contains("Status:accepted") Then
                Dim parts = sResult.Replace("{", String.Empty).Replace("}", String.Empty).Split(",")
                For i As Int16 = 0 To parts.Length - 1
                    If parts(i).StartsWith("RedirectUrl") Then
                        app.Outcome.RedirectURL = parts(i).Substring(12)
                    End If
                    If parts(i).StartsWith("Commission") Then
                        lendertier.LenderComm = CDec(parts(i).Substring(11))
                    End If
                    If parts(i).StartsWith("ApplicationRef") Then
                        app.Outcome.LenderReference = parts(i).Substring(15)
                    End If
                Next
                AcceptLead()
                Return True
            End If
            If sResult.Contains("Reason:Duplicate") Then
                sReason = "Duplicate"
            ElseIf sResult.Contains("{Error:invalid credentials") Then
                sReason = "Error: Invalid credentials"
            ElseIf sResult.Contains("ValidationErrors:") Then
                sReason = "Invalid call:" + sResult.Replace("ValidationErrors:", String.Empty).Replace(",Status:rejected", String.Empty)
            Else
                sReason = "Rejected"
            End If
            DeclineLead(sReason)
            Return False