I have a console application and writing a string content (a long comment you can say) through POST method to LiquidPlanner comment. I have a long string having some html tags and maintains a format like following.
When i am serializing through JsonConvert.SerializeObject then formatting of that comment is breaking and writing on other side as messy text as following.
i tried following link but was not helpful.
Link for serialization
how can serialize exact same ? is there any way please let me know asap, thank you in advance.
Cause of Error:
I solved the issue, actually what happening in original string you can see the new line so when this string pass into the string variable then .Net environment put \r\n on every new line and when i serialize from the newton.json library it put one more slash in \r and \n (\\r\\n) so when i POST the string in LiquidPlanner, LiquidPlanner fail to interpretate (\\r\\n) and write as text non formatted way.
Solution of Error:
what i did, i used regex and replaced the \\r\\n with tag from the serialized string and posted, and it worked for me.
string.Join("<br/>", System.Text.RegularExpressions.Regex.Split(createCommentJson, #"(?:\\r\\n|\\n|\\r)"));
and it posted in formatted way.
:)
Related
How can I have Newtonsoft.Json read the value of a path without converting or otherwise meddling with values?
This code
jsonObject.SelectToken("path.to.nested.value").ToString()
Returns this string
03/07/2019 00:02:12
From this string in the JSON document
2019-07-03T00:02:12.1542739Z
It's lost its original formatting, ISO 8601 in this case.
I would like all values to come through as strings, verbatim. I'm writing code to reshape JSON into other formats and I don't want to effect the values as they pass through my .NET code.
What do I need to change? I am not wedded to Newtonsoft.Json btw.
I got it, I think.
jsonObject.SelectToken(path).ToString(Newtonsoft.Json.Formatting.None);
The other options were to supply nothing or this.
Newtonsoft.Json.Formatting.Indented
Which is strange logic in this API as you'd think None means not indented but it means not ... I don't know. Hang on....
Okay so None or Indented returns
"2019-07-03T00:02:12.1542739Z"
(including quotes) but using the overload taking no parameters returns
03/07/2019 00:02:12
That's an odd API design ¯\_(ツ)_/¯
Here's a screenshot which shows really simple repro code.
I have a string where I need to use as the body of a JSON object. I know its possible that the data could have quotes in it, so I parse through to add an escape character to those instance of quotes.. like so:
string NewComment = comment.Replace("\"", "\\\"");
However, somehow on some edgecases, a quote still makes it through. I don't know if this is something with UTF or some other issue, But I am trying to find a function that would safely create a json compatible string, I figured there has to be something like this out there, or a regex way of doing so.
Basically a TLDR is how to create a json syntax safe string from a c# string
The simple answer is don't do it this way. What if you have escaped quotes in your string? "Hello \"World\"" would become invalid with such a simple approach: "Hello \\"World\\"". JSON.Net or Newtonsoft are going to save you so many headaches in the long run.
Here is my prob, I wanted String.Format() function should take 4 objects and format string. But it throws "Input string not in a correct format error".
Here is my code,
string jsonData = string.Format("{{\"sectionTitle\":\"{0}\",\"strPushMsg\":\"{1}\",\"Language\":\"{2}\",}\",\"articleid\":\"{3}\"}}", urlsectiontitle, formatHeadline, Language, articleid);
\"{2}\",}\"
Looks like you need to escape that closing brace by doubling it:
string.Format("{{\"sectionTitle\":\"{0}\",\"strPushMsg\":\"{1}\",\"Language\":\"{2}\",}}\",\"articleid\":\"{3}\"}}", urlsectiontitle, formatHeadline, Language, articleid);
It appears you are creating JSON. This can use single quotes (which would avoid all the escaping), but even better use a tool like JSON.Net designed to create JSON. While your (partial) structure here is quite small (the unmatched } shows this is only partial), and the JSON gets bigger it is much easier to use a tool to get it right.
I've got a bit of a weird one here (well I think that it's weird!).
I'm using a web service to return a string and I'm trying to put quotes inside the string so say for instance I want to return the string Craig says, "hello" I would normally do something like:
zString = "Craig says, \"Hello\"";
but what I'm actually getting back from the webservice is the string including the \'s. So I actually get back:
Craig says, \"Hello\"
This is driving me loopy! Any ideas anyone? Could this declaration at the start be causing the problem?
[WebMethod]
[ScriptMethod(ResponseFormat = ResponseFormat.Json)]
Thanks,
Craig
That a json string inside an json output, so you need to parse it twice.
RFC 4627:
All Unicode characters may be placed within the
quotation marks except for the characters that must be escaped:
quotation mark, reverse solidus, and the control characters (U+0000
through U+001F).
This simply means that nothing is wrong. The character is being escaped according to the json standard.
Yes. Being in JSON format, it also escapes the " characters by using \ when returned.
It's the same as:
{
"zString": "Craig says, \"Hello\""
}
Deserialize JSON into C# dynamic object?
Following above question, I copy the dynamicJsonDeserilization and trying to use that in my application.
then I try to access the object as
var Data = json.deserilization(jsonstring);
Now, my string is
{"0":{"Name":"C:\\","Type":"Partition","Path":"C:\\"},"1":{"Name":"D:\\","Type":"Partition","Path":"D:\\"},"2":{"Name":"E:\\","Type":"Partition","Path":"E:\\"}}
i.e. I just have an Array on my server which I convert to JSON string and send.
As per code from best answer I should be able to access it as Data.0 but it give "End of Expression expected", Also Data[0] is giving same error. I am not sure how can I use it ? Any help is appreciated. Thanks.
Now, my string is
{"0":{"Name":"C:\","Type":"Partition","Path":"C:\"},"1":{"Name":"D:\","Type":"Partition","Path":"D:\"},"2":{"Name":"E:\","Type":"Partition","Path":"E:\"}}
Your string is indeed not valid JSON due to escaped quotes.
Those C:\ are breaking the parser. You should generate it like this, sending three backslahes:
{"0":{"Name":"C:\\\","Type":"Partition","Path":"C:\\\"} ...