Android Question Encoding Text when Manually Building JSON

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
 

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?

B4X:
    Dim jgen As JSONGenerator
    jgen.Initialize(CreateMap("customerId": 123))
    Log(jgen.ToString)
 

colboy

Member
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?

B4X:
    Dim jgen As JSONGenerator
    jgen.Initialize(CreateMap("customerId": 123))
    Log(jgen.ToString)
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.
 

npsonic

Active 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
You may want to encode all problematic strings with HEX. Encoding method can be found from ByteConverter lib.
 

colboy

Member
Licensed 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.
 

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
 

colboy

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

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:

colboy

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 "
B4X:
{"key1":"\'[\\"&"}
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.
 

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.
 

colboy

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.
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.
 
Top