skip to Main Content

I have been trying to import a JSON string into VB.NET just to experiment with an http client I am working with, this is a throw away exercise but it will allow me to confirm some puzzling functionality I am experiencing. The JSON is:

 {
 "BoundaryType": "Outside",
 "StartTime": "2022-12-07T00:00:00",
 "EndTime": "2022-12-08T00:00:00",
 "TagFilter": "atagname",
 "Delimiter":"",
 "ServerName": "aservername"
 }

Trying to replicate this in VB.NET as a plain string is proving to be a RPITA. No matter what I do VB.NET does not seem to want to allow me to concatenate a single double quote. I have even resorted to altering the JSON to supplanting the double quotes with ‘!’ as a placeholder and then replacing with double quotes as in:

 {
 !BoundaryType!: !Outside!,
 !StartTime!: !2022-12-07T00:00:00!,
 !EndTime!: !2022-12-08T00:00:00!,
 !TagFilter!: !atagname!,
 !Delimiter!:!!,
 !ServerName!: !aservername!
 }

 content = content.Replace("!", Chr(34))

But this still results in:

 {
 ""BoundaryType"": ""Outside"",
 ""StartTime"": ""2022-12-07T00:00:00"",
 ""EndTime"": ""2022-12-08T00:00:00"",
 ""TagFilter"": ""atagname"",
 ""Delimiter"":"""",
 ""ServerName"": ""aservername""
 }

Curiously enough if I view this in the Text Visualizer in Visual Studio it displays what I want:

{
"BoundaryType": "Outside",
"StartTime": "2022-12-07T00:00:00",
"EndTime": "2022-12-08T00:00:00",
"TagFilter": "atagname",
"Delimiter":"",
"ServerName": "aservername"
}

but passed as a variable into a function I get the repeated double quotes.

3

Answers


  1. Chosen as BEST ANSWER

    I think on reflection probably avoiding the hassle with quotes entirely (JimC's response) is the way to go. Thanks all for your feedback.


  2. Use "" to insert double quotes in a string:

    Dim json As String = "{" & vbCrLf & """BoundaryType"":  ""Outside""" & vbCrLf & "}"
    MsgBox(json)
    
    Login or Signup to reply.
  3. If you want to avoid the problem entirely, you can use tags to define the string:

    Code:

        Dim JsonData = <json> 
            {
                "BoundaryType": "Outside",
                "StartTime": "2022-12-07T00:00:00",
                "EndTime": "2022-12-08T00:00:00",
                "TagFilter": "atagname",
                "Delimiter":"",
                "ServerName": "aservername"
            }
        </json>
    
        Dim results = Newtonsoft.Json.JsonConvert.DeserializeObject(Of JsonExample)(JsonData)
    
        MsgBox(results.BoundaryType)
    

    Class:

    Public Class JsonExample
        Public Property BoundaryType As String
        Public Property StartTime As DateTime
        Public Property EndTime As DateTime
        Public Property TagFilter As String
        Public Property Delimiter As String
        Public Property ServerName As String
    End Class
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search