I am writing an app for SmartThings (www.smartthings.com) in their own IDE. I have an input field here that is supposed to be text input. I ask for a departure address:
section("Departing From:"){
input "departFrom", "text", title: "Address?"
}
when putting in the value of Monterey, CA the value magically gets changed to a JSON Array with the values of [Monterey, CA]
I want to pass this value to an httpGET statement but I need to URLencode it first to omit spaces, etc.I have tried URLencoder with no success due to the JSON array.
I have tried join(",") with no luck as it adds double quotes to the value.
How can I get a clean Monterey%2C%20CA URL encoded value from this variable?
** bear in mind someone could input any combination of numbers, spaces, and commas into this input as an address. The mapquest API I am sending it to can handle all these things as long as they dont have special characters and spaces are URL encoded.
Maybe try:
def l = ['Monterey', 'CA']
assert URLEncoder.encode(l.join(', ')).replaceAll('\\+','%20') == 'Monterey%2C%20CA'
When it comes to replacing + sign, please see here
There are different types of URL encoding, but in this case there are two: One that converts spaces to %20 and one that converts spaces to +.
For the first, you'd use UriUtils:
def yourEncodedString = UriUtils.encodeUri(yourString.toString(), "UTF-8")
For the second, you'd use UrlEncoder:
def yourEncodedString = URLEncoder.encode(yourString.toString(), "UTF-8")
Alternatively (I think) you can use URLEncoder with UTF-16 to get what you want.
I've never had a fun time with UriUtils, so hopefully UrlEncoder will work for you.
Related
TL;DR: I have a String variable in java (not a json string, just a string) and I want to encode it to json, how? Please read the rest to be sure to have understood the question.
// This is javascript, I use it for this example because I know it better than java
// All of the following strings are valid json strings
const validJsonStrings = [
"{\"key\": \"value\"}",
"true",
"[]",
"\"long_complex_string\""
];
// Each of them can be parsed/decoded as you can easily test with:
console.log(validJsonStrings.map(s => JSON.parse(s)));
I'm interested in the 4th one, that is "\"long_complex_string\"" and that decodes into "long_complex_string".
Now, back to java, Let's say I have this variable:
String myString = "long_complex_string";
This is not json, it's just a string, it could be very long and could contain many special characters including double quotes. I want to encode this string to json, I want it to be exactly like the 4th string of the previous javascript example. I've seen many examples where objects or arrays are serialized to json, but I'm having trouble finding one that accepts a single string as input.
jsonObj.get("key") will retrieve only the stored value.
Please notice that \ is a special escape character for Java Strings. To get the desired String, your original has to look like this, escaping both \ and the ".
String original = "my ve\\\"ry c\\tomplex ✪string èè òòò ììì aaa";
Basically, when JSON takes in a string it will convert things like ' or & to their Unicode value. I'm trying to store the JSON value as it goes out, and when it comes back in later, compare it with the JSON value. However, when I send out something like "Let's Party" it comes back "Let\u0027s Party"
So basically, I'm looking to convert all JSON Unicode to their specific Unicode values before storing it or sending it out.
I'm looking to convert all JSON Unicode to their specific Unicode values
I doubt you want to convert all characters to \u-escapes. In that case Let's party would become \u004c\u0065\u0074\u0027\u0073\u0020\u0050\u0061\u0072\u0074\u0079.
There is nothing special about the apostrophe or ampersand that means it has to be encoded in JSON, although some encoders do so anyway (it can have advantages for using JSON inside another wrapper context where those characters are sepecial).
It looks like you want to match the exact output that another encoder produces. To do that you'd have to determine the full set of characters that that encoder decides to escape, and either alter or configure your own JSON encoder to match that. For some characters that could be as simple as doing a string replace: for example as ' may only legitimately appear in JSON as part of a string literal it would be safe to replace with \u0027 after the encoding. This is ugly and fragile though.
It is generally a bad idea to rely on the exact encoding choices of a JSON serialiser. The JSON values {"a": "'", "b": 0.0} and {"b": 0, a: "\u0027"} represent the same data and should generally be treated as equal. For comparison purposes it is usually better to parse the JSON and check the content piece-by-piece, or re-serialise using your own encoder and compare that output (assuming your JSON encoder is deterministic).
I am using java's URLEncoder to take a user provided string and create a string that is safe to use for filenames. What I'm wondering is it possible for two different strings to be encoded to the same value.
For example, if one string is "ABC%20D" but since % is used as a character to replace special characters is it possible that something like "ABC D" and "ABC%20D" both end up as the same encoded value? Or will the encoder always replace characters like % with something else?
It seems to encode escape characters using your example input:
String result = URLEncoder.encode("ABC%20D", "UTF-8");
System.out.println(result); //prints ABC%2520D
I'm writing a lucene server. I want to receive the post query like :
http://www.site.com/search?+title:google +type:website
but the post argument "+title:google +type:website" is encoded like this: "+title:google%20+type:website"
so I use URLDecoder.decode(argument,"UTF-8") to get the original input, but I get the wrong result:
" title:goole type:website", because the URLDecoder convert the plus sign "+" into a space character " ". What can I do to get the decode argument without converting plus sign?
You can try this:
"+title:google%20+type:website".replaceAll("\\+", "%2b")
It will replace all plus signs and after that you use the decoder, which will convert back the plus sign
Actually All spaces will be replaced by %20 , so u can replace all the %20 to space once you get the URL string.
I have a URL that looks like this:
Liberty%21%20ft.%20Whiskey%20Pete%20-%20Thunderfist%20%28Original%20Mix%29.mp3
I'm trying to extract just the words from it. Right now, I'm using string.replace("%21", "!") for each and every %20, %29, etc. because each segment represent different characters or spaces. Is there a way to just covert those symbols and numbers to what they actually mean?
Thanks.
Those symbols are URLEncoded representations of characters that can't legally exist in a URL. (%20 = a single space, etc)
You need to UrlDecode those strings:
http://icfun.blogspot.com/2009/08/java-urlencode-and-urldecode-options.html
Official documentation here:
http://download.oracle.com/javase/6/docs/api/java/net/URLDecoder.html
It seems the input string is written using the URL encoding. Instead of writing all possible replacements manually (you can hardly cover all possibilities), you can use URLDecoder class in Java.
String input = "Liberty%21%20ft.%20Whiskey%20Pete...";
String decoded = URLDecoder.decode(input, "UTF-8");