Android Question Encoding Text when Manually Building JSON

colboy

Member
Licensed User
Longtime 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
 

colboy

Member
Licensed User
Longtime 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.
 
Upvote 0

npsonic

Active Member
Licensed User
You may want to encode all problematic strings with HEX. Encoding method can be found from ByteConverter lib.
 
Upvote 0

colboy

Member
Licensed User
Longtime User
You may want to encode all problematic strings with HEX. Encoding method can be found from ByteConverter lib.
I know other languages have a function to encode and decode for this very purpose. Would be useful on B4A.
 
Upvote 0

npsonic

Active Member
Licensed User
I know other languages have a function to encode and decode for this very purpose. Would be useful on B4A.
It isn't hard with B4A either.

B4X:
Sub Encode (Text As String) As String
    Dim bc As ByteConverter
    Return bc.HexFromBytes(bc.StringToBytes(Text,"UTF-8"))
End Sub
 
Upvote 0

colboy

Member
Licensed User
Longtime 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?
 
Upvote 0

npsonic

Active 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?
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 "
B4X:
{"key1":"\'[\\"&"}
 
Last edited:
Upvote 0

colboy

Member
Licensed User
Longtime 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.
 
Upvote 0

npsonic

Active 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.
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.
 
Upvote 0

colboy

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

B4X:
Sub StringToJSON(pText As String) As 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.
 
Upvote 0
Cookies are required to use this site. You must accept them to continue using the site. Learn more…