How to Convert a string containing escaped JSON to object? - c#

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?

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?

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.

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.

JSON string parsing issue in C#

The JSON string that I receive is inconsistent. Sometimes some of the data elements in the JSON return as 'List ' type, the other times return as 'dictionary' type. Currently using JavaScriptSerializer to Deserialize the JSON string, but it gives me an exception in the cases like
for example - I have a data field "jpjhseq" which I have declared as a dictionary but at times the JSON string returns this as a string and I get an exception. This issue happens for other fields as well.
JavaScriptSerializer serializer = new JavaScriptSerializer();
Foo foo = serializer.Deserialize<Foo>(jsonString);
Many a times I get following json string
{"element1":{"0":"0","1":"S","2":"S","3":"J","4":"Q","5":"X","6":"M"},"element2":{"1":"one" ,"2":"two","4":"four","5":"five","6":"six","7":"seven","8":"eight"}}
for other data i get following json string
{"element1":["0","S","S","J","Q","X","M"],"element2":["one" ,"two","four","five","six","seven","eight"]}
How can i write a generic json parser for such inconsistent json strings?
Key names will be standard, but the data type of their values may vary.
What can be done to resolve this issue?

unexpected non-whitespace character after JSON data

string result="12334,23432,3453455";
I am getting this string through Ajax call but it gives me the following error:
"unexpected non-whitespace character after JSON data"
When I remove comma's between strings it works fine .How to handle this?. I want to put value in textarea with comma's after the Ajax call
Whatever's outputting that isn't doing so in JSON format, but more like CSV.
A few options:
If you're able, fix the output method to correctly output JSON
Parse the string like a CSV
e.g. "12334,23432,3453455".split(',')
Conform the output to JSON first, then parse
e.g. JSON.parse("["+"12334,23432,3453455"+"]") (wrap with [])
Specify dataType:'text' in your $.ajax call.
Options 1-3 of the above would result in [12334,23432,3453455] as a javascript array of numbers, while Option 4 will simply result in "12334,23432,3453455" as a string.
BTW, using JSON.NET, this is what it should result in:
// As an array:
Int32[] ary = new[]{ 12334, 23432, 3453455 };
Console.WriteLine(JsonConvert.SerializeObject(ary));
// [12334,23432,3453455]
// As a string:
String str = "12334,23432,3453455";
Console.WriteLine(JsonConvert.SerializeObject(str));
// "12334,23432,3453455"
Your data has to be parsed by your JSON parser.
If your data is an array, your string should look like:
"[12334,23432,3453455]"
or should it be astring:
"\"12334,23432,3453455\""

Categories