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"}
Trifactor
  • 59
  • 9
  • 1
    possible duplicate of [How to parse json in C#?](http://stackoverflow.com/questions/6620165/how-to-parse-json-in-c) – Victor Zakharov Sep 12 '14 at 12:49
  • Also look into [System.Runtime.Serialization.Json](http://msdn.microsoft.com/en-us/library/system.runtime.serialization.json(v=vs.110).aspx). – Victor Zakharov Sep 12 '14 at 12:49

4 Answers4

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.

L01NL
  • 1,753
  • 1
  • 13
  • 17
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
Ciarán
  • 3,017
  • 1
  • 16
  • 20
  • What if the sExampleJSON string value changes, i just added that as an example. – Trifactor Sep 12 '14 at 13:47
  • Well then you just deserialise it again. The MyDictionary will then contain the new deserialised JSON results. – Ciarán Sep 12 '14 at 21:05
  • could you provide an example please – Trifactor Sep 15 '14 at 11:51
  • I don't understand what you mean, an example of what exactly? – Ciarán Sep 16 '14 at 08:29
  • 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 – Trifactor Sep 16 '14 at 09:32
  • That's just an example. From your code above I suspect this is in your xml.SelectSimpleNode("result/redirecturl").InnerText, but I can't say for sure. Don't you know where your JSON is? because you don't make that clear in your code above. You've asked how to parse JSON and both these answers tell you how. If you've a wider coding question (and it certainly looks like you do) then you must expand your question to ask it. There's not a lot more I can add without getting into an extended discussion. Please be more specific. – Ciarán Sep 16 '14 at 12:39
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);          
      }
  }
  }
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
Trifactor
  • 59
  • 9