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

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.

Related

JsonNode.Parse wrong UTF8

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?

Are there any methods to automatically convert strings inside JSON response to BASE64?

I'm returning objects as JSON results from my APIController inside an ASP.NET MVC web-API.
To convert objects to JSON results I use the below syntax:
return Ok(new WebServiceResult(...));
Are there any methods to convert strings automatically to BASE64 encoding during object to JSON conversions?
Can OK handle non-English characters during JSON conversions and avoid failure to decode that string on other platforms (I mean does it support encodings like UTF-8 or does it handle escape characters that reside inside the contents which are going to be converted such as { character or : character)?

How to convert UTF-8 JSON containing Unicode characters to Unicode encoding?

I am posting JSON requests to an URL using Unity and C#. I am getting back responses in JSON format with texts containing some special characters. Such as: "ó", "ü","é" etc.
For the posting and receiving, I am using the WWW class of Unity. I have tried to convert the incoming byte[] the following way:
byte[] incomingBytes = Encoding.Convert(Encoding.UTF8, Encoding.Unicode, www.downloadHandler.data);
var str = Encoding.Unicode.GetString(incomingBytes);
Debug.Log(str);
It makes no effect on the output. I am still getting the following example text back: csap\u00e1sra instead of csapásra. Any idea how to get back a field containing special characters from a JSON in Unity/C#?

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.

JSON message encoding

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.

Categories