Android Question Encoding Text when Manually Building JSON

Discussion in 'Android Questions' started by colboy, Dec 19, 2018.

  1. colboy

    colboy Member Licensed User

    I'm manually building in code, some JSON files, but have a hit a problem where I need to ensure that characters like " ' [ & etc are encoded, as if I don't, the recipient can't parse the resulting file. Is there a function somewhere that will allow me to convert a text string to a JSON friendly string?

    Thanks, Colin
     
  2. DonManfred

    DonManfred Expert Licensed User

    Usually i do use a map to build the structure and then pass the map to the jsongenerator to build a json out of it.

    Does this not work for you?

    Code:
    Dim jgen As JSONGenerator
        jgen.Initialize(CreateMap(
    "customerId"123))
        
    Log(jgen.ToString)
     
  3. colboy

    colboy Member Licensed User

    I'm manually building using the StringBuilder. Ideally I'll refactor to use the JSONGenerator, but short term, I just want to encode the text values, to ensure the JSON file can be parsed correctly at the other end.
     
  4. npsonic

    npsonic Active Member Licensed User

    You may want to encode all problematic strings with HEX. Encoding method can be found from ByteConverter lib.
     
  5. colboy

    colboy Member Licensed User

    I know other languages have a function to encode and decode for this very purpose. Would be useful on B4A.
     
  6. npsonic

    npsonic Active Member Licensed User

    It isn't hard with B4A either.

    Code:
    Sub Encode (Text As StringAs String
        
    Dim bc As ByteConverter
        
    Return bc.HexFromBytes(bc.StringToBytes(Text,"UTF-8"))
    End Sub
     
  7. colboy

    colboy Member Licensed User

    But I don't think I can use hex, as I'm posting to a site, I have no control over. Surely this would need to be decoded correctly at the other end?
     
  8. DonManfred

    DonManfred Expert Licensed User

    yes
     
  9. npsonic

    npsonic Active Member Licensed User

    If you build your own json string like you said what's the problem you are facing?
    Characters " ' [ & should work just fine when json string is correctly created.

    Just use \ with some characters like ' and \\ with the "
    Code:
    {"key1":"\'[\\"&"}
     
    Last edited: Dec 19, 2018
  10. colboy

    colboy Member Licensed User

    I'm just going to create a function based on DonManfred's solution. I'll then strip out the desired result. Just thought there was an easier way.
     
  11. npsonic

    npsonic Active Member Licensed User

    If you find method you need from B4A libs like in this case lib JSON, it's very good chance that it is already made easy as it can be.
    Like I pointed out my last message you need to take care of correct syntax with some special characters when dealing with json. JSON lib already takes care of this all automatically.
     
  12. colboy

    colboy Member Licensed User

    Yes ideally I need to use the JSONGenerator, but I just want a quick fix now. This is what I ended up with.

    Code:
    Sub StringToJSON(pText As StringAs String
        
    Dim lResult As String=""
        
    Dim jgen As JSONGenerator
        jgen.Initialize(CreateMap(
    "Remove": pText))
        lResult=Mid(jgen.ToString,
    12,jgen.ToString.Length-13)
        
    Return lResult
    End Sub
    Mid is just a wrapper function around SubString2.

    Thanks all.
     
Loading...
  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice