JSON message encoding - c#

Why does the JSON message I receive from a web service (unknown platform) contains double "" and \u0142? For example:
{""message"":""Nieprawid\u0142owy format""}
When I serialise my own object into a JSON message using json.net the resulting message contains only single ".
Finally, how to parse such a JSON message to get rid of "" and convert \u0142 to Unicode without deserializing the whole message into an object - just to get a proper JSON message text?

Didn't find the answer, however parsing (deserializing) JSON message with json.net results with proper data. So I gave up with getting the raw message.

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?

Corrupted JSON HTTP response

I am getting a HTTP request for a website and the content type is JSON. However, I am getting a nested JSON that is a unicode and is causing consistency problems.
Here is an example:
{"key1":"value",
"key2":"value",
"key3":{
u'key31':u'value',
u'key32':u'value'}}
This reminds me of python 2.7 troubles but I am not sure how to fix this JSON. I am using C# to parse it. Everything works correctly until I try to access key3.
The content should be a JSON object type but it is considered rather a value or a string.
Thanks for ya help. Is there a way to fix it if it is actually corrupted or am I parsing it wrongly?
You're correct that this json object is not complete / does not have the correct syntax. You're missing a closing '}' character.
How are you parsing your data? Try taking a look at this documentation.
your json object is not in valid formatted it should be like as folllows
{
"key1":"value",
"key2":"value",
"key3":{
" u'key31'":"u'value'",
"u'key32'":"u'value'"
}
}
by any chance do you get this json from python dump? coz Python's unicode literals are not valid JSON, and neither are single quotes

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.

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