Hannington Mambo
Hannington Mambo

Reputation: 1090

How do you convert JSON in VB.net for consumption in PHP

I was handed a php file that submits a JSON String to an API. The application I am working with to do the same, however, runs on VB.net.

I have used a Dictionary object to reconstruct the JSON object but I am getting some subtle differences in final output. Here is my code:

        Dim data As New Dictionary(Of String, Object)

        Dim AuthDetails As New Dictionary(Of String, Object)
        AuthDetails.Add("UserID", 18)
        AuthDetails.Add("Token", "One")
        AuthDetails.Add("Timestamp", Date.Now().ToString("yyyyMMddhhmmss")

        data.Add("AuthDetails", AuthDetails)

        Dim JSONByte As Byte() = Encoding.Default.GetBytes(JsonConvert.SerializeObject(data, Formatting.Indented))
        Dim JSONString As String = Encoding.ASCII.GetString(JSONByte).Replace(vbCrLf, "").Replace("""", """""")

There are two issues I am facing:

  1. I have added the 2 replace methods on the JSONString because in VB.net the JSONString would have line breaks (nice formatting) and one " less than what, apparently, PHP gives out with a comparable construction. I think my code above is a fix and not a convention. But it works fine so far unless I figure out how to avoid that fix!

  2. The main issue is that this is the PHP JSON String expected, which has subtle differences from what I am able to get in VB.net as show below:

PHP:

{""AuthDetails"":[{""UserID"":""18"",""Token"":""One"",""Timestamp"":""20190424132431""}]}

VB.net:

{ ""AuthDetails"": { ""UserID"": ""18"", ""Token"": ""One"", ""Timestamp"": ""20190424053345"" }}

So if you look carefully, the PHP part above has some square brackets, which I am missing in my VB.net part. I understand it is PHP way of defined arrays, but I can't figure out how to do it in VB.net with the Dictionary object that I have used.

Please help me figure out the additional square bracket to my JSON String.

Revision:

I am able to get to this point:

{ ""AuthDetails"": [ { ""Key"": ""UserID"", ""Value"": ""1581"" }, { ""Key"": ""Token"", ""Value"": ""4f7bbcf97b4313f8353fa06c5e3a8fb6"" }, { ""Key"": ""Timestamp"", ""Value"": ""20190425052154"" } ]}

when I convert the Dictionary object to Array like this:

        data.Add("AuthDetails", AuthDetails.toArray())

But that now introduces Key names and value names into the string..., which I don't need!

Thank you!

Upvotes: 0

Views: 235

Answers (1)

Hannington Mambo
Hannington Mambo

Reputation: 1090

Thank you for the comments! It added up to what I have now figured out as my solution as below:

        Dim auth(0) As Object
        auth(0) = AuthDetails
        data.Add("AuthDetails", auth)

So, instead of adding AuthDetails to the data object, I added it to a defined array first (Tim Morton!).

Revision: (Jimi) I changed the Formatting to Formatting.None and it removed the line breaks and also removed the need to add additional double quotes!

[Deleted the earlier comment about PHP]

Upvotes: 1

Related Questions