2014-09-12 121 views
0

一般来说,我解析为遵循XML字符串:解析在vb.net字符串

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"} 

我该怎么做?

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

+0

[Json中的解析不使用XML。 VB.Net](http://stackoverflow.com/questions/25808129/parsing-in-json-not-in-xml-vb-net) – har07 2014-09-12 14:35:02

+1

这是JSON(javascript对象表示法)。有许多JSON解析器可用于.Net,其中有一些在Nuget上。 – 2014-09-12 14:41:49

+1

如果您不想使用其中一个JSON解析器,我会将逗号分割为字符串以查找键/值对,然后再次在冒号中检索键和值。 – Seth 2014-09-12 14:44:02

回答

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