I'm not an expert with json, actually I started last week working with it and I found myself having trouble trying to get map the following Json into a class.
S far I have copy the data from the browser into different Json files (isolate data to see the problem) and I realize that the property Config mess the deserialization process. If I (1) grab the entire data and (2) put it into a file and (3) add the extension .json and then later (4) just erase the Config property, everything works like a charm. But I want to be able to deserialize the whole thing.
For reading the url I'm using Flurl to generate a string from the response as I mentioned in a previous post using the GetStringAsync()
and for generating the Classes I just paste the response from Flurl or Postman into a Json2Csharp converter. Now for Deserialization I have try the following using Json.Net.
//Test 1
string cleanseStr = Regex.Unescape(FlurlResponse);
var myObj = JsonConvert.DeserializeObject<MyModel>(cleanseStr );
//Test 2
FlurlResponse= FlurlResponse.Replace("\\\\\\", "\\\\");
FlurlResponse= FlurlResponse.Replace("\\\\", "\\");
var myObj = JsonConvert.DeserializeObject<MyModel>(FlurlResponse);```
//Test 3
FlurlResponse= FlurlResponse.Replace("\\\\\\", "\\\\");
string cleanseStr = Regex.Unescape(FlurlResponse);
var myObj = JsonConvert.DeserializeObject<MyModel>(cleanseStr );
So far, I had no luck. I get errors at the beginning of the json or at the end saying that "cannot convert string into [MyModel]". I also took the response value from Test1 and Test2 (if I'm not istaken) and add it to this unserializer using "Json" to "Unserialized print_r" as my settings and compare them to my deserialization by hand (before using JsonConvert.DeserializeObject
) and I got the same result.
At the moment I'm stuck thinking what I'm missing in order to correctly deserialize the HTML string within the Json from the Config property. Probably someone that had face the same problem in the past can help me deserialize this or give any advice.
Error: Newtonsoft.Json.JsonSerializationException: 'Error converting value blah and 4millions characters later... "value":"<span fontWeight=\" to type TestingAPI.MyModel'. Path '', line 1, position 250 (for Test2) or 1950 (for Test1 and 3, 1950 means the end of the file).
ConfigProperty.json, What really matter is the format and not the words for the string, I left all the symbols as the original property.
"{\"Config\":[{\"id\":1,\"description\":\"Title\",\"value\":\"blah, blah\"},{\"id\":2,\"description\":\"Dislcaimer\",\"value\":\"<span fontWeight=\\\"bold\\\"> blah- </span>\\r\\n<br/><br/>blah 101 bl.ah. <span fontWeight=\\\"bold\\\"> blah (blah) blah 101 blah\\r\\n blah blah (blah)</span> blah, blah,\\r\\n blah. blah.\\r\\n blah, blah. blah, blah\\r\\n blah.\\r\\n<br/><br/>blah, blah, blah, blah blah\\r\\n blah blah-ah blah. blah (bl-ah blah, bl-ah blah, bl-ah blah, and >blah)\\r\\n blah.\\r\\n<br/><br/>blah, blah\\r\\n blah. \\r\\n<br/><br/>blah:<a
href=\\\"http://blah.blah.blah/blah/blah/blah.htm#blah\\\">http://blah.blah.blah/blah/blah/blah.htm#blah</a>\"}]}"
EDIT
I solved my problem in a not very pleasant way, manual deserialization:
string cleanseStr= Regex.Unescape(FlurlResponseString);
cleanseStr= cleanseStr.Replace("\r\n", "");
cleanseStr= cleanseStr.Replace("\\\\\\", "\\\\");
cleanseStr= cleanseStr.Replace("=\"", "=\\\"");
cleanseStr= cleanseStr.Remove(0, 1);
cleanseStr= cleanseStr.Remove(scapedJson.Length - 1, 1);
MyModel _myModel= new MyModel();
JsonConvert.PopulateObject(cleanseStr, _myModel);
The correct way is proposed by @BrianRogers, thank you @BrianRogers.