Step 1. Unserialize First Message to dictionary1 (receivedDict)
Step 2. Unserialize dictionary1[“data”] to dictionary2 (receivedDict2)
Whenever the second layer contains another serialized array, it fails. It works if it doesn’t. The third step would be to unserialize dictionary2[“replaceArray”] but I’m not reaching that stage due to the error.
Piccy:

The code:
CGlobals.output("Received a message with length: " + _message.Length);
Console.WriteLine("message Contains : " + _message);
//Unserialize main message
Dictionary<string, string> receivedDict = new Dictionary<string, string>();
try
{
receivedDict = CJsonStuff.unserializeDict(_message);
}
catch (Exception ex) { return retError(1, ex.Message,false); }
if (receivedDict.Count < 1) return retError(2, "",false);
//List all elements for debugging.
string temp = "";
foreach (KeyValuePair<string, string> item in receivedDict)
{
temp += item.Key + " : " + item.Value + "\n";
}
Console.WriteLine("\n" + temp + "\n");
//Parse jobID
int jobID = -1;
try
{
jobID = Int32.Parse(receivedDict["jobID"]);
}
catch (Exception ex) { return retError(3, ex.Message,false); }
//Parse alteredID
int alteredID = -1;
bool isAltered = false;
try
{
Dictionary<string, string> receivedDict2 = new Dictionary<string, string>();
Console.WriteLine("Unserializing : "+receivedDict["data"]);
receivedDict2 = CJsonStuff.unserializeDict(receivedDict["data"]); //Error occurs here
Console.WriteLine("Unserialized data");
//Show all elements for debugging.
string temp2 = "";
foreach (KeyValuePair<string, string> item in receivedDict2)
{
temp2 += item.Key + " : " + item.Value + "\n";
}
Console.WriteLine("\n" + temp2 + "\n");
if (receivedDict2.ContainsKey("alteredID"))
{
alteredID = Int32.Parse(receivedDict2["alteredID"]);
isAltered = true;
}
}
catch (Exception ex) { Console.WriteLine(ex.Message); return retError(6, ex.Message, false, jobID); }
CJSonStuff:
public static Dictionary<string, string> unserializeDict(string thestring)
{
Dictionary<string, string> dict = new Dictionary<string, string>();
dict = JsonConvert.DeserializeObject<Dictionary<string, string>>(thestring);
return dict;
}
So basically, it unserializes the first message just fine, even though it contains an embedded serialized array, but trying to do that exact same thing again fails.. how do I fix this?
That’s because
datais a string, it’s not a JSON object, it’s a string representation of a JSON object, that’s a big difference (notice the"at the start and end ofdata).You can’t deserialize a JSON object into a string, as far as I know and it doesn’t make sense to me to do that. You should either deserialize it as
JObjectorDictionary<string, object>, or (the best option if the data doesn’t change) create a class for this and deserialize into that.