0

I am trying to desearilize a object as given below ,one of the string having some special characters associated with it and facing some errors.

obj= JsonConvert.DeserializeObject<response>(request.Message)

one of the input string looks like below

"Message":"{
    'Id':'text me on dec may\' 17',
}"

Error details:After parsing a value an unexpected character was encountered: 1. Path 'Id', line 4, position 56

thanks in advance

Valerica
  • 1,618
  • 1
  • 13
  • 20
Raushan Rauf
  • 45
  • 10
  • I'm pretty sure JSON is only valid with double quotes -- try replacing the single quotes. – arthurakay Oct 20 '17 at 13:44
  • Possible duplicate of [Parsing string as JSON with single quotes?](https://stackoverflow.com/questions/36038454/parsing-string-as-json-with-single-quotes) – arthurakay Oct 20 '17 at 13:44
  • arthurakay, am facing issues only whenever am getting a special character inside the string ,otherwise Deserialize working as perferct, replace single quotes will be a costly operation ryt, as i am working huge amount of data. is there any setting/config level changes that can be done for accepting special characters as well. – Raushan Rauf Oct 20 '17 at 14:13
  • adding to above while serializing it is correctly getting appended with back slash before the character(\') – Raushan Rauf Oct 20 '17 at 14:14

2 Answers2

0

In my prior comment, I had mis-read the initial problem. Sorry about that.

In JavaScript I can do this:

JSON.parse('{"Message":"{\'Id\':\'text me on dec may\' 17\',}"}')

Which works just fine. Notice that I had to also escape the single quotes surrounding the inner string.

arthurakay
  • 5,631
  • 8
  • 37
  • 62
  • In My Scenario, I am consuming a external service , which will send above json having around 50 fields like ID in the above message, so i wont be able to to modify the message, am just consuming the trigger from the external service and deseraliizing the data by using class with all 50 attributes ,and pushing to DB, is there any other way to rectify the issue. thanks you so much for quick response – Raushan Rauf Oct 20 '17 at 14:52
0

I have found the issue, actually it is problem with the input, after data serialization input should be appended with 2 backslash for each special characters, as when catching the data in API method, one of the backslash will be removed during data assigned(get/set) to the property.so if two slashes are there one will remove and another will use for deserailise the data.

Message":"{ 'Id':'text me on dec may\' 17', }"

thanks all for you support

Raushan Rauf
  • 45
  • 10