Results 1 to 14 of 14

Thread: [RESOLVED] VISUAL STUDIO 2017 json double quotes problem working with api

  1. #1

    Thread Starter
    Enjoy the moment
    Join Date
    Feb 2011
    Location
    Barrio Del pilar madrid spain
    Posts
    5,204

    Resolved [RESOLVED] VISUAL STUDIO 2017 json double quotes problem working with api

    hello

    im using json that works with api
    i get an error when i try to send a double quotes inside the string

    Code:
    system.nullreferenceexception 'object reference not set to an instance of an object.'
    this is the code
    Code:
                JsonString = "{'full_name': """ + jFullName + """,'mobile': """ + jCelular + """,'remark':""" + JRemarks + """,
                'group_id': """ + jGroupID + """,'DateAdded':""" + JDate + """}"
    
                Dim jsonObject As JObject = JObject.Parse(JsonString)
    tnx for any help
    salsa

  2. #2
    Super Moderator dday9's Avatar
    Join Date
    Mar 2011
    Location
    South Louisiana
    Posts
    11,711

    Re: VISUAL STUDIO 2017 json double quotes problem working with api

    Instead of trying to construct the JObject from a String, try creating a new anonymous object and then use JObject.FromObject:
    Code:
    Dim anonymousObject = New With { Key .full_name = "...", .mobile = "867-5309", .remark = "Jenny?", .group_id = 1, .DateAdded = DateTime.Now }
    Dim jsonObject = JObject.FromObject(anonymousObject)
    Fiddle: https://dotnetfiddle.net/KDGDVC

    No more messing with escaping characters.
    "Code is like humor. When you have to explain it, it is bad." - Cory House
    VbLessons | Code Tags | Sword of Fury - Jameram

  3. #3

    Thread Starter
    Enjoy the moment
    Join Date
    Feb 2011
    Location
    Barrio Del pilar madrid spain
    Posts
    5,204

    Re: VISUAL STUDIO 2017 json double quotes problem working with api

    same error my friend
    this is the string im sending:
    Code:
    {{
      "full_name": "gfdאקראקראקר",
      "mobile": "",
      "remark": "ש\"ח",
      "group_id": "122256062T121021330K",
      "DateAdded": "2000-01-01 00:00:00"
    }}
    the whole code:
    Code:
                Dim anonymousObject = New With {Key .full_name = jFullName, .mobile = jCelular, .remark = JRemarks, .group_id = jGroupID, .DateAdded = JDate}
    
    
                Dim jsonObject = JObject.FromObject(anonymousObject)
                reqStream = WebReq.GetRequestStream()
                Dim jsonBytes() As Byte = Encoding.UTF8.GetBytes(JsonString)
    
                reqStream.Write(jsonBytes, 0, jsonBytes.Length)
                reqStream.Close()
                WebResp = WebReq.GetResponse
    main problem is this
    Code:
    ש"ח
    Last edited by salsa31; Dec 15th, 2020 at 05:06 PM.

  4. #4
    Super Moderator dday9's Avatar
    Join Date
    Mar 2011
    Location
    South Louisiana
    Posts
    11,711

    Re: VISUAL STUDIO 2017 json double quotes problem working with api

    You will still need to escape your double-quote in the JSON string by using back-slash.

    Update
    I see that you have a back-slash in your code example, but it is coming after the double-quote. You need for it to come before the double-quote.

    Update #2
    You do not need to actually add the back-slash in your example, just use two double-quotes:
    Code:
    Dim anonymousObject = New With { Key .full_name = "gfdאקראקראקר", .mobile = "", .remark = "ש""ח", .group_id = "122256062T121021330K", .DateAdded = "2000-01-01 00:00:00" }
    Dim jsonObject = JObject.FromObject(anonymousObject)
    Fiddle: https://dotnetfiddle.net/YbTniR
    Last edited by dday9; Dec 15th, 2020 at 05:32 PM.
    "Code is like humor. When you have to explain it, it is bad." - Cory House
    VbLessons | Code Tags | Sword of Fury - Jameram

  5. #5
    King of sapila
    Join Date
    Oct 2006
    Location
    Greece
    Posts
    6,597

    Re: VISUAL STUDIO 2017 json double quotes problem working with api

    You also might want to set culturinfo serialization as I'm inclined to do because I'm using Greek chars all the time.
    I'm not sure that this is you main issue but have also that in mind.
    Look at this for an idea:
    https://stackoverflow.com/questions/...on-serializing
    ἄνδρα μοι ἔννεπε, μοῦσα, πολύτροπον, ὃς μάλα πολλὰ
    πλάγχθη, ἐπεὶ Τροίης ἱερὸν πτολίεθρον ἔπερσεν·

  6. #6

    Thread Starter
    Enjoy the moment
    Join Date
    Feb 2011
    Location
    Barrio Del pilar madrid spain
    Posts
    5,204

    Re: VISUAL STUDIO 2017 json double quotes problem working with api

    Quote Originally Posted by dday9 View Post
    You will still need to escape your double-quote in the JSON string by using back-slash.

    Update
    I see that you have a back-slash in your code example, but it is coming after the double-quote. You need for it to come before the double-quote.

    Update #2
    You do not need to actually add the back-slash in your example, just use two double-quotes:
    Code:
    Dim anonymousObject = New With { Key .full_name = "gfdאקראקראקר", .mobile = "", .remark = "ש""ח", .group_id = "122256062T121021330K", .DateAdded = "2000-01-01 00:00:00" }
    Dim jsonObject = JObject.FromObject(anonymousObject)
    Fiddle: https://dotnetfiddle.net/YbTniR
    im not actualy sending the back slash
    im using the method you posted but still getting same error
    Code:
    system.nullreferenceexception 'object reference not set to an instance of an object.'

  7. #7
    Super Moderator dday9's Avatar
    Join Date
    Mar 2011
    Location
    South Louisiana
    Posts
    11,711

    Re: VISUAL STUDIO 2017 json double quotes problem working with api

    A null reference exception indicates that you are trying to access an object that has not been initialized.

    Setup a break point on that line and investigate which one of the following values are null: jFullName, jCelular, JRemarks, jGroupID, and JDate
    "Code is like humor. When you have to explain it, it is bad." - Cory House
    VbLessons | Code Tags | Sword of Fury - Jameram

  8. #8

    Thread Starter
    Enjoy the moment
    Join Date
    Feb 2011
    Location
    Barrio Del pilar madrid spain
    Posts
    5,204

    Re: VISUAL STUDIO 2017 json double quotes problem working with api

    Quote Originally Posted by dday9 View Post
    A null reference exception indicates that you are trying to access an object that has not been initialized.

    Setup a break point on that line and investigate which one of the following values are null: jFullName, jCelular, JRemarks, jGroupID, and JDate
    i did exactly as you said
    is my code ok?'
    you have maybe a better method?
    Code:
                Dim anonymousObject = New With {Key .full_name = jFullName, .mobile = jCelular, .remark = JRemarks, .group_id = jGroupID, .DateAdded = JDate}
    
    
                Dim jsonObject = JObject.FromObject(anonymousObject)
                reqStream = WebReq.GetRequestStream()
                Dim jsonBytes() As Byte = Encoding.UTF8.GetBytes(JsonString)
    
                reqStream.Write(jsonBytes, 0, jsonBytes.Length)
                reqStream.Close()
                WebResp = WebReq.GetResponse

  9. #9
    King of sapila
    Join Date
    Oct 2006
    Location
    Greece
    Posts
    6,597

    Re: VISUAL STUDIO 2017 json double quotes problem working with api

    Hi.
    Why are we going in that direction? IMO you better use a free JSON converter to .net classes to check if what you write compute.

    I've made a complete framework last year for work and IMO the best way to go is to use .net classes for JSON (an of course newtonsoft).
    What I did, because I would never have gotten to write down correctly Json, as I would have been missing out a comma or a dot or something, was to write the Classes in .net and the using an online converter get the JSON schema. Mind you some of the JSON I was getting was from another company provided the JSON string so in that occasion I went the opposite direction.
    I think it is OK to have a basic knowledge on how JSON is formed so you can see what comes out but to go and write JSON yourself is a nightmare. Even here you got issues with a small JSON string.

    So for an initial try I would suggest, first to parse the JSON through an online JSON to .net converter. That will show you errors and if all is OK take out the .net classes created and use them.
    Again that was what I did and it was very helpful but if you think you can handle Json as is then by all mean go for it...But imaging if you have 1000 lines of Json (like I had) and you are missing a colon somewhere!!
    ἄνδρα μοι ἔννεπε, μοῦσα, πολύτροπον, ὃς μάλα πολλὰ
    πλάγχθη, ἐπεὶ Τροίης ἱερὸν πτολίεθρον ἔπερσεν·

  10. #10
    Super Moderator dday9's Avatar
    Join Date
    Mar 2011
    Location
    South Louisiana
    Posts
    11,711

    Re: VISUAL STUDIO 2017 json double quotes problem working with api

    Quote Originally Posted by salsa31 View Post
    i did exactly as you said
    is my code ok?'
    you have maybe a better method?
    Your code looks fine, but if you are getting a null reference exception then you need to set up a break point on the line that is throwing the exception and inspect which reference is null.

    Quote Originally Posted by sapator View Post
    Why are we going in that direction? IMO you better use a free JSON converter to .net classes to check if what you write compute.
    I recently discovered that Visual Studio has a built-in option that allows you to convert JSON to POCO classes: Edit > Paste as Special > Paste JSON as Classes. No need for an online converter.
    "Code is like humor. When you have to explain it, it is bad." - Cory House
    VbLessons | Code Tags | Sword of Fury - Jameram

  11. #11

    Thread Starter
    Enjoy the moment
    Join Date
    Feb 2011
    Location
    Barrio Del pilar madrid spain
    Posts
    5,204

    Re: VISUAL STUDIO 2017 json double quotes problem working with api

    Quote Originally Posted by sapator View Post
    Hi.
    Why are we going in that direction? IMO you better use a free JSON converter to .net classes to check if what you write compute.

    I've made a complete framework last year for work and IMO the best way to go is to use .net classes for JSON (an of course newtonsoft).
    What I did, because I would never have gotten to write down correctly Json, as I would have been missing out a comma or a dot or something, was to write the Classes in .net and the using an online converter get the JSON schema. Mind you some of the JSON I was getting was from another company provided the JSON string so in that occasion I went the opposite direction.
    I think it is OK to have a basic knowledge on how JSON is formed so you can see what comes out but to go and write JSON yourself is a nightmare. Even here you got issues with a small JSON string.

    So for an initial try I would suggest, first to parse the JSON through an online JSON to .net converter. That will show you errors and if all is OK take out the .net classes created and use them.
    Again that was what I did and it was very helpful but if you think you can handle Json as is then by all mean go for it...But imaging if you have 1000 lines of Json (like I had) and you are missing a colon somewhere!!
    this is the only method that i use(for now)
    Code:
    'remark':""" + Replace(JRemarks, """", "") + """

  12. #12
    King of sapila
    Join Date
    Oct 2006
    Location
    Greece
    Posts
    6,597

    Re: VISUAL STUDIO 2017 json double quotes problem working with api

    @dday9 I wouldn't trust Visual Studio Conversions even if they paid me to do so.
    But OK, OK, lets go with that.
    So I went on to try that strange-goofy looking Json string from one of your examples:
    Code:
    {{
      "full_name": "gfdאקראקראקר",
      "mobile": "",
      "remark": "ש\"ח",
      "group_id": "122256062T121021330K",
      "DateAdded": "2000-01-01 00:00:00"
    }}
    First I removed the extra {{ and }} from the beginning and end to make it proper.

    Now using Json.net I was able to serialize and deserialize the string like so:


    Code:
    Imports Newtonsoft.Json
    
    Public Class Form1
        Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    
            Dim ClsExample As New Example With {.DateAdded = "2000-01-01 00:00:00", .full_name = "gfdאקראקראקר", .mobile = ":", .remark = "ש\""ח""", .group_id = "122256062T121021330K"}
    
            'serialize:
            Dim jsonstring As String = JsonConvert.SerializeObject(ClsExample)
            ' add something new on the string
            jsonstring = "{""full_name"":""gfdאקראקראקר"",""mobile"":""NEWMOBILE"",""remark"":""ש\""ח"",""group_id"":""122256062T121021330K"",""DateAdded"":""2000-01-01T00:00:00""}"
            
           'Deserialize       
            jsonstring = jsonstring.ToString(System.Globalization.CultureInfo.CreateSpecificCulture("el-GR"))  ' don't use this if you don't want to
            ClsExample = JsonConvert.DeserializeObject(Of Example)(jsonstring)
    
        End Sub
    End Class
    
    Public Class Example
        Public Property full_name As String
        Public Property mobile As String
        Public Property remark As String
        Public Property group_id As String
        Public Property DateAdded As DateTime
    End Class
    Last edited by sapator; Dec 16th, 2020 at 12:22 PM.
    ἄνδρα μοι ἔννεπε, μοῦσα, πολύτροπον, ὃς μάλα πολλὰ
    πλάγχθη, ἐπεὶ Τροίης ἱερὸν πτολίεθρον ἔπερσεν·

  13. #13

    Thread Starter
    Enjoy the moment
    Join Date
    Feb 2011
    Location
    Barrio Del pilar madrid spain
    Posts
    5,204

    Re: VISUAL STUDIO 2017 json double quotes problem working with api

    what is this?
    Code:
     Dim ClsExample As New Example
    what is the real variable?

  14. #14
    Super Moderator dday9's Avatar
    Join Date
    Mar 2011
    Location
    South Louisiana
    Posts
    11,711

    Re: VISUAL STUDIO 2017 json double quotes problem working with api

    You are missing the point, sapator's example does the same exact thing as my example. The only difference is that he uses a strongly typed object to represent his JSON where as I use an anonymous object.

    The real issue at hand is that you are trying to get a value from a null reference. To solve that, you will need to setup a break point and figure out which reference is null.
    "Code is like humor. When you have to explain it, it is bad." - Cory House
    VbLessons | Code Tags | Sword of Fury - Jameram

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width