This question already has answers here:
json error Bad JSON escape sequence
(2 answers)
Closed 4 years ago.
I'm currently testing an application but it's throwing a Bad JSON Escape Sequence at me, however I don't see the problem...
I'm probably overlooking something so a fresh pair of eyes might be useful.
messageContents = "{\"command\":\"cue\",\"channel\":1,\"uid\":\"aesd-deaf\",\"type\":\"standard\",\"waitforexecute\":true,\"duration\":0,\"scene\":[{\"name\":\"Scene1\",\"fields\":[{\"Quad1\":\"F:\\TestFolder\\mill.jpg\"}]}]}";
And the error I'm getting is
{"Bad JSON escape sequence: \\T. Path 'scene[0].fields[0].Quad1', line 1, position 150."}
Can anyone spot the mistake?
Thanks,
Kenneth
Like the error says, the problem happens inside the array for the fields property:
[{\"Quad1\":\"F:\\TestFolder\\mill.jpg\"}]
Imagine what this looks like, once parsed:
[{"Quad1": "F:\TestFolder\mill.jpg"}]
The JSON parser doesn't recognize the escape sequence \T, which is not the same as \t.
To fix is simply double escape all the \ characters. So that section would look like:
\"fields\": [{\"Quad1\":\"F:\\\\TestFolder\\\\mill.jpg\"}]
Related
This question already has answers here:
Replace "\\" with "\" in a string in C#
(9 answers)
Escape Characters Aren't Working In Debugger (C#)
(2 answers)
How can I add \ symbol to the end of string in C#
(8 answers)
Why does .NET add an additional slash to the already existent slashes in a path?
(4 answers)
Escape sequence with "\" symbol
(4 answers)
Closed 2 years ago.
I'm struggling with escape characters in C#.
I'm saving a varchar field in a SQL Server database with a quote that needs to be escaped. So, an example could be something like that: (\"value\")
When my frontend application consume this value from API I get the value with a single escape character as i have in the DB.
The problem come up when consuming this API from C# code. For some reason this single character (\) is replaced for three of them (\\\).
After debugging the code I found that when I retrieve the data I get this:
debugging the value
When I click to check the value it seems OK:
Value into Text Visualizer
My question is why theese two extra backslashes are added and how can I get rid of them in order to get only one => (\").
Hope anyone can help me :)
Thanks!
You value should be correct. The debugger window escape the “/“, therefore it appears like escape(backslash)escape(quote).
(//)(/“)
This question already has an answer here:
C# Regex, Unrecognized escape sequence
(1 answer)
Closed 5 years ago.
I have the following regex which is working ok in https://regex101.com/ and so I want to use it in C# but I get the error Unrecognized escape sequence \_.
^https:\/\/github\.com\/([a-zA-Z\-0-9]+)\/([a-zA-Z\-\_]+)\/commit\/([a-fA-F0-9]{40}),(.*),([0-9]+),([0-9]+)$
in C# I do that:
string regex = #"^https:\/\/github\.com\/([a-zA-Z\-0-9]+)\/([a-zA-Z\-\_]+)\/commit\/([a-fA-F0-9]{40}),(.*),([0-9]+),([0-9]+)$";
and then want to do:
if (Regex.IsMatch(input, regex)) and so on. the error is here on that line.
I cant understand why I get the error in case I use #
You don't need to escape underscores in C# regular expressions. The error is coming from the regular expression parser.
You mentioned it is working OK on regex101.com. Maybe double check that it is dealing with underscores in the way you expect.
This question already has an answer here:
What is the correct way to use JSON.NET to parse stream of JSON objects?
(1 answer)
Closed 4 years ago.
I've got to process files that are full of JSON objects. These have simply been concatenated together with no separator thus making the whole file invalid JSON. What is the best way to split this up again? I need to ensure that I don't end up splitting in encoded strings and it needs to be fairly fast as the file can be quite big.
Example file:
{"property":"Data which may include}{"}{"property":"A second object"}
I've done a lot of parsing like this. There's so much JSON code out there that it's rarely necessary with JSON. But if you really need to pass this code yourself in C#, I see no way to approach this other than by manually parsing it character by character.
Special attention needs to be given to curly braces and colons. And, when parsing tokens you'll need to determine if it's quoted. If it's quoted, then you go until the closing quote (ignore any escaped quotes). If it's not quoted, then you go until you hit a non-symbol character.
You might find this task a little easier using my Text Parsing Helper Class class to handle some of the lower-level string handling of your parser.
This question already has answers here:
Parsing CSV files in C#, with header
(19 answers)
Closed 7 years ago.
i have a semicolon separates string, that contains values of every type. string and date values are in quotations.
Now i have an evil string, where an inner string contains s semicolon, that i need to remove (replace by nothing).
eg:
"Value1";0;"Value2";4711;"Evil; Value";"2015-09-03"
in C#:
string value = "\"Value1\";0;\"Value2\";4711;\"Evil; Value\";\"2015-09-03\""
So how to replace all semicolons, that are in quotations? can anybody help?
Regex is awful at handling delimited strings. It can do it, but it's not often as good of a choice as it first appears. This is one of several reasons why.
Instead, you should use a dedicated delimited string parser. There are (at least) three built into the .Net framework. The TextFieldParser type is one of those, and it will handle this correctly.
You should try this i.e to match only those semicolons which is not preceded by : :
(?<=[^"]);
Here is demo
This question already has answers here:
Dealing with forbidden characters in XML using C# .NET
(6 answers)
Closed 8 years ago.
We have an app that collects data electronically and by user input. The data is eventually turned into XML. We have had problems with invalid XML characters being in the inbound data when we turn it into XML either by serializing objects or using a .Net Transform. The process will thrown an exception like the below.
Exception: System.Xml.XmlException: '', hexadecimal value 0x10, is an invalid character. Line 5, position 74.
I don't know any other way to fix this other than scrubbing all the data either at input time or at the time the XML is created. The thought of running every string input or string property in an object through a cleaning function doesn't sound appealing. Is that the way this would need to be resolved.
Looking for confirmation or alternatives.
Thanks,
Kevin
There really isn't an elegant solution for this, but this response has some examples of whitelist cleansers.