Send JSON snippet as string as part of larger JSON request - c#

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.

Related

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

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.

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.

Dynamic Json deserialization

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:\\\"} ...

Restsharp parsing names not supported by C# Json

Hi I'm making a web api client that returns stuff in json.
I'm using Restsharp that uses newtonsoft.json to deserialize json objects.
The problem is that the server returns an object with a property with #Text as name. Is there a way to make restsharp parse this?
Here is a sample:
image: [
{
#text: http://userserve-ak.last.fm/serve/34s/55125087.png
size: small
}]
All the other properties are being parsed just right the only problem is this one, the property is a string type so no problem in here.
Regards
That JSON isn't valid; you need to put quotes around the #text key (so make it "#text") as well as both values (so "http://...png" and "small").

Categories