JsonNode.Parse wrong UTF8 - c#

I have this Json string which I'll need to convert into an JsonNode.
{"3":"Maybachstraße 14","4":"45659","5":"Recklinghausen","6":"1","7":""}
However, the JSON node always believes that the JSON is this string instead:
{"3":"Maybachstra\u00DFe 14","4":"45659","5":"Recklinghausen","6":"1","7":""}
What am I doing wrong here?

Related

C# Pass filename as json parameter- Getting error "Unrecognized escape sequence. "

I want to pass a filepath through JSON. On deserializing I am getting error:
Unrecognized escape sequence. (43): {"Jobtype": "StepBatch","SelectedId": "D:\Input\file1.CATPart"}
I have escaped characters but it still shows error...am I missing something here?
string json = "{\"Jobtype\": \"StepBatch\",\"SelectedId\": \"D:\\Input\\file1.CATPart\"}";
var jsonObj = new JavaScriptSerializer().Deserialize<List<Arguments>>(json);
The problem is that the content of your string at execution time is:
{"Jobtype": "StepBatch","SelectedId": "D:\Input\file1.CATPart"}
That's not valid JSON, because of the backslashes in the value for SelectedId. You'd need the JSON to be:
{"Jobtype": "StepBatch","SelectedId": "D:\\Input\\file1.CATPart"}
so your C# would have to be:
string json = "{\"Jobtype\": \"StepBatch\",\"SelectedId\": \"D:\\\\Input\\\\file1.CATPart\"}";
However, given that you're immediately deserializing the JSON anyway, I'd suggest getting rid of the JSON part entirely, and just creating the Arguments values yourself.
If you need to produce JSON, create the right values directly, and then get JavaScriptSerializer (or preferrably Json.NET) to create the JSON for you, instead of hand-coding it.

How to Convert a string containing escaped JSON to object?

I have a JSON received from a third part, which I can't change.
This JSON has a property with special characters
"CryptoKey":"dqwe`fqer]OS#xMKA^Qd[3123ddFjqr412_hRHBXTfNEyp\lVLoia",
So, when I try to deserialize it, I receive the following error:
Newtonsoft.Json.JsonReaderException: 'Bad JSON escape sequence: \l. Path '['148/FOEConfiguration'].CryptoKey', line 7, position 75.'
What I'm doing is:
string text = File.ReadAllText(configFile);
dynamic result = JsonConvert.DeserializeObject(text);
Is there a way to deserialize this to an object without breaking it?
Or I'll have to read in a diferent way?

Send JSON snippet as string as part of larger JSON request

I'm interacting with a Web API in which all communication is done with JSON - i.e. we serialise data to send them to them and vice versa.
This works fine, but there is a field called 'CustomData' which is a string type, and we want to use this to store 2-3 more variables. The API requires to send through a JSON string which it then stores and parses internally. However, this JSON is obviously being deserialised on their end as an object and we're getting an error saying it's expecting a string but has got an object.
Is there anyway I can mark this string field as raw JSON, and instruct the server NOT to deserialise it?
I'm using JSON.NET
May be try to encode the JSON string from the sender's side and decode it on your receiving end before adding to your database? That way your overall JSON deserialize won't recognize the data string as JSON.

XML String with no parent node to JSON with C#

I have an XML string that does not contain a parent node. This XML is a representation of a json request for an API. It seems pointless, but it is done this way to make it easy for non programmers to read the file. In order to convert the XML to json, pretty much everything i have seen says to convert the string to an XMLDocument and then use the following to get the json.
string jsonText = JsonConvert.SerializeXmlNode(doc);
The problem i have here is that the xml is not really valid and because of this, it cannot be converted to an xml document. What i really want is to be able to do this.
string jsonText = JsonConvert.SerializeXmlNode(doc.InnerXml);
This doesnt work since innerXML is a string and not an object. I have been able to get it to work by creating a root element and then just using a sub string to cut the resulting string, but this seems pointless. There has to be a better way to do this without having to add xml only to have to remove it from the json afterwards. Is it possible to convert a piece of xml like the xml below into json like the example below.
<rootnode>
<fielda>a</fielda>
<fieldb>b</fieldb>
</rootnode>
Converts to
{
"fielda": "a",
"fieldb": "b"
}
There's an overload of SerializeXmlNode that takes a boolean omitRootObject:
string jsonText = JsonConvert.SerializeXmlNode(doc, Formatting.None, true);
JsonConvert.SerializeXmlNode has an overloaded method which you could use to ignore root.
string jsonText = JsonConvert.SerializeXmlNode(doc, Formatting.None, true);
Third parameter is for omitting RootObject

deserialize JSON object receive via REST API (c# web api 2)

I'm sending an JSON Object to a rest server using a WebRequest.
The ContentType is set to application/json
The original class is serialized to JSON string, than the string in converted to byte (using BlockCopy than makes it a unicode) and sent via HTTP.
On the server side I revieve a a JContainer Object rather than the string (probably b/c of the Content Type)
How do I Deserialize this object directly with JSON.NET ?
I was able to turn this to a string and then deserialize it, but with some errors that I can't trace as the string is unreadable (lots of \0 from the unicode conversion....)
My question is - can I deserialize the original JSON Object ?
Is there a way to convert this object to a regular ASCII string ?
public void Put(MyClass cls)
{
}
// I get nothing
public void Put(JContainer cls)
{
string myjson = cls.ToString()
** Update: This string is the original JSON but in UNICODE.
** this text doesn't deserialized to the original class.
}
The problem was in the encoding.
using ASCII encoding instead of Unicode solved all De-Serialization problems.
Just Took the JContainer.ToString() and sent it to the Deserialize function.

Categories