C colboy Member Licensed User Longtime User Dec 19, 2018 #1 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
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 Longtime User Dec 19, 2018 #2 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) Upvote 0
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)
C colboy Member Licensed User Longtime User Dec 19, 2018 #3 DonManfred said: 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) Click to expand... 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
DonManfred said: 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) Click to expand... 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 Dec 19, 2018 #4 colboy said: 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 Click to expand... You may want to encode all problematic strings with HEX. Encoding method can be found from ByteConverter lib. Upvote 0
colboy said: 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 Click to expand... You may want to encode all problematic strings with HEX. Encoding method can be found from ByteConverter lib.
C colboy Member Licensed User Longtime User Dec 19, 2018 #5 npsonic said: You may want to encode all problematic strings with HEX. Encoding method can be found from ByteConverter lib. Click to expand... I know other languages have a function to encode and decode for this very purpose. Would be useful on B4A. Upvote 0
npsonic said: You may want to encode all problematic strings with HEX. Encoding method can be found from ByteConverter lib. Click to expand... 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 Dec 19, 2018 #6 colboy said: I know other languages have a function to encode and decode for this very purpose. Would be useful on B4A. Click to expand... It isn't hard with B4A either. Code: Sub Encode (Text As String) As String Dim bc As ByteConverter Return bc.HexFromBytes(bc.StringToBytes(Text,"UTF-8")) End Sub Upvote 0
colboy said: I know other languages have a function to encode and decode for this very purpose. Would be useful on B4A. Click to expand... It isn't hard with B4A either. Code: Sub Encode (Text As String) As String Dim bc As ByteConverter Return bc.HexFromBytes(bc.StringToBytes(Text,"UTF-8")) End Sub
C colboy Member Licensed User Longtime User Dec 19, 2018 #7 npsonic said: It isn't hard with B4A either. Code: Sub Encode (Text As String) As String Dim bc As ByteConverter Return bc.HexFromBytes(bc.StringToBytes(Text,"UTF-8")) End Sub Click to expand... 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 said: It isn't hard with B4A either. Code: Sub Encode (Text As String) As String Dim bc As ByteConverter Return bc.HexFromBytes(bc.StringToBytes(Text,"UTF-8")) End Sub Click to expand... 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?
DonManfred Expert Licensed User Longtime User Dec 19, 2018 #8 colboy said: Surely this would need to be decoded correctly at the other end? Click to expand... yes Upvote 0
npsonic Active Member Licensed User Dec 19, 2018 #9 colboy said: 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? Click to expand... 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 Upvote 0
colboy said: 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? Click to expand... 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":"\'[\\"&"}
C colboy Member Licensed User Longtime User Dec 19, 2018 #10 npsonic said: 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":"\'[\\"&"} Click to expand... 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 said: 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":"\'[\\"&"} Click to expand... 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 Dec 19, 2018 #11 colboy said: 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. Click to expand... 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 said: 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. Click to expand... 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.
C colboy Member Licensed User Longtime User Dec 19, 2018 #12 npsonic said: 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. Click to expand... 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 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
npsonic said: 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. Click to expand... 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 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.