I try to replace the group 2 character \" in my JSON string but not sure why it didnt work.
Here is what i have
raw_json_text_edited = string.gsub( raw_json_text, [[\"]], [[]])
I also use
raw_json_text_edited = string.gsub( raw_json_text, '\"', '')
Both way doesnt work since it somehow only remove the " part of string rather than both \"
A bit background about the problem if you have any other suggestion.
I have a long JSON string with
..."phone":"{\"p1\":\"13068527218\",\"p2\":\"13062225064\",\"p3\":\"14445554444\"}","email":"{\"e1\":\"test#test.com\",\"e2\":\"test2#test.com\",\"e3\":\"sss#ww.com\"}",....
If I remove \" part in this JSON string, everything work.
UPDATE:
WORKING CODE ONLY FOR REPLACING:
raw_json_text_edited = string.gsub( raw_json_text, [[\\"]], [[]])
However, I just discover a problem why my JSON didnt work is that for JSON string,
..."phone":"{\"p1\":\"13068527218\",\"p2\":\"13062225064\",\"p3\":\"14445554444\"}","email":"{\"e1\":\"test#test.com\",\"e2\":\"test2#test.com\",\"e3\":\"sss#ww.com\"}"
If i only replace \" then it would be a sub string in "{}", eg: "{"p1":"1213131"}". This is wrong JSON format since table cannot be string.
This problem is something else so I will put this as solved
raw_json_text_edited = string.gsub( raw_json_text, [[\"]], [[]])
Related
I have a submit form where there are multiple textfields.
Whenever user enters text like "Hi, my name is "xyz"", the service does not accept this JSON due to double quotes in my string.
Please suggest ways to escape this character.
I have tried using encode and decode JSON, replaceOccurrencesOf methods, but none work.
replaceOccurrencesOf()
The below code snippet with replace "(double quote) in a string by \". This will help to replace "(double quote) by any string or character in a given string.
Swift 5 or above
let replacedString = stringToBeModified.replacingOccurrences(of: "\"", with: #"\""#)
Instead of putting the name (i.e., "XYZ" if you getting xyz from textfield ) why not to place (textField.text!) it will not put extra " "
In my get request, I am sending the parameter like this localhost:3000/home?q="item1 item2"
But in the server params if I observe the q. It changes like this.
"\"item1 item2\""
However I don't want the extra \" in the starting and ending of string, is there any thing I am doing wrong while sending the request?
The scenario is the same even when q="item1+item2"
First of all, there are no \" characters in the string, it is just a way how Rails quotes " strings in logs. In reality, the string has a value of "item1 item2" and the " characters are part of it.
Second, if you don't want the " to be there, you can either just not send it - see #Sudipta Mondal:
localhost:3000/home?q=item1%20item2
or if you need to send it, then remove it afterwards in the controller:
params[:q].to_s[1..-2]
which will remove first and last character, or:
params[:q].gsub /"/, ""
which will remove all occurences of the ".
The value of q parameter in the URL should be: localhost:3000/home?q=item1+item2
I have the followigurl localhost.dev?q=dyYJDXWoTKjj9Za6Enzg4lB+NHJsrZQehfY1dqbU1fc= and extract the query string as follows:
NameValueCollection query = HttpUtility.ParseQueryString(actionContext.Request.RequestUri.Query);
string str1 = query[0];
If i call query.ToString() it shows the correct characters query string. However, when I access the value from the NameValueCollection 'query[0]' the "+" is replaced by a empty " " i.e. dyYJDXWoTKjj9Za6Enzg4lB NHJsrZQehfY1dqbU1fc=
I've tried specifing different encoding and using the Get method from the namevaluecollection. I've also tried spliting the string, but the "+" is being removed each time. Has anyone got any ideas? Many thanks
You can't use this chars in the url variables, you need use URLEncode and URLDecode of HttpUtility class to convert this into a valid url.
I hope this help you.
I'm trying to pass more than one regex parameter for parts of a string that needs to be replaced. Here's the string:
str = "stands in hall "Let's go get to first period everyone" Students continue moving to seats."
Here is the expected string:
str = "stands in hall "Let's go get to first period everyone" Students continue moving to seats."
This is what I tried:
str.gsub(/'|"/, "'" => "\'", """ => "\"")
This is what I got:
"stands in hall \"Let's go get to first period everyone\" Students continue moving to seats."
How do I get the quotes in while sending in two regex parameters using gsub?
This is an HTML unescaping problem.
require 'cgi'
CGI.unescape_html(str)
This gives you the correct answer.
From my comments on this question:
Your updated version is correct. The only reason the slashes are in your final line of code is that it's an escape sequence so that you don't mistakenly think the first slash is used to terminate the string. Try assigning your output and printing it:
str1 = str.gsub(/'|"/, "'" => "\'", """ => "\"")
puts str1
and you'll see that the slashes are gone when str1 is printed using puts.
The difference is that autoevaluating variables within irb (which is what I assume you're doing to execute this sample code) automatically calls the inspect method, which for string variables shows the string in its entirety.
Because I did not understand unescaping characters I found an alternative solution that might be the "rails-way"
Can you use <%= raw 'some_html' %>
My final solution ended up being this instead of messy regex and requiring CGI
<%= raw evidence_score.description %>
Unescaping HTML string in Rails
So just in case someone stumbles across the same issue, I have a string:
a = " I am a string with whitespace at the start and end "
Interestingly when trying to a.strip, my string didn't change to:
a = "I am a string with whitespace at the start and end"
The issue here was, that I had or some other kind of space which prevented strip from doing what it does best.
My solution, first replace all my spaces with spaces:
a.gsub("\u00A0", " ") (I tried a.gsub(" ", " ") at first, but no luck)
tada!
Now I got my expected a.strip result :)
(Maybe there is a clearer way to do this, if so let me know)