2

At the moment I am using VB.Net. I build my string, post it out and then parse the results.

Parsing Example for XML

Dim xml As New MWXMLDocument()
            Dim sReason As String = "Unknown"

            Try
                xml.LoadXml(sresult)
                If xml.SelectSimpleNode("AcceptedLead").InnerText = "true" Then
                    app.Outcome.RedirectURL = xml.SelectSimpleNode("result/redirecturl").InnerText

                    AcceptLead()
                    Return True
                End If

                sReason = xml.SelectSimpleNode("Reason").InnerText
            Catch ex As Exception
                sReason = "Error: " & ex.Message
            End Try
            DeclineLead(sReason)
            Return False
        End Function

How would I parse a result sent back in JSON, here is an example of the result I want to parse in using VB : Can i not just get the data from the string and parse as normal XML.

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

4 Answers 4

2

You can use the JSON.NET Library

Example in C#:

var result = JsonConvert.DeserializeObject<RootObject>(string json);

The RootObject should be your own class.

Sign up to request clarification or add additional context in comments.

Comments

1

You could use the .Net built in JavaScriptSerialiser

First add a reference to System.Web.Extensions and then

Imports System.Web.Script.Serialization

Followed by...

Dim sExampleJSON As String = "{""RedirectUrl"":""www.test.com"",""Commission"":5.0000,""Status"":""accepted""}"

Dim MySerializer As JavaScriptSerializer = New JavaScriptSerializer()
Dim MyDictionary As Dictionary(Of String, Object) = MySerializer.Deserialize(Of Dictionary(Of String, Object))(sExampleJSON)

If MyDictionary.ContainsKey("RedirectUrl") Then
  Console.WriteLine(MyDictionary("RedirectUrl"))
End If

6 Comments

What if the sExampleJSON string value changes, i just added that as an example.
Well then you just deserialise it again. The MyDictionary will then contain the new deserialised JSON results.
could you provide an example please
I don't understand what you mean, an example of what exactly?
I understand all of your example, but i dont understand Dim **** sExampleJSON As String = "{""RedirectUrl"":""www.test.com"",""Commission"":5.0000,""Status"":""accepted""}" ***** , those value's you filled in , how do i collect those value's after the result has been sent back to me
|
1

in global.asax.cs

using System.Data.Entity;

   namespace RpManticSolAPI
  {
  public class WebApiApplication : System.Web.HttpApplication
  {
    protected void Application_Start()
    {
          GlobalConfiguration.Configure(WebApiConfig.Register);
          GlobalConfiguration.Configuration.Formatters.JsonFormatter.SerializerSettings.ReferenceLoopHandling = Newtonsoft.Json.ReferenceLoopHandling.Ignore;
          GlobalConfiguration.Configuration.Formatters.Remove(GlobalConfiguration.Configuration.Formatters.XmlFormatter);          
      }
  }
  }

Comments

0

The complete Answer

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.